diff options
-rw-r--r-- | ImageProvider.cs | 51 | ||||
-rw-r--r-- | MediuxDownloader.cs | 28 | ||||
-rw-r--r-- | POJO.cs | 15 |
3 files changed, 60 insertions, 34 deletions
diff --git a/ImageProvider.cs b/ImageProvider.cs index df349eb..8274a27 100644 --- a/ImageProvider.cs +++ b/ImageProvider.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Net.Http; using System.Text.Json; @@ -37,58 +38,46 @@ public class ImageProvider return new List<ImageType> { ImageType.Primary, - // ImageType.Backdrop, + ImageType.Backdrop, }; } public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken) { - var movie = item as Movie; var movieId = item.GetProviderId(MetadataProvider.Tmdb); - var collectionId = item.GetProviderId(MetadataProvider.TmdbCollection); - _logger.LogInformation( - $"Help i am stuck in a movie labeling factory and have been taskted to label {movie.Name} " + - $"({movieId} in collection {collectionId})" - ); - var movieData = + var movieJson = await MediuxDownloader.instance.GetMediuxMetadata("https://mediux.pro/movies/" + movieId) .ConfigureAwait(false); - var deserMovieData = JsonSerializer.Deserialize<POJO.MovieData>(movieData as JsonObject); - _logger.LogInformation("Movie Data: {JsonData}", movieData.ToJsonString()); - _logger.LogInformation("Movie Data Decoded: {Data}", JsonSerializer.Serialize(deserMovieData)); + var movieData = JsonSerializer.Deserialize<POJO.MovieData>(movieJson as JsonObject); List<RemoteImageInfo> images = new(); - foreach (var set in deserMovieData.allSets) + foreach (var set in movieData.allSets) { - _logger.LogInformation("Set Data: {Name} {Data}", set.set_name, set.files.Count); foreach (var file in set.files) { - _logger.LogInformation("Matching file {Name}", JsonSerializer.Serialize(file)); - if (file.fileType != "poster") + var ft = file.JellyFinFileType(); + if (ft == null) { - _logger.LogInformation("Skipping non poster file"); continue; } - if (file.title.Contains(deserMovieData.movie.title)) + if (!file.title.Contains(movieData.movie.title, StringComparison.InvariantCulture)) { - _logger.LogInformation("Adding image"); - var imageInfo = new RemoteImageInfo - { - Url = file.downloadUrl, - ProviderName = Name, - ThumbnailUrl = file.downloadUrl, - Language = "en", - RatingType = RatingType.Likes, - Type = ImageType.Primary, - }; - _logger.LogInformation("Constructed image"); - images.Add(imageInfo); - _logger.LogInformation("Appended image"); + continue; } + + var imageInfo = new RemoteImageInfo + { + Url = file.downloadUrl, + ProviderName = set.user_created.username + "(from Mediux)", + ThumbnailUrl = file.downloadUrl, + Language = "en", + RatingType = RatingType.Likes, + Type = ft.Value + }; + images.Add(imageInfo); } } - _logger.LogInformation("Collected images {0}", images); return images; } diff --git a/MediuxDownloader.cs b/MediuxDownloader.cs index a027773..c7c415d 100644 --- a/MediuxDownloader.cs +++ b/MediuxDownloader.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -71,11 +73,31 @@ public class MediuxDownloader .Content.ReadAsStringAsync().ConfigureAwait(false); } + private ConcurrentDictionary<string, SemaphoreSlim> cacheLock = new(); + private ConcurrentDictionary<string, JsonNode> cache = new(); + public async Task<JsonNode> GetMediuxMetadata(string url) { - Plugin.Logger.LogInformation("Loading data from {Url}", url); - var text = await GetString(url).ConfigureAwait(false); - return ExtractJsonNodes(text).First(); + var semaphore = cacheLock.GetOrAdd(url, ignored => new SemaphoreSlim(1)); + try + { + await semaphore.WaitAsync().ConfigureAwait(false); + if (cache.TryGetValue(url, out var data)) + { + Plugin.Logger.LogInformation("Loading cached data from {Url}", url); + return data; + } + + Plugin.Logger.LogInformation("Loading data from {Url}", url); + var text = await GetString(url).ConfigureAwait(false); + var node = ExtractJsonNodes(text).First(); + cache[url] = node; + return node; + } + finally + { + semaphore.Release(); + } } public async Task<HttpResponseMessage> DownloadFile(string url) @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; +using MediaBrowser.Model.Entities; namespace Jellyfin.Plugin.JCoverXtremePro; @@ -35,6 +36,20 @@ public class POJO public string title { get; set; } public string id { get; set; } + public ImageType? JellyFinFileType() + { + switch (fileType) + { + case "backdrop": + return ImageType.Backdrop; + case "poster": + return ImageType.Primary; + } + + return null; + } + + [JsonIgnore] public string downloadUrl => "https://api.mediux.pro/assets/" + id; } |