summaryrefslogtreecommitdiff
path: root/ImageProvider.cs
blob: df349eba7093d8aa12ab902daa4f09ccc8833e0c (plain)
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
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Model.Dto;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.JCoverXtremePro;

public class ImageProvider
    : IRemoteImageProvider, IHasOrder
{
    private ILogger _logger;


    public ImageProvider(ILogger<ImageProvider> logger)
    {
        _logger = logger;
    }

    public bool Supports(BaseItem item)
    {
        return item is Movie;
    }

    public string Name => "Mediux";

    public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
    {
        return new List<ImageType>
        {
            ImageType.Primary,
            // 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 =
            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));
        List<RemoteImageInfo> images = new();
        foreach (var set in deserMovieData.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")
                {
                    _logger.LogInformation("Skipping non poster file");
                    continue;
                }

                if (file.title.Contains(deserMovieData.movie.title))
                {
                    _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");
                }
            }
        }

        _logger.LogInformation("Collected images {0}", images);
        return images;
    }

    public int Order => 0;

    public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
    {
        return MediuxDownloader.instance.DownloadFile(url);
    }
}