jellyfin/MediaBrowser.Providers/Plugins/Omdb/OmdbItemProvider.cs

343 lines
12 KiB
C#
Raw Normal View History

2021-07-23 02:33:19 +02:00
#pragma warning disable CS1591, SA1300
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
2020-08-17 21:10:02 +02:00
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions.Json;
using MediaBrowser.Common;
2020-08-31 19:05:21 +02:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
2014-02-03 21:51:28 +01:00
using MediaBrowser.Controller.Entities.TV;
2014-11-18 03:48:22 +01:00
using MediaBrowser.Controller.Library;
2014-02-03 21:51:28 +01:00
using MediaBrowser.Controller.Providers;
2014-02-15 17:36:09 +01:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
2014-02-03 21:51:28 +01:00
2020-03-09 15:36:02 +01:00
namespace MediaBrowser.Providers.Plugins.Omdb
2014-02-03 21:51:28 +01:00
{
2014-02-15 17:36:09 +01:00
public class OmdbItemProvider : IRemoteMetadataProvider<Series, SeriesInfo>,
2017-10-05 20:10:07 +02:00
IRemoteMetadataProvider<Movie, MovieInfo>, IRemoteMetadataProvider<Trailer, TrailerInfo>, IHasOrder
2014-02-03 21:51:28 +01:00
{
2020-08-17 21:10:02 +02:00
private readonly IHttpClientFactory _httpClientFactory;
2014-11-18 03:48:22 +01:00
private readonly ILibraryManager _libraryManager;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _configurationManager;
2018-09-12 19:26:21 +02:00
private readonly IApplicationHost _appHost;
2020-12-25 19:37:38 +01:00
private readonly JsonSerializerOptions _jsonOptions;
2014-02-03 21:51:28 +01:00
public OmdbItemProvider(
IApplicationHost appHost,
2020-08-17 21:10:02 +02:00
IHttpClientFactory httpClientFactory,
ILibraryManager libraryManager,
IFileSystem fileSystem,
IServerConfigurationManager configurationManager)
2014-02-03 21:51:28 +01:00
{
2020-08-17 21:10:02 +02:00
_httpClientFactory = httpClientFactory;
2014-11-18 03:48:22 +01:00
_libraryManager = libraryManager;
_fileSystem = fileSystem;
_configurationManager = configurationManager;
2018-09-12 19:26:21 +02:00
_appHost = appHost;
2020-12-25 19:37:38 +01:00
2021-03-09 05:57:38 +01:00
_jsonOptions = new JsonSerializerOptions(JsonDefaults.Options);
2020-12-25 19:37:38 +01:00
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableStringConverter());
2021-01-04 15:52:44 +01:00
_jsonOptions.Converters.Add(new JsonOmdbNotAvailableInt32Converter());
2014-02-03 21:51:28 +01:00
}
2020-09-07 13:20:39 +02:00
public string Name => "The Open Movie Database";
// After primary option
public int Order => 2;
2017-10-05 20:10:07 +02:00
2015-01-27 07:50:40 +01:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
2015-01-27 07:50:40 +01:00
return GetSearchResults(searchInfo, "series", cancellationToken);
}
2015-01-27 07:50:40 +01:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
{
2015-01-27 07:50:40 +01:00
return GetSearchResults(searchInfo, "movie", cancellationToken);
}
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ItemLookupInfo searchInfo, string type, CancellationToken cancellationToken)
{
return GetSearchResultsInternal(searchInfo, type, true, cancellationToken);
}
2016-04-06 04:18:56 +02:00
private async Task<IEnumerable<RemoteSearchResult>> GetSearchResultsInternal(ItemLookupInfo searchInfo, string type, bool isSearch, CancellationToken cancellationToken)
{
var episodeSearchInfo = searchInfo as EpisodeInfo;
2020-06-06 21:17:49 +02:00
var imdbId = searchInfo.GetProviderId(MetadataProvider.Imdb);
2015-01-27 07:50:40 +01:00
2017-05-12 06:54:19 +02:00
var urlQuery = "plot=full&r=json";
if (type == "episode" && episodeSearchInfo != null)
{
2020-06-06 21:17:49 +02:00
episodeSearchInfo.SeriesProviderIds.TryGetValue(MetadataProvider.Imdb.ToString(), out imdbId);
}
2015-01-27 07:50:40 +01:00
var name = searchInfo.Name;
var year = searchInfo.Year;
2015-05-31 20:22:51 +02:00
if (!string.IsNullOrWhiteSpace(name))
2015-01-27 07:50:40 +01:00
{
2015-05-31 20:22:51 +02:00
var parsedName = _libraryManager.ParseName(name);
var yearInName = parsedName.Year;
name = parsedName.Name;
year ??= yearInName;
2015-01-27 07:50:40 +01:00
}
2015-05-31 20:22:51 +02:00
if (string.IsNullOrWhiteSpace(imdbId))
{
if (year.HasValue)
{
2017-05-12 06:54:19 +02:00
urlQuery += "&y=" + year.Value.ToString(CultureInfo.InvariantCulture);
2015-05-31 20:22:51 +02:00
}
2015-01-27 07:50:40 +01:00
// &s means search and returns a list of results as opposed to t
2016-04-06 04:18:56 +02:00
if (isSearch)
{
2017-05-12 06:54:19 +02:00
urlQuery += "&s=" + WebUtility.UrlEncode(name);
}
else
{
2017-05-12 06:54:19 +02:00
urlQuery += "&t=" + WebUtility.UrlEncode(name);
}
2017-05-12 06:54:19 +02:00
urlQuery += "&type=" + type;
2015-05-31 20:22:51 +02:00
}
else
{
2017-05-12 06:54:19 +02:00
urlQuery += "&i=" + imdbId;
2016-04-20 04:28:24 +02:00
isSearch = false;
2015-05-31 20:22:51 +02:00
}
2015-01-27 07:50:40 +01:00
if (type == "episode")
{
if (searchInfo.IndexNumber.HasValue)
{
2017-05-12 06:54:19 +02:00
urlQuery += string.Format(CultureInfo.InvariantCulture, "&Episode={0}", searchInfo.IndexNumber);
}
if (searchInfo.ParentIndexNumber.HasValue)
{
2017-05-12 06:54:19 +02:00
urlQuery += string.Format(CultureInfo.InvariantCulture, "&Season={0}", searchInfo.ParentIndexNumber);
}
}
2020-08-07 19:26:28 +02:00
var url = OmdbProvider.GetOmdbUrl(urlQuery);
2017-05-12 06:54:19 +02:00
2020-08-31 19:05:21 +02:00
using var response = await OmdbProvider.GetOmdbResponse(_httpClientFactory.CreateClient(NamedClient.Default), url, cancellationToken).ConfigureAwait(false);
2020-11-17 19:43:00 +01:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2020-08-17 21:10:02 +02:00
var resultList = new List<SearchResult>();
if (isSearch)
2015-01-27 07:50:40 +01:00
{
var searchResultList = await JsonSerializer.DeserializeAsync<SearchResultList>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
2020-08-17 21:10:02 +02:00
if (searchResultList != null && searchResultList.Search != null)
{
2020-08-17 21:10:02 +02:00
resultList.AddRange(searchResultList.Search);
2017-10-20 18:16:56 +02:00
}
2015-01-27 07:50:40 +01:00
}
2020-08-17 21:10:02 +02:00
else
{
var result = await JsonSerializer.DeserializeAsync<SearchResult>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
2020-08-17 21:10:02 +02:00
if (string.Equals(result.Response, "true", StringComparison.OrdinalIgnoreCase))
{
resultList.Add(result);
}
}
return resultList.Select(result =>
{
var item = new RemoteSearchResult
{
IndexNumber = searchInfo.IndexNumber,
Name = result.Title,
ParentIndexNumber = searchInfo.ParentIndexNumber,
SearchProviderName = Name
};
if (episodeSearchInfo != null && episodeSearchInfo.IndexNumberEnd.HasValue)
{
item.IndexNumberEnd = episodeSearchInfo.IndexNumberEnd.Value;
}
item.SetProviderId(MetadataProvider.Imdb, result.imdbID);
if (result.Year.Length > 0
&& int.TryParse(result.Year.AsSpan().Slice(0, Math.Min(result.Year.Length, 4)), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedYear))
{
item.ProductionYear = parsedYear;
}
if (!string.IsNullOrEmpty(result.Released)
&& DateTime.TryParse(result.Released, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out var released))
{
item.PremiereDate = released;
}
if (!string.IsNullOrWhiteSpace(result.Poster) && !string.Equals(result.Poster, "N/A", StringComparison.OrdinalIgnoreCase))
{
item.ImageUrl = result.Poster;
}
return item;
});
}
2016-03-19 16:38:05 +01:00
public Task<MetadataResult<Trailer>> GetMetadata(TrailerInfo info, CancellationToken cancellationToken)
2014-09-22 23:56:54 +02:00
{
2016-03-19 16:38:05 +01:00
return GetMovieResult<Trailer>(info, cancellationToken);
2014-09-22 23:56:54 +02:00
}
2016-03-19 16:38:05 +01:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(TrailerInfo searchInfo, CancellationToken cancellationToken)
2014-09-22 23:56:54 +02:00
{
2015-01-27 07:50:40 +01:00
return GetSearchResults(searchInfo, "movie", cancellationToken);
2014-09-22 23:56:54 +02:00
}
2014-02-15 17:36:09 +01:00
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
2014-02-03 21:51:28 +01:00
{
2014-02-15 17:36:09 +01:00
var result = new MetadataResult<Series>
{
Item = new Series(),
QueriedById = true
2014-02-15 17:36:09 +01:00
};
2020-06-06 21:17:49 +02:00
var imdbId = info.GetProviderId(MetadataProvider.Imdb);
2015-05-31 20:22:51 +02:00
if (string.IsNullOrWhiteSpace(imdbId))
2014-07-12 16:05:51 +02:00
{
2015-05-31 20:22:51 +02:00
imdbId = await GetSeriesImdbId(info, cancellationToken).ConfigureAwait(false);
result.QueriedById = false;
2014-07-12 16:05:51 +02:00
}
2015-05-29 01:37:43 +02:00
if (!string.IsNullOrEmpty(imdbId))
2014-02-15 17:36:09 +01:00
{
2020-06-06 21:17:49 +02:00
result.Item.SetProviderId(MetadataProvider.Imdb, imdbId);
2014-02-15 17:36:09 +01:00
result.HasMetadata = true;
await new OmdbProvider(_httpClientFactory, _fileSystem, _appHost, _configurationManager).Fetch(result, imdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
2014-02-15 17:36:09 +01:00
}
return result;
2014-02-03 21:51:28 +01:00
}
2014-02-15 17:36:09 +01:00
public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
2014-02-03 21:51:28 +01:00
{
2014-02-15 17:36:09 +01:00
return GetMovieResult<Movie>(info, cancellationToken);
}
2014-02-15 17:36:09 +01:00
private async Task<MetadataResult<T>> GetMovieResult<T>(ItemLookupInfo info, CancellationToken cancellationToken)
2015-06-01 16:49:23 +02:00
where T : BaseItem, new()
2014-02-15 17:36:09 +01:00
{
var result = new MetadataResult<T>
{
Item = new T(),
QueriedById = true
2014-02-15 17:36:09 +01:00
};
2020-06-06 21:17:49 +02:00
var imdbId = info.GetProviderId(MetadataProvider.Imdb);
2015-05-29 01:37:43 +02:00
if (string.IsNullOrWhiteSpace(imdbId))
2014-02-15 17:36:09 +01:00
{
2015-05-31 20:22:51 +02:00
imdbId = await GetMovieImdbId(info, cancellationToken).ConfigureAwait(false);
result.QueriedById = false;
2014-02-15 17:36:09 +01:00
}
2015-05-31 20:22:51 +02:00
if (!string.IsNullOrEmpty(imdbId))
2014-02-15 17:36:09 +01:00
{
2020-06-06 21:17:49 +02:00
result.Item.SetProviderId(MetadataProvider.Imdb, imdbId);
2014-02-15 17:36:09 +01:00
result.HasMetadata = true;
await new OmdbProvider(_httpClientFactory, _fileSystem, _appHost, _configurationManager).Fetch(result, imdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
2014-02-15 17:36:09 +01:00
}
return result;
}
2015-05-31 20:22:51 +02:00
private async Task<string> GetMovieImdbId(ItemLookupInfo info, CancellationToken cancellationToken)
2014-02-15 17:36:09 +01:00
{
var results = await GetSearchResultsInternal(info, "movie", false, cancellationToken).ConfigureAwait(false);
2015-05-31 20:22:51 +02:00
var first = results.FirstOrDefault();
2020-09-07 13:20:39 +02:00
return first?.GetProviderId(MetadataProvider.Imdb);
2014-02-15 17:36:09 +01:00
}
2015-05-31 20:22:51 +02:00
private async Task<string> GetSeriesImdbId(SeriesInfo info, CancellationToken cancellationToken)
2014-02-15 17:36:09 +01:00
{
var results = await GetSearchResultsInternal(info, "series", false, cancellationToken).ConfigureAwait(false);
2015-05-31 20:22:51 +02:00
var first = results.FirstOrDefault();
2020-09-07 13:20:39 +02:00
return first?.GetProviderId(MetadataProvider.Imdb);
2014-02-03 21:51:28 +01:00
}
2020-08-17 21:10:02 +02:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 19:05:21 +02:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
2015-01-27 07:50:40 +01:00
}
2020-09-07 13:20:39 +02:00
private class SearchResult
2015-01-27 07:50:40 +01:00
{
public string Title { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Year { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Rated { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Released { get; set; }
2020-06-15 23:43:52 +02:00
public string Season { get; set; }
2020-06-15 23:43:52 +02:00
public string Episode { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Runtime { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Genre { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Director { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Writer { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Actors { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Plot { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Language { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Country { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Awards { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Poster { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Metascore { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string imdbRating { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string imdbVotes { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string imdbID { get; set; }
2020-06-15 23:43:52 +02:00
public string seriesID { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Type { get; set; }
2020-06-15 23:43:52 +02:00
2015-01-27 07:50:40 +01:00
public string Response { get; set; }
}
private class SearchResultList
{
/// <summary>
/// Gets or sets the results.
/// </summary>
/// <value>The results.</value>
public List<SearchResult> Search { get; set; }
}
2014-02-03 21:51:28 +01:00
}
}