Basic search UI (no download links yet)

This commit is contained in:
2024-11-09 17:13:02 +01:00
parent 804e5e91a0
commit bfb76389cf
35 changed files with 5502 additions and 4 deletions

View File

@@ -0,0 +1,94 @@
using System;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace StreamCinemaLib.API;
public class Metadata
{
private const string UserAgent = "User-Agent: XBMC/19 (Linux 6.1; http://www.xbmc.org)";
private const string AccessToken = "9ajdu4xyn1ig8nxsodr3";
private const int MaxPageLimit = 1000;
private static readonly Uri ApiRoot = new Uri("https://plugin.sc2.zone/api/");
private static readonly Uri ApiFilter = new Uri(ApiRoot, "media/filter/v2/");
public static Task<FilterResponse?> SearchAsync(string expression, ItemOrder order = ItemOrder.Descending, FilterSortBy sort = FilterSortBy.Score, ItemType type = ItemType.All, int limit = 0, CancellationToken cancel = default)
{
if (expression == null)
throw new ArgumentNullException();
if (limit < 0)
throw new ArgumentOutOfRangeException();
if (limit == 0 || limit > MaxPageLimit)
limit = MaxPageLimit;
UriBuilder uri = new UriBuilder(new Uri(ApiFilter, "search"));
uri.Query = $"?access_token={AccessToken}&value={Uri.EscapeDataString(expression)}&order={ToString(order)}&sort={ToString(sort)}&type={ToString(type)}&size={limit.ToString()}";
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new CustomDateTimeConverter());
return Program._http.GetFromJsonAsync<FilterResponse>(uri.Uri, options, cancel);
}
private static string ToString(ItemOrder value)
{
switch (value)
{
case ItemOrder.Ascending: return "asc";
case ItemOrder.Descending: return "desc";
default: throw new ArgumentOutOfRangeException();
}
}
private static string ToString(FilterSortBy value)
{
switch (value)
{
case FilterSortBy.Score: return "score";
case FilterSortBy.Year: return "year";
case FilterSortBy.Premiered: return "premiered";
case FilterSortBy.DateAdded: return "dateAdded";
case FilterSortBy.LastChildrenDateAdded: return "lastChildrenDateAdded";
case FilterSortBy.LastChildPremiered: return "lastChildPremiered";
case FilterSortBy.Title: return "title";
case FilterSortBy.PlayCount: return "playCount";
case FilterSortBy.LastSeen: return "lastSeen";
case FilterSortBy.Episode: return "episode";
case FilterSortBy.News: return "news";
case FilterSortBy.Popularity: return "popularity";
case FilterSortBy.Trending: return "trending";
case FilterSortBy.LangDateAdded: return "langDateAdded";
case FilterSortBy.Custom: return "custom";
default: throw new ArgumentOutOfRangeException();
}
}
private static string ToString(ItemType value)
{
switch (value)
{
case ItemType.TVShow: return "tvshow";
case ItemType.Concert: return "concert";
case ItemType.Anime: return "anime";
case ItemType.Movie: return "movie";
case ItemType.Season: return "season";
case ItemType.Episode: return "episode";
case ItemType.All: return "*";
default: throw new ArgumentOutOfRangeException();
}
}
sealed class CustomDateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
}
}