forked from jellyfin-archive/jellyfin-plugin-anime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAniDbTitleDownloader.cs
131 lines (115 loc) · 4.67 KB
/
AniDbTitleDownloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Anime.Providers.AniDB.Metadata;
using MediaBrowser.Common.Configuration;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Anime.Providers.AniDB.Identity
{
/// <summary>
/// The AniDbTitleDownloader class downloads the anime titles file from AniDB and stores it.
/// </summary>
public class AniDbTitleDownloader : IAniDbTitleDownloader
{
/// <summary>
/// The URL for retrieving a list of all anime titles and their AniDB IDs.
/// </summary>
private const string TitlesUrl = "https://anidb.net/api/anime-titles.xml.gz";
private static readonly HttpClient _httpClient;
private readonly ILogger _logger;
public AniDbTitleDownloader(ILogger logger, IApplicationPaths applicationPaths)
{
_logger = logger;
Paths = GetDataPath(applicationPaths);
}
static AniDbTitleDownloader()
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", Constants.UserAgent);
}
public static string Paths { get; private set; }
/// <summary>
/// Gets the path to the anidb data folder.
/// </summary>
/// <param name="applicationPaths">The application paths.</param>
/// <returns>The path to the anidb data folder.</returns>
public static string GetDataPath(IApplicationPaths applicationPaths)
{
return Path.Combine(applicationPaths.CachePath, "anidb");
}
/// <summary>
/// Load XML static| Too prevent EXCEPTIONS
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task Load_static(CancellationToken cancellationToken)
{
var titlesFile = TitlesFilePath_;
var titlesFileInfo = new FileInfo(titlesFile);
// download titles if we do not already have them, or have not updated for a week
if (!titlesFileInfo.Exists || (DateTime.UtcNow - titlesFileInfo.LastWriteTimeUtc).TotalDays > 7)
{
await DownloadTitles_static(titlesFile).ConfigureAwait(false);
}
}
public async Task Load(CancellationToken cancellationToken)
{
var titlesFile = TitlesFilePath;
var titlesFileInfo = new FileInfo(titlesFile);
// download titles if we do not already have them, or have not updated for a week
if (!titlesFileInfo.Exists || (DateTime.UtcNow - titlesFileInfo.LastWriteTimeUtc).TotalDays > 7)
{
await DownloadTitles(titlesFile).ConfigureAwait(false);
}
}
/// <summary>
/// Downloads an xml file from AniDB which contains all of the titles for every anime, and their IDs,
/// and saves it to disk.
/// </summary>
/// <param name="titlesFile">The destination file name.</param>
private Task DownloadTitles(string titlesFile)
{
_logger.LogDebug("Downloading new AniDB titles file.");
return DownloadTitles_static(titlesFile);
}
/// <summary>
/// static|Downloads an xml file from AniDB which contains all of the titles for every anime, and their IDs,
/// and saves it to disk.
/// </summary>
/// <param name="titlesFile"></param>
/// <returns></returns>
private static async Task DownloadTitles_static(string titlesFile)
{
await AniDbSeriesProvider.RequestLimiter.Tick().ConfigureAwait(false);
await Task.Delay(Plugin.Instance.Configuration.AniDbRateLimit).ConfigureAwait(false);
using (var stream = await _httpClient.GetStreamAsync(TitlesUrl).ConfigureAwait(false))
using (var unzipped = new GZipStream(stream, CompressionMode.Decompress))
using (var writer = File.Open(titlesFile, FileMode.Create, FileAccess.Write))
{
await unzipped.CopyToAsync(writer).ConfigureAwait(false);
}
}
public string TitlesFilePath
{
get
{
Directory.CreateDirectory(Paths);
return Path.Combine(Paths, "titles.xml");
}
}
/// <summary>
/// Get the FilePath
/// </summary>
public static string TitlesFilePath_
{
get
{
Directory.CreateDirectory(Paths);
return Path.Combine(Paths, "titles.xml");
}
}
}
}