Skip to content

Commit

Permalink
Merge pull request #77 from insomniachi/anime_plugin_improvements
Browse files Browse the repository at this point in the history
plugin improvements
  • Loading branch information
insomniachi authored Dec 28, 2023
2 parents 691c79a + 91951ef commit c7f0de8
Show file tree
Hide file tree
Showing 122 changed files with 1,480 additions and 1,388 deletions.
Binary file modified Plugins Store/Totoro.Plugins.Anime.AllAnime.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Anime.AnimePahe.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Anime.AnimeSaturn.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Anime.GogoAnime.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Anime.WitAnime.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Anime.YugenAnime.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Manga.MangaDex.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.MediaDetection.Generic.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.MediaDetection.Vlc.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.MediaDetection.Win11MediaPlayer.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Torrents.AnimeTosho.dll
Binary file not shown.
Binary file modified Plugins Store/Totoro.Plugins.Torrents.Nya.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Flurl;
using Totoro.Plugins.Anime.Models;
using Totoro.Plugins.Options;
using Xunit.Abstractions;

namespace Totoro.Plugins.Anime.AllAnime.Tests;
Expand All @@ -15,8 +17,8 @@ public class StreamProviderTests(ITestOutputHelper output)
private readonly JsonSerializerOptions _searializerOption = new() { WriteIndented = true };
private readonly Dictionary<string, string> _urlMap = new()
{
{ Hyouka, Url.Combine(Config.Url, "/anime/dxxqKsaMhdrdQxczP") },
{ Hyakkano, Url.Combine(Config.Url, "/anime/pp9g8Qt7iem4RMjbJ") }
{ Hyouka, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/dxxqKsaMhdrdQxczP") },
{ Hyakkano, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/pp9g8Qt7iem4RMjbJ") }
};
private readonly bool _allEpisodes = false;

Expand All @@ -30,7 +32,7 @@ public async Task GetNumberOfEpisodes(string key, int expected)
var sut = new StreamProvider();

// act
var actual = await sut.GetNumberOfStreams(url, Models.StreamType.EnglishSubbed);
var actual = await sut.GetNumberOfStreams(url, Models.StreamType.Subbed(Languages.English));

// assert
Assert.Equal(expected, actual);
Expand All @@ -46,7 +48,7 @@ public async Task GetStreams(string key)
var sut = new StreamProvider();

// act
var result = await sut.GetStreams(url, _allEpisodes ? Range.All : 12..12, Models.StreamType.EnglishSubbed).ToListAsync();
var result = await sut.GetStreams(url, _allEpisodes ? Range.All : 12..12, Models.StreamType.Subbed(Languages.English)).ToListAsync();

Assert.NotEmpty(result);
foreach (var item in result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json.Linq;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Contracts.Optional;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

Expand Down Expand Up @@ -47,7 +48,7 @@ class AiredEpisode : IAiredAnimeEpisode, IHaveCreatedTime, IHaveAnilistId, IHave

public async IAsyncEnumerable<IAiredAnimeEpisode> GetRecentlyAiredEpisodes(int page = 1)
{
var jObject = await Config.Api
var jObject = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(SHOWS_QUERY)
.SetGraphQLVariables(new
{
Expand All @@ -58,8 +59,8 @@ public async IAsyncEnumerable<IAiredAnimeEpisode> GetRecentlyAiredEpisodes(int p
},
limit = 26,
page,
translationType = Config.StreamType.ConvertToTranslationType(),
countryOrigin = Config.CountryOfOrigin
translationType = ConfigManager<Config>.Current.StreamType.ConvertToTranslationType(),
countryOrigin = ConfigManager<Config>.Current.CountryOfOrigin
})
.PostGraphQLQueryAsync()
.ReceiveGraphQLRawJsonResponse();
Expand All @@ -76,7 +77,7 @@ public async IAsyncEnumerable<IAiredAnimeEpisode> GetRecentlyAiredEpisodes(int p
var ep = $"{item?["lastEpisodeInfo"]?["sub"]?["episodeString"]}";
_ = int.TryParse(ep, out int epInt);
var datetime = new DateTime(year, month, day, hour, min, 0).ToLocalTime();
var animeUrl = Url.Combine(Config.Url, $"/anime/{item?["_id"]}");
var animeUrl = Url.Combine(ConfigManager<Config>.Current.Url, $"/anime/{item?["_id"]}");
var malId = $"{item?["malId"]}";
var aniListId = $"{item?["aniListId"]}";

Expand Down
5 changes: 3 additions & 2 deletions Plugins/Anime/Totoro.Plugins.Anime.AllAnime/Catalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json.Linq;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Contracts.Optional;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

Expand Down Expand Up @@ -55,7 +56,7 @@ class SearchResult : ICatalogItem, IHaveImage, IHaveSeason, IHaveRating, IHaveMa

public async IAsyncEnumerable<ICatalogItem> Search(string query)
{
var jObject = await Config.Api
var jObject = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(SEARCH_QUERY)
.SetGraphQLVariables(new
{
Expand All @@ -75,7 +76,7 @@ public async IAsyncEnumerable<ICatalogItem> Search(string query)
_ = long.TryParse($"{item?["malId"]}", out long malId);
_ = long.TryParse($"{item?["aniListId"]}", out long aniListId);
var title = $"{item?["name"]}";
var url = Url.Combine(Config.Url, $"/anime/{item?["_id"]}");
var url = Url.Combine(ConfigManager<Config>.Current.Url, $"/anime/{item?["_id"]}");
var season = "";
var year = "";
if (item?["season"]?.HasValues == true)
Expand Down
30 changes: 24 additions & 6 deletions Plugins/Anime/Totoro.Plugins.Anime.AllAnime/Config.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
using Totoro.Plugins.Anime.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Totoro.Plugins.Anime.Models;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

internal static class Config
public class Config : AnimeProviderConfigObject
{
public static string Url { get; set; } = "https://allanime.to/";
public static StreamType StreamType { get; set; } = StreamType.EnglishSubbed;
public static string CountryOfOrigin { get; set; } = "JP";
public static string Api = "https://api.allanime.day/api";
[Description("Url to home page")]
[Glyph(Glyphs.Url)]
public string Url { get; set; } = "https://allanime.to/";

[DisplayName("Stream Type")]
[Description("Choose what to play by default, sub/dub")]
[AllowedValues(@"English Subbed", @"English Dubbed", @"Raw")]
[Glyph(Glyphs.StreamType)]
public StreamType StreamType { get; set; } = StreamType.Subbed(Languages.English);

[DisplayName("Country Of Origin")]
[Description("Filter anime by country")]
[AllowedValues("ALL", "JP", "CN", "KR")]
[Glyph("\uF2B7")]
public string CountryOfOrigin { get; set; } = "JP";

[IgnoreDataMember]
public string Api = "https://api.allanime.day/api";
}
6 changes: 3 additions & 3 deletions Plugins/Anime/Totoro.Plugins.Anime.AllAnime/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public static string ConvertToTranslationType(this StreamType streamType)
{
return streamType switch
{
StreamType.EnglishSubbed => "sub",
StreamType.EnglishDubbed => "dub",
StreamType.Raw => "raw",
{ AudioLanguage: Languages.Japanese, SubtitleLanguage: Languages.English } => "sub",
{ AudioLanguage: Languages.English, SubtitleLanguage: _ } => "dub",
{ AudioLanguage: Languages.Japanese, SubtitleLanguage: "" } => "raw",
_ => throw new NotSupportedException(streamType.ToString())
};
}
Expand Down
3 changes: 2 additions & 1 deletion Plugins/Anime/Totoro.Plugins.Anime.AllAnime/IdMapper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using FlurlGraphQL.Querying;
using Totoro.Plugins.Anime.Models;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

internal class IdMapper : IIdMapper
{
public async Task<AnimeId> MapId(string url)
{
var jObject = await Config.Api
var jObject = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(StreamProvider.SHOW_QUERY)
.SetGraphQLVariable("showId", url.Split('/').LastOrDefault()?.Trim())
.PostGraphQLQueryAsync()
Expand Down
42 changes: 3 additions & 39 deletions Plugins/Anime/Totoro.Plugins.Anime.AllAnime/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,27 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Anime.Models;
using Totoro.Plugins.Contracts;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

[ExcludeFromCodeCoverage]
public class Plugin : IPlugin<AnimeProvider>
public class Plugin : Plugin<AnimeProvider, Config>
{
public AnimeProvider Create() => new()
public override AnimeProvider Create() => new()
{
Catalog = new Catalog(),
StreamProvider = new StreamProvider(),
AiredAnimeEpisodeProvider = new AiredEpisodesProvider(),
IdMapper = new IdMapper(),
};

public PluginInfo GetInfo() => new()
public override PluginInfo GetInfo() => new()
{
DisplayName = "AllAnime",
Name = "allanime",
Version = Assembly.GetExecutingAssembly().GetName().Version!,
Icon = typeof(Plugin).Assembly.GetManifestResourceStream("Totoro.Plugins.Anime.AllAnime.allanime-icon.png"),
Description = "AllAnime's goal is to provide you with the highest possible amount of daily anime episodes/manga chapters for free and without any kind of limitation."
};

public PluginOptions GetOptions()
{
return new PluginOptions()
.AddOption(x => x.WithName(nameof(Config.Url))
.WithDisplayName("Url")
.WithDescription("Url to home page")
.WithValue(Config.Url)
.WithGlyph("\uE71B")
.ToPluginOption())
.AddOption(x => x.WithName(nameof(Config.StreamType))
.WithDisplayName("Stream Type")
.WithDescription("Choose what to play by default, sub/dub")
.WithGlyph("\uF2B7")
.WithValue(Config.StreamType)
.WithAllowedValues(new[] { StreamType.EnglishSubbed, StreamType.EnglishDubbed, StreamType.Raw })
.ToSelectablePluginOption())
.AddOption(x => x.WithName(nameof(Config.CountryOfOrigin))
.WithDisplayName("Country Of Origin")
.WithDescription("Filter anime by country")
.WithGlyph("\uE909")
.WithValue(Config.CountryOfOrigin)
.WithAllowedValues(new[] { "ALL", "JP", "CN", "KR" })
.ToSelectablePluginOption());
}

public void SetOptions(PluginOptions options)
{
Config.Url = options.GetString(nameof(Config.Url), Config.Url);
Config.StreamType = options.GetEnum(nameof(Config.StreamType), Config.StreamType);
Config.CountryOfOrigin = options.GetString(nameof(Config.CountryOfOrigin), Config.CountryOfOrigin);
}

object IPlugin.Create() => Create();
}
33 changes: 18 additions & 15 deletions Plugins/Anime/Totoro.Plugins.Anime.AllAnime/StreamProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Diagnostics;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
Expand All @@ -9,6 +10,7 @@
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Anime.Models;
using Totoro.Plugins.Helpers;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AllAnime;

Expand Down Expand Up @@ -54,11 +56,11 @@ internal partial class StreamProvider : IMultiLanguageAnimeStreamProvider, IAnim
[GeneratedRegex("(?<=/clock)(?=[?&#])")]
private static partial Regex ClockRegex();

public Task<int> GetNumberOfStreams(string url) => GetNumberOfStreams(url, Config.StreamType);
public Task<int> GetNumberOfStreams(string url) => GetNumberOfStreams(url, ConfigManager<Config>.Current.StreamType);

public async Task<int> GetNumberOfStreams(string url, StreamType streamType)
{
var jObject = await Config.Api
var jObject = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(SHOW_QUERY)
.SetGraphQLVariable("showId", url.Split('/').LastOrDefault()?.Trim())
.PostGraphQLQueryAsync()
Expand All @@ -78,15 +80,15 @@ public async Task<int> GetNumberOfStreams(string url, StreamType streamType)
return total;
}

public IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Range range) => GetStreams(url, range, Config.StreamType);
public IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Range range) => GetStreams(url, range, ConfigManager<Config>.Current.StreamType);

public async IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Range range, StreamType streamType)
{
var versionResponse = await Config.Url.AppendPathSegment("getVersion").GetJsonAsync<GetVersionResponse>();
var versionResponse = await ConfigManager<Config>.Current.Url.AppendPathSegment("getVersion").GetJsonAsync<GetVersionResponse>();
var apiEndPoint = new Url(versionResponse?.episodeIframeHead ?? "");
var id = url.Split('/').LastOrDefault()?.Trim();

var jObject = await Config.Api
var jObject = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(SHOW_QUERY)
.SetGraphQLVariable("showId", id)
.PostGraphQLQueryAsync()
Expand All @@ -105,7 +107,7 @@ public async IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Ran
yield break;
}

var sorted = GetEpisodes(episodesDetail!, Config.StreamType).OrderBy(x => x.Length).ThenBy(x => x).ToList();
var sorted = GetEpisodes(episodesDetail!, ConfigManager<Config>.Current.StreamType).OrderBy(x => x.Length).ThenBy(x => x).ToList();
var total = int.Parse(sorted.LastOrDefault(x => int.TryParse(x, out int e))!);
var (start, end) = range.Extract(total);
foreach (var ep in sorted)
Expand All @@ -126,18 +128,18 @@ public async IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Ran
continue;
}

var streamTypes = new List<StreamType>() { StreamType.EnglishSubbed };
var streamTypes = new List<StreamType>() { StreamType.Subbed(Languages.English) };

if (episodesDetail.dub?.Contains(ep) == true)
{
streamTypes.Add(StreamType.EnglishDubbed);
streamTypes.Add(StreamType.Dubbed(Languages.English));
}
if (episodesDetail.raw?.Contains(ep) == true)
{
streamTypes.Add(StreamType.Raw);
streamTypes.Add(StreamType.Raw());
}

var jsonNode = await Config.Api
var jsonNode = await ConfigManager<Config>.Current.Api
.WithGraphQLQuery(EPISODE_QUERY)
.SetGraphQLVariables(new
{
Expand Down Expand Up @@ -168,6 +170,7 @@ public async IAsyncEnumerable<VideoStreamsForEpisode> GetStreams(string url, Ran
if (stream is not null)
{
stream.Episode = e;
stream.StreamTypes.AddRange(streamTypes);
yield return stream;
yield break;
}
Expand Down Expand Up @@ -319,10 +322,10 @@ private static List<string> GetEpisodes(EpisodeDetails episodeDetails, StreamTyp
{
return streamType switch
{
StreamType.EnglishSubbed => episodeDetails.sub,
StreamType.EnglishDubbed => episodeDetails.dub,
StreamType.Raw => episodeDetails.raw,
_ => throw new NotSupportedException(streamType.ToString())
{ AudioLanguage: Languages.Japanese, SubtitleLanguage: Languages.English } => episodeDetails.sub,
{ AudioLanguage: Languages.English, SubtitleLanguage: _ } => episodeDetails.dub,
{ AudioLanguage: Languages.Japanese, SubtitleLanguage: "" } => episodeDetails.raw,
_ => throw new UnreachableException(streamType.ToString())
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<Version>2.6</Version>
<Version>2.7</Version>
<OutputPath Condition="$(Configuration) == Release">..\..\..\Plugins Store\</OutputPath>
<AppendTargetFrameworkToOutputPath Condition="$(Configuration) == Release">false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath Condition="$(Configuration) == Release">false</AppendRuntimeIdentifierToOutputPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Flurl;
using Totoro.Plugins.Options;
using Xunit.Abstractions;

namespace Totoro.Plugins.Anime.AnimePahe.Tests;
Expand All @@ -17,10 +18,10 @@ public class StreamProviderTests
private readonly JsonSerializerOptions _searializerOption = new() { WriteIndented = true };
private readonly Dictionary<string, string> _urlMap = new()
{
{ Hyouka, Url.Combine(Config.Url, "/anime/bcc68f86-30c0-477f-6b6b-615cecdad923") },
{ OshiNoKo, Url.Combine(Config.Url, "/anime/ae95c1fc-25d9-d824-1340-d46440e9652e") },
{ RentAGFS3, Url.Combine(Config.Url, "/anime/e53e054a-c233-f2ba-45d5-d762e183cc96") },
{ JjkS2, Url.Combine(Config.Url, "/anime/c24ef525-a643-7dc5-1882-a6b27b2421c2") }
{ Hyouka, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/bcc68f86-30c0-477f-6b6b-615cecdad923") },
{ OshiNoKo, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/ae95c1fc-25d9-d824-1340-d46440e9652e") },
{ RentAGFS3, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/e53e054a-c233-f2ba-45d5-d762e183cc96") },
{ JjkS2, Url.Combine(ConfigManager<Config>.Current.Url, "/anime/c24ef525-a643-7dc5-1882-a6b27b2421c2") }
};

private readonly bool _allEpisodes = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Flurl.Http;
using Totoro.Plugins.Anime.Contracts;
using Totoro.Plugins.Contracts.Optional;
using Totoro.Plugins.Options;

namespace Totoro.Plugins.Anime.AnimePahe;

Expand All @@ -21,7 +22,7 @@ class AiredEpisode : IAiredAnimeEpisode, IHaveCreatedTime

public async IAsyncEnumerable<IAiredAnimeEpisode> GetRecentlyAiredEpisodes(int page = 1)
{
var json = await Config.Url.AppendPathSegment("api")
var json = await ConfigManager<Config>.Current.Url.AppendPathSegment("api")
.SetQueryParams(new
{
m = "airing",
Expand All @@ -36,7 +37,7 @@ public async IAsyncEnumerable<IAiredAnimeEpisode> GetRecentlyAiredEpisodes(int p
{
var title = $"{item!["anime_title"]}";
var image = $"{item["snapshot"]}";
var url = Url.Combine(Config.Url, "anime", $"{item["anime_session"]}");
var url = Url.Combine(ConfigManager<Config>.Current.Url, "anime", $"{item["anime_session"]}");
var episode = (int)(double)item!["episode"]!.AsValue();
var createdAt = DateTime.ParseExact($"{item["created_at"]}", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).ToLocalTime();

Expand Down
Loading

0 comments on commit c7f0de8

Please sign in to comment.