diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-10 01:18:40 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-10 01:18:40 +0100 |
commit | 38ac187fc44aa1531e492fb65436e2a7e5e5cbb1 (patch) | |
tree | e77a43763fb7b7bed2a6843b0777aa8f434ea942 | |
parent | 9a06e4e555fe10edc9df4693e63b07aa6b568ddf (diff) | |
download | JCoverXtremePro-38ac187fc44aa1531e492fb65436e2a7e5e5cbb1.tar.gz JCoverXtremePro-38ac187fc44aa1531e492fb65436e2a7e5e5cbb1.tar.bz2 JCoverXtremePro-38ac187fc44aa1531e492fb65436e2a7e5e5cbb1.zip |
feat: Added season images
-rw-r--r-- | Api/JCoverSharedController.cs | 105 | ||||
-rw-r--r-- | Api/coverscript.js | 2 | ||||
-rw-r--r-- | POJO.cs | 6 |
3 files changed, 74 insertions, 39 deletions
diff --git a/Api/JCoverSharedController.cs b/Api/JCoverSharedController.cs index 47a8c8a..dea10c4 100644 --- a/Api/JCoverSharedController.cs +++ b/Api/JCoverSharedController.cs @@ -10,6 +10,7 @@ using System.Web; using Jellyfin.Api; using MediaBrowser.Controller; using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; @@ -68,72 +69,100 @@ public class JCoverSharedController : BaseJellyfinApiController })); } - [HttpPost("DownloadSeries")] - [ProducesResponseType(StatusCodes.Status204NoContent)] - public async Task<ActionResult> DownloadEntireSeriesImages( - [FromBody, Required] JsonObject setMeta - ) + private static Dictionary<(int, int), POJO.File> CreateCoverFileLUT(POJO.Set set) { - // TODO: handle missing fields, local seasons missing, series missing, etc. - var setMetaObj = JsonSerializer.Deserialize<SetMeta>(setMeta); - var series = _libraryManager.GetItemById(setMetaObj.seriesId) as Series; - var jsonMeta = await MediuxDownloader.instance.GetMediuxMetadata($"https://mediux.pro/sets/{setMetaObj.setId}") - .ConfigureAwait(false); - var set = JsonSerializer.Deserialize<POJO.SetData>(jsonMeta).set; - Dictionary<string, (int, int)> lut = new(); + Dictionary<string, (int, int)> episodeIdToEpisodeNumber = new(); foreach (var showSeason in set.show.seasons) { + episodeIdToEpisodeNumber[showSeason.id] = (showSeason.season_number, -10); foreach (var showEpisode in showSeason.episodes) { - lut[showEpisode.id] = (showSeason.season_number, showEpisode.episode_number); + episodeIdToEpisodeNumber[showEpisode.id] = (showSeason.season_number, showEpisode.episode_number); } } - Dictionary<(int, int), POJO.File> files = new(); + Dictionary<(int, int), POJO.File> episodeNumberToFile = new(); foreach (var file in set.files) { - var episode = file.episode_id; - if (episode == null) + string id = string.Empty; + if (file.episode_id != null) { - continue; + id = file.episode_id.id; } - var tup = lut[episode.id]; - files[tup] = file; + if (file.season_id != null) + { + id = file.season_id.id; + } + + var tup = episodeIdToEpisodeNumber.GetValueOrDefault(id); + episodeNumberToFile[tup] = file; } + return episodeNumberToFile; + } + + [HttpPost("DownloadSeries")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task<ActionResult> DownloadEntireSeriesImages( + [FromBody, Required] JsonObject setMeta + ) + { + // TODO: handle missing fields, local seasons missing, series missing, etc. + var setMetaObj = JsonSerializer.Deserialize<SetMeta>(setMeta); + var series = _libraryManager.GetItemById(setMetaObj.seriesId) as Series; + var jsonMeta = await MediuxDownloader.instance.GetMediuxMetadata($"https://mediux.pro/sets/{setMetaObj.setId}") + .ConfigureAwait(false); + var set = JsonSerializer.Deserialize<POJO.SetData>(jsonMeta).set; + var files = CreateCoverFileLUT(set); foreach (var item in series.GetSeasons(null, new DtoOptions(true))) { var season = item as Season; var seasonNumber = season.GetLookupInfo().IndexNumber.Value; Plugin.Logger.LogInformation($"Season id: {seasonNumber}:"); + await TryDownloadEpisode(season, files, (seasonNumber, -10)) + .ConfigureAwait(false); foreach (var itemAgain in season.GetEpisodes()) { var episode = itemAgain as Episode; var episodeNumber = episode.GetLookupInfo().IndexNumber.Value; Plugin.Logger.LogInformation($" * Episode id: {episodeNumber} {episode.Name}"); - POJO.File file; - if (files.TryGetValue((seasonNumber, episodeNumber), out file)) - { - Plugin.Logger.LogInformation($" Found cover: {file.downloadUrl}"); - // await _providerManager.SaveImage(item, imageUrl, type, null, CancellationToken.None) - // .ConfigureAwait(false); - - // await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false); - await _providerManager.SaveImage( - episode, file.downloadUrl, - // Note: this needs to be updated if SeriesImageProvider ever supports more image types - ImageType.Primary, - null, - CancellationToken.None - ).ConfigureAwait(false); - - await episode.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None) - .ConfigureAwait(false); - } + await TryDownloadEpisode(episode, files, (seasonNumber, episodeNumber)) + .ConfigureAwait(false); } } return Empty; } + + private async Task TryDownloadEpisode( + BaseItem item, + Dictionary<(int, int), POJO.File> files, + (int, int) episodeNumber) + { + POJO.File file; + if (files.TryGetValue(episodeNumber, out file)) + { + Plugin.Logger.LogInformation($" Found cover: {file.downloadUrl}"); + await SaveCoverFileForItem(item, file.downloadUrl) + .ConfigureAwait(false); + } + } + + private async Task SaveCoverFileForItem( + BaseItem item, + string downloadUrl + ) + { + await _providerManager.SaveImage( + item, downloadUrl, + // Note: this needs to be updated if SeriesImageProvider ever supports more image types + ImageType.Primary, + null, + CancellationToken.None + ).ConfigureAwait(false); + + await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None) + .ConfigureAwait(false); + } }
\ No newline at end of file diff --git a/Api/coverscript.js b/Api/coverscript.js index 9f3eb7f..a4f0c04 100644 --- a/Api/coverscript.js +++ b/Api/coverscript.js @@ -80,8 +80,8 @@ const cardContainer = findParent(element, ".cardBox") const cardImage = cardContainer.querySelector("a.cardImageContainer[href]") const setMeta = extractSetMeta(cardImage.href) + if (!setMeta) return; if (downloadRowContainer.querySelector(`.${injectionMarker}`)) return - // TODO: extract information about the series, and check if this is at all viable downloadRowContainer.appendChild(createDownloadSeriesButton(element, setMeta)) }) @@ -70,12 +70,18 @@ public class POJO public string id { get; set; } } + public class SeasonId + { + public string id { get; set; } + } + public class File { public string fileType { get; set; } public string title { get; set; } public string id { get; set; } public EpisodeId? episode_id { get; set; } + public SeasonId? season_id { get; set; } public ImageType? JellyFinFileType() { |