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

314 lines
11 KiB
C#
Raw Normal View History

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