Skip to content

Commit

Permalink
anime id offline mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Dec 4, 2023
1 parent 922a5b5 commit b7583fc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/IAnimeIdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IAnimeIdService
{
Task<AnimeIdExtended> GetId(ListServiceType serviceType, long id);
Task<AnimeIdExtended> GetId(long id);
Task UpdateOfflineMappings();
}
93 changes: 87 additions & 6 deletions Totoro.Core/Services/AnimeIdService.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,107 @@
using Totoro.Core.Services.Simkl;
using System.Text.Json.Nodes;
using Flurl.Http;
using Totoro.Core.Services.Simkl;

namespace Totoro.Core.Services;

public class AnimeIdService : IAnimeIdService
{
private readonly ISettings _settings;
private readonly ISimklService _simklService;
private readonly IKnownFolders _knownFolders;
private readonly IFileService _fileService;
private readonly string _dbUrl = @"https://raw.githubusercontent.com/Fribb/anime-lists/master/anime-offline-database-reduced.json";
private readonly string _fileName = @"ids.json";
private readonly List<AnimeIdExtended> _ids;
private bool _isUpdating;

public AnimeIdService(ISettings settings,
ISimklService simklService)
ISimklService simklService,
IKnownFolders knownFolders,
IFileService fileService)
{
_settings = settings;
_simklService = simklService;
_knownFolders = knownFolders;
_fileService = fileService;

_ids = fileService.Read<List<AnimeIdExtended>>(knownFolders.ApplicationData, _fileName) ?? new();
}

public async Task<AnimeIdExtended> GetId(ListServiceType serviceType, long id)
{
if(_isUpdating || _ids.FirstOrDefault(GetSelector(serviceType, id)) is not { } idExtended)
{
return await _simklService.GetId(serviceType, id);
}

return idExtended;
}

public Task<AnimeIdExtended> GetId(ListServiceType serviceType, long id)
private static Func<AnimeIdExtended, bool> GetSelector(ListServiceType type, long serviceId)
{
return _simklService.GetId(serviceType, id);
return type switch
{
ListServiceType.AniList => (AnimeIdExtended id) => id.AniList == serviceId,
ListServiceType.MyAnimeList => (AnimeIdExtended id) => id.MyAnimeList == serviceId,
ListServiceType.Kitsu => (AnimeIdExtended id) => id.Kitsu == serviceId,
ListServiceType.AniDb => (AnimeIdExtended id) => id.AniDb == serviceId,
_ => (AnimeIdExtended id) => false,
};
}

public Task<AnimeIdExtended> GetId(long id)
public Task<AnimeIdExtended> GetId(long id) => GetId(_settings.DefaultListService, id);

public async Task UpdateOfflineMappings()
{
return _simklService.GetId(_settings.DefaultListService, id);
var stream = await _dbUrl.GetStreamAsync();
var array = JsonNode.Parse(stream).AsArray();
var keys = new[]
{
"livechart_id",
"anidb_id",
"kitsu_id",
"mal_id",
"anilist_id"
};

_isUpdating = true;
_ids.Clear();

foreach (var item in array)
{
var id = new AnimeIdExtended();
var obj = item.AsObject();

foreach (var key in keys)
{
if(obj.ContainsKey(key))
{
var value = obj[key].GetValue<long>();
switch (key)
{
case "livechart_id":
id.LiveChart = value;
break;
case "anidb_id":
id.AniDb = value;
break;
case "kitsu_id":
id.Kitsu = value;
break;
case "mal_id":
id.MyAnimeList = value;
break;
case "anilist_id":
id.AniList = value;
break;
}
}
}

_ids.Add(id);
}
_isUpdating = false;
_fileService.Save(_knownFolders.ApplicationData, _fileName, _ids);
}
}
7 changes: 6 additions & 1 deletion Totoro.Core/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace Totoro.Core.Services;

public class FileService : IFileService
{
private readonly JsonSerializerSettings _settings = new()
{
DefaultValueHandling = DefaultValueHandling.Ignore
};

public T Read<T>(string folderPath, string fileName)
{
var path = Path.Combine(folderPath, fileName);
Expand All @@ -24,7 +29,7 @@ public void Save<T>(string folderPath, string fileName, T content)
Directory.CreateDirectory(folderPath);
}

var fileContent = JsonConvert.SerializeObject(content);
var fileContent = JsonConvert.SerializeObject(content, Formatting.Indented, _settings);
File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8);
}

Expand Down
10 changes: 10 additions & 0 deletions Totoro.WinUI/Views/SettingsSections/AnimePluginsSection.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
OnContent="On" />
</labs:SettingsCard>

<labs:SettingsCard Description="This repository is to store and provide the mapping between various anime sources, (https://github.com/Fribb/anime-lists)" Header="Offline Anime DB">
<labs:SettingsCard.HeaderIcon>
<FontIcon Glyph="&#xE78C;" />
</labs:SettingsCard.HeaderIcon>
<Button
Command="{x:Bind UpdateOfflineDb}"
Content="Update"
Style="{ThemeResource AccentButtonStyle}" />
</labs:SettingsCard>

<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public SettingsViewModel ViewModel
DependencyProperty.Register("ViewModel", typeof(SettingsViewModel), typeof(AnimePluginsSection), new PropertyMetadata(null));


public ICommand UpdateOfflineDb { get; }

public AnimePluginsSection()
{
InitializeComponent();

UpdateOfflineDb = ReactiveCommand.CreateFromTask(() => App.GetService<IAnimeIdService>().UpdateOfflineMappings());
}

protected override void OnNavigatedTo(NavigationEventArgs e)
Expand Down

0 comments on commit b7583fc

Please sign in to comment.