Skip to content

Commit

Permalink
Include ratelimit retries, fix bad clipboard links
Browse files Browse the repository at this point in the history
  • Loading branch information
Drutol committed Jul 11, 2023
1 parent 89ab396 commit d3c2016
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 43 deletions.
2 changes: 1 addition & 1 deletion MALClient.Android.Adapters/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MALClient.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.drutol.malclient" android:installLocation="auto" android:versionName="1.5.13.5" android:versionCode="161">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.drutol.malclient" android:installLocation="auto" android:versionName="1.5.13.6" android:versionCode="162">
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
2 changes: 1 addition & 1 deletion MALClient.Models/MALClient.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JikanDotNet" Version="2.6.0" />
<PackageReference Include="JikanDotNet" Version="2.6.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion MALClient.XShared/Comm/Anime/AnimeEpisodesQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Android.Runtime;
using JikanDotNet;
using JikanDotNet.Config;
using Newtonsoft.Json;
using AnimeEpisode = MALClient.Models.Models.Anime.AnimeEpisode;

Expand All @@ -23,7 +24,7 @@ public async Task<List<AnimeEpisode>> GetEpisodes(int animeId, bool force = fals
{
var result = new List<AnimeEpisode>();
int page = 1;
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
while (true)
{
try
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Anime/AnimeGeneralDetailsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<AnimeGeneralDetailsData> GetAnimeDetails(bool force, string id

var requestedApiType = apiOverride ?? CurrentApiType;
var response = string.Empty;
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
try
{
switch (requestedApiType)
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Anime/AnimeGenreStudioQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<List<SeasonalAnimeData>> GetAnime()
if (output.Count > 0)
return output;

var jikan = new Jikan();
var jikan = JikanClient.Jikan;

var jikanPage = _page;
try
Expand Down
49 changes: 39 additions & 10 deletions MALClient.XShared/Comm/Anime/AnimeReviewsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -39,17 +40,45 @@ public async Task<List<AnimeReviewData>> GetAnimeReviews(bool force = false)

try
{
var jikan = new Jikan();
Root reviews;
if (_anime)
{
var json = await _client.GetStringAsync($"https://api.jikan.moe/v4/anime/{_targetId}/reviews?page=1");
reviews = JsonSerializer.Deserialize<Root>(json);
}
else
Root reviews = null;

while (true)
{
var json = await _client.GetStringAsync($"https://api.jikan.moe/v4/manga/{_targetId}/reviews?page=1");
reviews = JsonSerializer.Deserialize<Root>(json);
if (_anime)
{
try
{
var json = await _client.GetStringAsync(
$"https://api.jikan.moe/v4/anime/{_targetId}/reviews?page=1");
reviews = JsonSerializer.Deserialize<Root>(json);
break;
}
catch (HttpRequestException e)
{
if (e.Message.Contains("429"))
await Task.Delay(TimeSpan.FromSeconds(1));
else
throw;
}

}
else
{
try
{
var json = await _client.GetStringAsync(
$"https://api.jikan.moe/v4/manga/{_targetId}/reviews?page=1");
reviews = JsonSerializer.Deserialize<Root>(json);
break;
}
catch (HttpRequestException e)
{
if (e.Message.Contains("429"))
await Task.Delay(TimeSpan.FromSeconds(1));
else
throw;
}
}
}

foreach (var review in reviews.Data)
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Anime/AnimeSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<List<AnimeGeneralDetailsData>> GetSearchResults()

try
{
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
var searchResult = await jikan.SearchAnimeAsync(_query);

foreach (var result in searchResult.Data)
Expand Down
48 changes: 30 additions & 18 deletions MALClient.XShared/Comm/Anime/AnimeSeasonalQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,40 @@ public async Task<List<SeasonalAnimeData>> GetSeasonalAnime(bool force = false)
}
};

var season = JsonSerializer.Deserialize<PaginatedJikanResponse<ICollection<JikanDotNet.Anime>>>(
await client.GetStringAsync(
$"https://api.jikan.moe/v4/seasons/{requestedYear}/{requestedSeason}?page={currentPage}"));

foreach (var seasonSeasonEntry in season.Data)
try
{
output.Add(new SeasonalAnimeData
var season = JsonSerializer.Deserialize<PaginatedJikanResponse<ICollection<JikanDotNet.Anime>>>(
await client.GetStringAsync(
$"https://api.jikan.moe/v4/seasons/{requestedYear}/{requestedSeason}?page={currentPage}"));

foreach (var seasonSeasonEntry in season.Data)
{
Title = seasonSeasonEntry.Title,
Id = (int)(seasonSeasonEntry.MalId ?? -1),
ImgUrl = seasonSeasonEntry.Images.JPG.ImageUrl,
Episodes = (seasonSeasonEntry.Episodes ?? 0).ToString(),
Score = (float)(seasonSeasonEntry.Score ?? 0),
Genres = seasonSeasonEntry.Genres.Select(item => item.Name).ToList(),
Index = season.Data.FindIndex(seasonSeasonEntry) + (25 * (currentPage - 1)) + 1
});
}
output.Add(new SeasonalAnimeData
{
Title = seasonSeasonEntry.Title,
Id = (int)(seasonSeasonEntry.MalId ?? -1),
ImgUrl = seasonSeasonEntry.Images.JPG.ImageUrl,
Episodes = (seasonSeasonEntry.Episodes ?? 0).ToString(),
Score = (float)(seasonSeasonEntry.Score ?? 0),
Genres = seasonSeasonEntry.Genres.Select(item => item.Name).ToList(),
Index = season.Data.FindIndex(seasonSeasonEntry) + (25 * (currentPage - 1)) + 1
});
}

if(!season.Pagination.HasNextPage)
break;
if (!season.Pagination.HasNextPage)
break;

currentPage++;
await Task.Delay(TimeSpan.FromMilliseconds(500));

currentPage++;
}
catch (HttpRequestException e)
{
if (e.Message.Contains("429"))
await Task.Delay(TimeSpan.FromSeconds(1));
else
throw;
}
}

DataCache.SaveSeasonalData(output, _season.Name);
Expand Down
19 changes: 19 additions & 0 deletions MALClient.XShared/Comm/JikanClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using JikanDotNet;
using JikanDotNet.Config;

namespace MALClient.XShared.Comm;
public static class JikanClient
{
public static readonly Jikan Jikan = new Jikan(new JikanClientConfiguration
{
SuppressException = true,
LimiterConfigurations = new List<TaskLimiterConfiguration>
{
new TaskLimiterConfiguration(3, TimeSpan.FromSeconds(1)),
new TaskLimiterConfiguration(60, TimeSpan.FromSeconds(60))
}
});
}
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Manga/MangaSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<List<AnimeGeneralDetailsData>> GetSearchResults()

try
{
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
var searchResult = await jikan.SearchMangaAsync(_query);

foreach (var result in searchResult.Data)
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Profile/FriendsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<List<MalFriend>> GetFriends()

try
{
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
var result = await jikan.GetUserFriendsAsync(_userName);

foreach (var friend in result.Data)
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/Comm/Profile/ProfileQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var recentNode in

try
{
var jikan = new Jikan();
var jikan = JikanClient.Jikan;
var favs = await jikan.GetUserFavoritesAsync(_userName);
foreach (var favAnime in favs.Data.Anime)
{
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/MALClient.XShared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.28" />
<PackageReference Include="JikanDotNet" Version="2.6.0" />
<PackageReference Include="JikanDotNet" Version="2.6.1" />
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/ViewModels/AnimeItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public ICommand CopyLinkToClipboardCommand
if (Settings.SelectedApiType == ApiType.Mal)
{
ResourceLocator.ClipboardProvider.SetText(
$"http://www.myanimelist.net/{(ParentAbstraction.RepresentsAnime ? "anime" : "manga")}/{Id}");
$"https://myanimelist.net/{(ParentAbstraction.RepresentsAnime ? "anime" : "manga")}/{Id}");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public ICommand CopyToClipboardCommand
{
if (Settings.SelectedApiType == ApiType.Mal)
{
_clipboardProvider.SetText($"http://www.myanimelist.net/{(AnimeMode ? "anime" : "manga")}/{Id}");
_clipboardProvider.SetText($"https://myanimelist.net/{(AnimeMode ? "anime" : "manga")}/{Id}");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public string Created
public ICommand CopyAndWaifuCommand => _copyAndWaifuCommand ?? (_copyAndWaifuCommand = new RelayCommand(() =>
{
ResourceLocator.ClipboardProvider.SetText(Data.FileUrl);
ResourceLocator.SystemControlsLauncherService.LaunchUri(new Uri("http://waifu2x.booru.pics/"));
ResourceLocator.SystemControlsLauncherService.LaunchUri(new Uri("https://waifu2x.booru.pics/"));
}));

}
Expand Down
2 changes: 1 addition & 1 deletion MALClient.XShared/ViewModels/Main/AnimeListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ await Task.Run(async () =>
SeasonSelection.Clear();
var i = 0;
var currSeasonIndex = -1;
var seasons = await new Jikan().GetSeasonArchiveAsync();
var seasons = await JikanClient.Jikan.GetSeasonArchiveAsync();

foreach (var season in seasons.Data.Take(3))
{
Expand Down

0 comments on commit d3c2016

Please sign in to comment.