forked from vosmiic/jellyfin-ani-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateProviderStatus.cs
751 lines (680 loc) · 46.8 KB
/
UpdateProviderStatus.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using jellyfin_ani_sync.Api;
using jellyfin_ani_sync.Api.Anilist;
using jellyfin_ani_sync.Api.Annict;
using jellyfin_ani_sync.Api.Kitsu;
using jellyfin_ani_sync.Api.Shikimori;
using jellyfin_ani_sync.Api.Simkl;
using jellyfin_ani_sync.Configuration;
using jellyfin_ani_sync.Helpers;
using jellyfin_ani_sync.Models;
using jellyfin_ani_sync.Models.Mal;
using Jellyfin.Data.Entities;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace jellyfin_ani_sync {
public class UpdateProviderStatus {
private readonly IHttpClientFactory _httpClientFactory;
private readonly IApplicationPaths _applicationPaths;
private readonly IServerApplicationHost _serverApplicationHost;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<UpdateProviderStatus> _logger;
private ApiCallHelpers _apiCallHelpers;
private UserConfig? _userConfig;
private Type _animeType;
private readonly ILibraryManager _libraryManager;
private readonly IFileSystem _fileSystem;
private ApiName _apiName;
private readonly ILoggerFactory _loggerFactory;
private AnimeOfflineDatabaseHelpers.OfflineDatabaseResponse _apiIds = new ();
public UpdateProviderStatus(IFileSystem fileSystem,
ILibraryManager libraryManager,
ILoggerFactory loggerFactory,
IHttpContextAccessor httpContextAccessor,
IServerApplicationHost serverApplicationHost,
IHttpClientFactory httpClientFactory,
IApplicationPaths applicationPaths) {
_fileSystem = fileSystem;
_libraryManager = libraryManager;
_httpContextAccessor = httpContextAccessor;
_serverApplicationHost = serverApplicationHost;
_httpClientFactory = httpClientFactory;
_applicationPaths = applicationPaths;
_logger = loggerFactory.CreateLogger<UpdateProviderStatus>();
_loggerFactory = loggerFactory;
}
public async Task Update(BaseItem e, Guid userId, bool playedToCompletion) {
var video = e as Video;
Episode episode = video as Episode;
Movie movie = video as Movie;
if (video is Episode) {
_animeType = typeof(Episode);
} else if (video is Movie) {
_animeType = typeof(Movie);
video.IndexNumber = 1;
}
_userConfig = Plugin.Instance?.PluginConfiguration.UserConfig.FirstOrDefault(item => item.UserId == userId);
if (_userConfig == null) {
_logger.LogWarning($"The user {userId} does not exist in the plugins config file. Skipping");
return;
}
if (_userConfig.UserApiAuth == null) {
_logger.LogWarning($"The user {userId} is not authenticated. Skipping");
return;
}
if (LibraryCheck(_userConfig, _libraryManager, _fileSystem, _logger, e) && video is Episode or Movie && playedToCompletion) {
if ((video is Episode && (episode.IndexNumber == null)) ||
(video is Movie && movie.IndexNumber == null)) {
_logger.LogError("Video does not contain required index numbers to sync; skipping");
return;
}
int seasonNumber = 1;
if (_animeType == typeof(Episode)) {
string? aniListSeasonId = episode.Season.Studios.FirstOrDefault(s => s.StartsWith("anilist", StringComparison.OrdinalIgnoreCase));
if (aniListSeasonId != null) {
_logger.LogInformation("Season ID for AniList found.");
string id = aniListSeasonId.Substring(8);
// Parse the numeric part to an integer
if (int.TryParse(id, out int result)) {
_apiIds.Anilist = result;
} else {
_logger.LogWarning("Invalid form for: " + aniListSeasonId);
}
}
} else {
if (int.TryParse(movie.ProviderIds["AniList"], out int result)) {
_apiIds.Anilist = result;
} else {
_logger.LogWarning("Unable to retrieve anilistId for movie.");
}
}
(int? aniDbId, int? episodeOffset) aniDbId = (null, null);
foreach (UserApiAuth userApiAuth in _userConfig.UserApiAuth) {
_apiName = userApiAuth.Name;
_logger.LogInformation($"Using provider {userApiAuth.Name}...");
switch (userApiAuth.Name) {
case ApiName.Mal:
_apiCallHelpers = new ApiCallHelpers(malApiCalls: new MalApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, _userConfig));
if (_apiIds.MyAnimeList != null && _apiIds.MyAnimeList != 0 && (episode != null)) {
await CheckUserListAnimeStatus(_apiIds.MyAnimeList.Value, _animeType == typeof(Episode)
? (aniDbId.episodeOffset != null
? episode.IndexNumber.Value - aniDbId.episodeOffset.Value
: episode.IndexNumber.Value)
: movie.IndexNumber.Value,
false);
continue;
}
break;
case ApiName.AniList:
_apiCallHelpers = new ApiCallHelpers(aniListApiCalls: new AniListApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, _userConfig));
if (_apiIds.Anilist != null && _apiIds.Anilist != 0 && (episode != null)) {
await CheckUserListAnimeStatus(_apiIds.Anilist.Value, _animeType == typeof(Episode)
? (aniDbId.episodeOffset != null
? episode.IndexNumber.Value - aniDbId.episodeOffset.Value
: episode.IndexNumber.Value)
: movie.IndexNumber.Value,
false);
continue;
} else if (_animeType == typeof(Episode) ? episode.Series.ProviderIds.ContainsKey("AniList"): movie.ProviderIds.ContainsKey("AniList")) {
if (_animeType == typeof(Episode) ? int.TryParse(episode.Series.ProviderIds["AniList"], out int aniListId) : int.TryParse(movie.ProviderIds["AniList"], out aniListId)) {
await CheckUserListAnimeStatus(aniListId, _animeType == typeof(Episode)
? (aniDbId.episodeOffset != null
? episode.IndexNumber.Value - aniDbId.episodeOffset.Value
: episode.IndexNumber.Value)
: movie.IndexNumber.Value,
false);
continue;
}
}
break;
case ApiName.Kitsu:
_apiCallHelpers = new ApiCallHelpers(kitsuApiCalls: new KitsuApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, _userConfig));
if (_apiIds.Kitsu != null && _apiIds.Kitsu != 0 && (episode != null)) {
await CheckUserListAnimeStatus(_apiIds.Kitsu.Value, _animeType == typeof(Episode)
? (aniDbId.episodeOffset != null
? episode.IndexNumber.Value - aniDbId.episodeOffset.Value
: episode.IndexNumber.Value)
: movie.IndexNumber.Value,
false);
continue;
}
break;
case ApiName.Annict:
// annict works differently to the other providers, so we have to go the traditional route
_apiCallHelpers = new ApiCallHelpers(annictApiCalls: new AnnictApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, _userConfig));
break;
case ApiName.Shikimori:
string? shikimoriAppName = ConfigHelper.GetShikimoriAppName(_logger);
if (shikimoriAppName == null) return;
_apiCallHelpers = new ApiCallHelpers(shikimoriApiCalls: new ShikimoriApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, new Dictionary<string, string> { { "User-Agent", shikimoriAppName } }, _userConfig));
break;
case ApiName.Simkl:
string? simklClientId = ConfigHelper.GetSimklClientId(_logger);
if (simklClientId == null) return;
_apiCallHelpers = new ApiCallHelpers(simklApiCalls: new SimklApiCalls(_httpClientFactory, _loggerFactory, _serverApplicationHost, _httpContextAccessor, new Dictionary<string, string> { { "simkl-api-key", simklClientId } }, _userConfig));
if (((_apiIds.MyAnimeList != null && _apiIds.MyAnimeList != 0) ||
(_apiIds.Anilist != null && _apiIds.Anilist != 0) ||
(_apiIds.Kitsu != null && _apiIds.Kitsu != 0) ||
(_apiIds.AniDb != null && _apiIds.AniDb != 0)) &&
(episode != null)) {
await CheckUserListAnimeStatus(_apiIds, _animeType == typeof(Episode)
? (aniDbId.episodeOffset != null
? episode.IndexNumber.Value - aniDbId.episodeOffset.Value
: episode.IndexNumber.Value)
: movie.IndexNumber.Value, _animeType == typeof(Episode) ? episode.SeriesName : video.Name,
false);
continue;
}
break;
}
var animeType = _animeType == typeof(Episode) ? "series" : "movie";
var searchTitle = _animeType == typeof(Episode) ? episode.SeriesName : video.Name;
_logger.LogInformation($"({_apiName}) Searching for {animeType}: {searchTitle}");
List<Anime> animeList = await _apiCallHelpers.SearchAnime(_animeType == typeof(Episode) ? episode.SeriesName : video.Name);
bool found = false;
if (animeList != null) {
foreach (var anime in animeList) {
var checkMalId = _apiName == ApiName.Annict ||
_apiName == ApiName.Shikimori && _apiIds.MyAnimeList != null && anime.Id > 0;
if (checkMalId && _apiIds.MyAnimeList != null && anime.Id == _apiIds.MyAnimeList ||
!checkMalId && TitleCheck(anime, episode, movie)) {
_logger.LogInformation($"({_apiName}) Found matching {animeType}: {GetAnimeTitle(anime)}");
Anime matchingAnime = anime;
if (_animeType == typeof(Episode)) {
int episodeNumber = episode.IndexNumber.Value;
if (!checkMalId) {
// should have already found the appropriate series/season/movie, no need to do other checks
if (seasonNumber is > 1) {
// if this is not the first season, then we need to lookup the related season.
matchingAnime = await GetDifferentSeasonAnime(anime.Id, episode.Season.IndexNumber.Value, alternativeId: anime.AlternativeId);
matchingAnime = await GetDifferentSeasonAnime(anime.Id, seasonNumber);
if (matchingAnime == null) {
_logger.LogWarning($"({_apiName}) Could not find next season");
found = true;
break;
}
_logger.LogInformation($"({_apiName}) Season being watched is {GetAnimeTitle(matchingAnime)}");
} else if (seasonNumber == 0) {
// the episode is an ova or special
matchingAnime = await GetOva(anime.Id, episode.Name, alternativeId: anime.AlternativeId);
if (matchingAnime == null) {
_logger.LogWarning($"({_apiName}) Could not find OVA");
found = true;
break;
}
} else if (matchingAnime.NumEpisodes < episode?.IndexNumber.Value) {
_logger.LogInformation($"({_apiName}) Watched episode passes total episodes in season! Checking for additional seasons/cours...");
// either we have found the wrong series (highly unlikely) or it is a multi cour series/Jellyfin has grouped next season into the current.
int seasonEpisodeCounter = matchingAnime.NumEpisodes;
int totalEpisodesWatched = 0;
int seasonCounter = seasonNumber;
int episodeCount = episode.IndexNumber.Value;
Anime season = matchingAnime;
bool isRootSeason = false;
while (seasonEpisodeCounter < episodeCount) {
var nextSeason = await GetDifferentSeasonAnime(season.Id, seasonCounter + 1, alternativeId: season.AlternativeId);
if (nextSeason == null) {
_logger.LogWarning($"({_apiName}) Could not find next season");
if (matchingAnime.Status == AiringStatus.currently_airing && matchingAnime.NumEpisodes == 0) {
_logger.LogWarning($"({_apiName}) Show is currently airing and API reports 0 episodes, going to use first season");
isRootSeason = true;
}
found = true;
break;
}
seasonEpisodeCounter += nextSeason.NumEpisodes;
seasonCounter++;
// complete the current season; we have surpassed it onto the next season/cour
totalEpisodesWatched += season.NumEpisodes;
await CheckUserListAnimeStatus(season.Id, season.NumEpisodes, overrideCheckRewatch: false, alternativeId: matchingAnime.AlternativeId);
season = nextSeason;
}
if (!isRootSeason) {
if (season.Id != matchingAnime.Id) {
matchingAnime = season;
episodeNumber = episodeCount - totalEpisodesWatched;
} else {
break;
}
}
}
}
await CheckUserListAnimeStatus(matchingAnime.Id, episodeNumber, alternativeId: matchingAnime.AlternativeId);
found = true;
break;
}
if (_animeType == typeof(Movie)) {
await CheckUserListAnimeStatus(matchingAnime.Id, movie.IndexNumber.Value, alternativeId: matchingAnime.AlternativeId);
found = true;
break;
}
}
}
}
if (!found) {
_logger.LogWarning($"({_apiName}) Series not found");
}
}
}
}
/// <summary>
/// Gets anime's title.
/// </summary>
/// <param name="anime">The API anime</param>
/// <returns>
/// <see cref="Anime.Title"/> if it isn't empty.<br/>
/// If it is, then the first <see cref="AlternativeTitles.Synonyms">synonym</see>.<br/>
/// If there isn't any, then the <see cref="AlternativeTitles.Ja">japanese title</see>.
/// </returns>
private static string GetAnimeTitle(Anime anime) {
var title = string.IsNullOrWhiteSpace(anime.Title)
? anime.AlternativeTitles.Synonyms.Count > 0
? anime.AlternativeTitles.Synonyms[0]
: anime.AlternativeTitles.Ja
: anime.Title;
return title;
}
/// <summary>
/// Checks if the Jellyfin library entry matches the API names.
/// </summary>
/// <param name="anime">The API anime.</param>
/// <param name="episode">The episode if its a series.</param>
/// <param name="movie">The movie if its a single episode movie.</param>
/// <returns></returns>
private bool TitleCheck(Anime anime, Episode episode, Movie movie) {
var title = _animeType == typeof(Episode) ? episode.SeriesName : movie.Name;
return CompareStrings(anime.Title, title) ||
(anime.AlternativeTitles.En != null && CompareStrings(anime.AlternativeTitles.En, title)) ||
(anime.AlternativeTitles.Ja != null && CompareStrings(anime.AlternativeTitles.Ja, title)) ||
(anime.AlternativeTitles.Synonyms != null && anime.AlternativeTitles.Synonyms.Any(synonym => CompareStrings(synonym, title)));
}
/// <summary>
/// Compare two strings, ignoring symbols and case.
/// </summary>
/// <param name="first">The first string.</param>
/// <param name="second">The second string.</param>
/// <returns>True if first string is equal to second string, false if not.</returns>
private bool CompareStrings(string first, string second) {
return String.Compare(first, second, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0;
}
/// <summary>
/// Check if a string exists in another, ignoring symbols and case.
/// </summary>
/// <param name="first">The first string.</param>
/// <param name="second">The second string.</param>
/// <returns>True if first string contains second string, false if not.</returns>
private bool ContainsExtended(string first, string second) {
return StringFormatter.RemoveSpecialCharacters(first).Contains(StringFormatter.RemoveSpecialCharacters(second), StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Checks if the supplied item is in a folder the user wants to track for anime updates.
/// </summary>
/// <param name="userConfig">User config.</param>
/// <param name="libraryManager">Library manager instance.</param>
/// <param name="fileSystem">File system instance.</param>
/// <param name="logger">Logger instance.</param>
/// <param name="item">Item to check location of.</param>
/// <returns></returns>
public static bool LibraryCheck(UserConfig userConfig, ILibraryManager libraryManager, IFileSystem fileSystem, ILogger logger, BaseItem item) {
try {
// user has no library filters
if (userConfig.LibraryToCheck is { Length: 0 }) {
return true;
}
// item is in a path of a folder the user wants to be monitored
var topParent = item.GetTopParent();
if (topParent is not null) {
var allLocations = libraryManager.GetVirtualFolders()
.Where(item => userConfig.LibraryToCheck.Contains(item.ItemId))
.SelectMany(f => f.Locations)
.ToHashSet();
if (allLocations.Contains(topParent.Path)) {
return true;
}
}
logger.LogInformation("Item is in a folder the user does not want to be monitored; ignoring");
return false;
} catch (Exception e) {
logger.LogInformation($"Library check ran into an issue: {e.Message}");
return false;
}
}
private async Task CheckUserListAnimeStatus(AnimeOfflineDatabaseHelpers.OfflineDatabaseResponse matchingIds, int episodeNumber, string title, bool? overrideCheckRewatch = null, string? alternativeId = null) {
Anime detectedAnime = await GetAnime(matchingIds, title, alternativeId: alternativeId);
await CheckUserListAnimeStatusBase(detectedAnime, episodeNumber, overrideCheckRewatch, alternativeId);
}
private async Task CheckUserListAnimeStatus(int matchingAnimeId, int episodeNumber, bool? overrideCheckRewatch = null, string? alternativeId = null) {
Anime detectedAnime = await GetAnime(matchingAnimeId, alternativeId: alternativeId);
await CheckUserListAnimeStatusBase(detectedAnime, episodeNumber, overrideCheckRewatch, alternativeId);
}
private async Task CheckUserListAnimeStatusBase(Anime detectedAnime, int episodeNumber, bool? overrideCheckRewatch = null, string? alternativeId = null) {
if (detectedAnime == null) return;
if (detectedAnime.MyListStatus != null && detectedAnime.MyListStatus.Status == Status.Watching && _apiName != ApiName.Annict) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on watching list");
await UpdateAnimeStatus(detectedAnime, episodeNumber);
return;
}
// only plan to watch
if (_userConfig.PlanToWatchOnly) {
bool updated = false;
if (detectedAnime.MyListStatus != null && detectedAnime.MyListStatus.Status == Status.Plan_to_watch) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on plan to watch list");
if (_apiName == ApiName.Annict) {
updated = true;
await UpdateAnnictStatus(detectedAnime, episodeNumber);
} else {
updated = true;
await UpdateAnimeStatus(detectedAnime, episodeNumber);
}
}
if (_apiName != ApiName.Annict) {
// also check if rewatch completed is checked
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) not found in plan to watch list{(_userConfig.RewatchCompleted ? ", checking completed list.." : null)}");
updated = true;
await CheckIfRewatchCompleted(detectedAnime, episodeNumber, overrideCheckRewatch);
}
if (!updated) {
_logger.LogInformation($"({_apiName}) Could not update.");
}
return;
}
_logger.LogInformation("User does not have plan to watch only ticked");
if (_apiName != ApiName.Annict)
// check if rewatch completed is checked
await CheckIfRewatchCompleted(detectedAnime, episodeNumber, overrideCheckRewatch);
// everything else
if (detectedAnime.MyListStatus != null) {
// anime is on user list
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on {detectedAnime.MyListStatus.Status} list");
if (detectedAnime.MyListStatus.Status == Status.Completed) {
if (_apiName != ApiName.Annict)
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on Completed list, but user does not want to automatically set as rewatching. Skipping");
return;
}
} else {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) not on user list");
}
if (_apiName == ApiName.Annict) {
await UpdateAnnictStatus(detectedAnime, episodeNumber);
} else {
await UpdateAnimeStatus(detectedAnime, episodeNumber);
}
}
private async Task UpdateAnnictStatus(Anime detectedAnime, int episodeNumber) {
// rewatching isnt supported by annict; skip
if (detectedAnime.MyListStatus?.Status == Status.Completed) return;
if (_userConfig.PlanToWatchOnly && detectedAnime.MyListStatus is null) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found, but not on plan to watch list");
return;
}
if (detectedAnime.NumEpisodes == episodeNumber) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) complete, marking anime as complete");
await _apiCallHelpers.UpdateAnime(detectedAnime.Id, 1, Status.Completed, alternativeId: detectedAnime.AlternativeId, ids: _apiIds);
return;
}
if (detectedAnime.NumEpisodes > episodeNumber && detectedAnime.MyListStatus?.Status != Status.Watching) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) being marked as watching");
await _apiCallHelpers.UpdateAnime(detectedAnime.Id, 1, Status.Watching, alternativeId: detectedAnime.AlternativeId, ids: _apiIds);
} else {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) already set to watching, not updating again");
}
}
private async Task CheckIfRewatchCompleted(Anime detectedAnime, int indexNumber, bool? overrideCheckRewatch) {
if (overrideCheckRewatch == null ||
overrideCheckRewatch.Value ||
detectedAnime.MyListStatus is { Status: Status.Completed } ||
detectedAnime.MyListStatus is { Status: Status.Rewatching } && detectedAnime.MyListStatus.NumEpisodesWatched < indexNumber) {
if (_apiName == ApiName.Simkl) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on completed list, but {_apiName} does not support re-watching. Skipping");
return;
}
if (_userConfig.RewatchCompleted) {
if (detectedAnime.MyListStatus != null && detectedAnime.MyListStatus.Status == Status.Completed) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on completed list, setting as re-watching");
await UpdateAnimeStatus(detectedAnime, indexNumber, true, detectedAnime.MyListStatus.RewatchCount, true);
}
} else {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found on Completed list, but user does not want to automatically set as rewatching. Skipping");
}
} else if (detectedAnime.MyListStatus != null && detectedAnime.MyListStatus.NumEpisodesWatched >= indexNumber) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found, but provider reports episode already watched. Skipping");
} else if (_userConfig.PlanToWatchOnly) {
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) found, but not on completed or plan to watch list. Skipping");
}
}
/// <summary>
/// Get a single result from a user anime search.
/// </summary>
/// <param name="animeId">ID of the anime you want to get.</param>
/// <param name="status">User status of the show.</param>
/// <returns>Single anime result.</returns>
private async Task<Anime> GetAnime(int animeId, Status? status = null, string? alternativeId = null) {
Anime anime = await _apiCallHelpers.GetAnime(animeId, alternativeId: alternativeId);
if (anime != null && ((status != null && anime.MyListStatus != null && anime.MyListStatus.Status == status) || status == null)) {
return anime;
}
return null;
}
private async Task<Anime> GetAnime(AnimeOfflineDatabaseHelpers.OfflineDatabaseResponse animeIds, string title, Status? status = null, string? alternativeId = null) {
Anime anime = await _apiCallHelpers.GetAnime(animeIds, title);
if (anime != null && ((status != null && anime.MyListStatus != null && anime.MyListStatus.Status == status) || status == null)) {
return anime;
}
return null;
}
/// <summary>
/// Update a users anime status.
/// </summary>
/// <param name="detectedAnime">The anime search result to update.</param>
/// <param name="episodeNumber">The episode number to update the anime to.</param>
/// <param name="setRewatching">Whether to set the show as being re-watched or not.</param>
private async Task UpdateAnimeStatus(Anime detectedAnime, int? episodeNumber, bool? setRewatching = null, int? rewatchCount = null, bool firstTimeRewatch = false) {
if (episodeNumber != null) {
UpdateAnimeStatusResponse response;
if (detectedAnime.MyListStatus != null) {
if (detectedAnime.MyListStatus.NumEpisodesWatched < episodeNumber.Value || detectedAnime.NumEpisodes == 1 ||
(setRewatching != null && setRewatching.Value && detectedAnime.MyListStatus.NumEpisodesWatched == episodeNumber.Value)) {
// covers the very rare occurence of re-watching the show and starting at the last episode
// movie or ova has only one episode, so just mark it as finished
if (episodeNumber.Value == detectedAnime.NumEpisodes || detectedAnime.NumEpisodes == 1) {
// either watched all episodes or the anime only has a single episode (ova)
if (detectedAnime.NumEpisodes == 1) {
// its a movie or ova since it only has one "episode", so the start and end date is the same
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
1,
Status.Completed,
startDate: detectedAnime.MyListStatus.IsRewatching || detectedAnime.MyListStatus.Status == Status.Completed ? null : DateTime.Now,
endDate: detectedAnime.MyListStatus.IsRewatching || detectedAnime.MyListStatus.Status == Status.Completed ? null : DateTime.Now,
isRewatching: false,
numberOfTimesRewatched: (setRewatching != null && setRewatching.Value) || detectedAnime.MyListStatus.IsRewatching ? detectedAnime.MyListStatus.RewatchCount + 1 : null,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
} else {
// user has reached the number of episodes in the anime, set as completed
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
endDate: detectedAnime.MyListStatus.IsRewatching || detectedAnime.MyListStatus.Status == Status.Completed ? null : DateTime.Now,
isRewatching: false,
numberOfTimesRewatched: (setRewatching != null && setRewatching.Value) || detectedAnime.MyListStatus.IsRewatching ? detectedAnime.MyListStatus.RewatchCount + 1 : null,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
}
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) complete, marking anime as complete{(_apiName != ApiName.Mal && (setRewatching != null && setRewatching.Value) ? ", increasing re-watch count by 1" : "")}");
if ((detectedAnime.MyListStatus.IsRewatching || (detectedAnime.NumEpisodes == 1 && detectedAnime.MyListStatus.Status == Status.Completed) || (setRewatching != null && setRewatching.Value)) && _apiName == ApiName.Mal) {
// also increase number of times re-watched by 1
// only way to get the number of times re-watched is by doing the update and capturing the response, and then re-updating for MAL :/
_logger.LogInformation($"({_apiName}) {(_animeType == typeof(Episode) ? "Series" : "Movie")} ({GetAnimeTitle(detectedAnime)}) has also been re-watched, increasing re-watch count by 1");
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
numberOfTimesRewatched: response.NumTimesRewatched + 1,
isRewatching: false,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
}
} else {
if (detectedAnime.MyListStatus.IsRewatching) {
// MAL likes to mark re-watching shows as completed, instead of watching. I guess technically both are correct
_logger.LogInformation($"({_apiName}) User is re-watching {(_animeType == typeof(Episode) ? "series" : "movie")} ({GetAnimeTitle(detectedAnime)}), set as completed but update re-watch progress");
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
isRewatching: true,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
} else {
if (episodeNumber > 1) {
// don't set start date after first episode
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Watching,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
} else {
_logger.LogInformation($"({_apiName}) Setting new {(_animeType == typeof(Episode) ? "series" : "movie")} ({GetAnimeTitle(detectedAnime)}) as watching.");
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Watching,
startDate: DateTime.Now,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
}
}
}
if (response != null) {
_logger.LogInformation($"({_apiName}) Updated {(_animeType == typeof(Episode) ? "series" : "movie")} ({GetAnimeTitle(detectedAnime)}) progress to {episodeNumber.Value}");
} else {
_logger.LogError($"({_apiName}) Could not update anime status");
}
} else {
if (setRewatching != null && setRewatching.Value) {
_logger.LogInformation($"({_apiName}) Series ({GetAnimeTitle(detectedAnime)}) has already been watched, marking anime as re-watching; progress of {episodeNumber.Value}");
if (_apiName == ApiName.Kitsu && firstTimeRewatch) {
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Rewatching,
true,
detectedAnime.MyListStatus.RewatchCount + 1,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
} else {
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
true,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
// anilist seems to (at the moment) not allow you to set the show as rewatching and the progress at the same time; going to have to do a separate call
if (_apiName == ApiName.AniList) {
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
true,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
}
}
} else {
response = null;
_logger.LogInformation($"({_apiName}) Provider reports episode already watched; not updating");
}
}
} else {
// status is not set, must be a new anime
if (episodeNumber.Value == detectedAnime.NumEpisodes) {
// anime completed all at once or user has watched last episode
_logger.LogInformation($"({_apiName}) Adding new {(_animeType == typeof(Episode) ? "series" : "movie")} ({GetAnimeTitle(detectedAnime)}) to user list as completed with a progress of {episodeNumber.Value}");
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Completed,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
} else {
// not on last episodes so must still be watching
_logger.LogInformation($"({_apiName}) Adding new {(_animeType == typeof(Episode) ? "series" : "movie")} ({GetAnimeTitle(detectedAnime)}) to user list as watching with a progress of {episodeNumber.Value}");
response = await _apiCallHelpers.UpdateAnime(detectedAnime.Id,
episodeNumber.Value,
Status.Watching,
alternativeId: detectedAnime.AlternativeId,
ids: _apiIds,
isShow: _animeType == typeof(Episode));
}
}
if (response == null) {
_logger.LogError($"({_apiName}) Could not update anime status");
}
}
}
/// <summary>
/// Get further anime seasons. Jellyfin uses numbered seasons whereas MAL uses entirely different entities.
/// </summary>
/// <param name="animeId"></param>
/// <param name="seasonNumber"></param>
/// <returns></returns>
private async Task<Anime> GetDifferentSeasonAnime(int animeId, int seasonNumber, string? alternativeId = null) {
_logger.LogInformation($"({_apiName}) Attempting to get season 1...");
Anime initialSeason = await _apiCallHelpers.GetAnime(animeId, getRelated: true, alternativeId: alternativeId);
if (initialSeason != null) {
int i = 1;
while (i != seasonNumber) {
RelatedAnime initialSeasonRelatedAnime = initialSeason.RelatedAnime.FirstOrDefault(item => item.RelationType == RelationType.Sequel);
if (initialSeasonRelatedAnime != null) {
_logger.LogInformation($"({_apiName}) Attempting to get season {i + 1}...");
Anime nextSeason = await _apiCallHelpers.GetAnime(initialSeasonRelatedAnime.Anime.Id, getRelated: true, alternativeId: initialSeasonRelatedAnime.Anime.AlternativeId);
if (nextSeason != null) {
initialSeason = nextSeason;
}
} else {
_logger.LogInformation($"({_apiName}) Could not find any related anime sequel");
return null;
}
i++;
}
return initialSeason;
}
return null;
}
private async Task<Anime> GetOva(int animeId, string episodeName, string? alternativeId = null) {
Anime anime = await _apiCallHelpers.GetAnime(animeId, getRelated: true, alternativeId: alternativeId);
if (anime != null) {
var listOfRelatedAnime = anime.RelatedAnime.Where(relation => relation.RelationType is RelationType.Side_Story or RelationType.Alternative_Version or RelationType.Alternative_Setting);
foreach (RelatedAnime relatedAnime in listOfRelatedAnime) {
var detailedRelatedAnime = await _apiCallHelpers.GetAnime(relatedAnime.Anime.Id, alternativeId: relatedAnime.Anime.AlternativeId);
if (detailedRelatedAnime is { Title: { }, AlternativeTitles: { En: { } } }) {
if (ContainsExtended(detailedRelatedAnime.Title, episodeName) ||
(detailedRelatedAnime.AlternativeTitles.En != null && ContainsExtended(detailedRelatedAnime.AlternativeTitles.En, episodeName)) ||
(detailedRelatedAnime.AlternativeTitles.Ja != null && ContainsExtended(detailedRelatedAnime.AlternativeTitles.Ja, episodeName))) {
// rough match
return detailedRelatedAnime;
}
}
}
}
// no matches
return null;
}
}
}