2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common;
|
2016-06-04 00:23:44 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-04 20:19:29 +00:00
|
|
|
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;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-20 04:53:15 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-03 20:51:28 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-11-18 02:48:22 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2016-06-04 00:23:44 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly IApplicationHost _appHost;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
2020-05-12 20:03:15 +00:00
|
|
|
public OmdbItemProvider(
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
IApplicationHost appHost,
|
|
|
|
IHttpClient httpClient,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IServerConfigurationManager configurationManager)
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
_httpClient = httpClient;
|
2014-11-18 02:48:22 +00:00
|
|
|
_libraryManager = libraryManager;
|
2016-06-04 00:23:44 +00:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_configurationManager = configurationManager;
|
2018-09-12 17:26:21 +00:00
|
|
|
_appHost = appHost;
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
2020-05-12 20:03:15 +00:00
|
|
|
|
2019-01-17 19:24:39 +00:00
|
|
|
// After primary option
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 2;
|
2017-10-05 18:10:07 +00:00
|
|
|
|
2015-01-27 06:50:40 +00:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2015-01-27 06:50:40 +00:00
|
|
|
return GetSearchResults(searchInfo, "series", cancellationToken);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 06:50:40 +00:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2015-01-27 06:50:40 +00:00
|
|
|
return GetSearchResults(searchInfo, "movie", cancellationToken);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 19:16:25 +00:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ItemLookupInfo searchInfo, string type, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return GetSearchResultsInternal(searchInfo, type, true, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2016-04-06 02:18:56 +00:00
|
|
|
private async Task<IEnumerable<RemoteSearchResult>> GetSearchResultsInternal(ItemLookupInfo searchInfo, string type, bool isSearch, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2016-01-10 05:19:19 +00:00
|
|
|
var episodeSearchInfo = searchInfo as EpisodeInfo;
|
2015-10-01 01:43:41 +00:00
|
|
|
|
2015-01-27 06:50:40 +00:00
|
|
|
var imdbId = searchInfo.GetProviderId(MetadataProviders.Imdb);
|
|
|
|
|
2017-05-12 04:54:19 +00:00
|
|
|
var urlQuery = "plot=full&r=json";
|
2016-01-10 05:19:19 +00:00
|
|
|
if (type == "episode" && episodeSearchInfo != null)
|
|
|
|
{
|
|
|
|
episodeSearchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out imdbId);
|
|
|
|
}
|
|
|
|
|
2015-01-27 06:50:40 +00:00
|
|
|
var name = searchInfo.Name;
|
|
|
|
var year = searchInfo.Year;
|
|
|
|
|
2015-05-31 18:22:51 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(name))
|
2015-01-27 06:50:40 +00:00
|
|
|
{
|
2015-05-31 18:22:51 +00:00
|
|
|
var parsedName = _libraryManager.ParseName(name);
|
|
|
|
var yearInName = parsedName.Year;
|
|
|
|
name = parsedName.Name;
|
2020-05-12 20:03:15 +00:00
|
|
|
year ??= yearInName;
|
2015-01-27 06:50:40 +00:00
|
|
|
}
|
2015-10-01 01:43:41 +00:00
|
|
|
|
2015-05-31 18:22:51 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
{
|
|
|
|
if (year.HasValue)
|
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += "&y=" + year.Value.ToString(CultureInfo.InvariantCulture);
|
2015-05-31 18:22:51 +00:00
|
|
|
}
|
2015-01-27 06:50:40 +00:00
|
|
|
|
2015-10-01 01:43:41 +00:00
|
|
|
// &s means search and returns a list of results as opposed to t
|
2016-04-06 02:18:56 +00:00
|
|
|
if (isSearch)
|
2016-01-15 19:16:25 +00:00
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += "&s=" + WebUtility.UrlEncode(name);
|
2016-01-15 19:16:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += "&t=" + WebUtility.UrlEncode(name);
|
2016-01-15 19:16:25 +00:00
|
|
|
}
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += "&type=" + type;
|
2015-05-31 18:22:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += "&i=" + 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
|
|
|
|
2016-01-10 05:19:19 +00:00
|
|
|
if (type == "episode")
|
|
|
|
{
|
|
|
|
if (searchInfo.IndexNumber.HasValue)
|
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += string.Format(CultureInfo.InvariantCulture, "&Episode={0}", searchInfo.IndexNumber);
|
2016-01-10 05:19:19 +00:00
|
|
|
}
|
|
|
|
if (searchInfo.ParentIndexNumber.HasValue)
|
|
|
|
{
|
2017-05-12 04:54:19 +00:00
|
|
|
urlQuery += string.Format(CultureInfo.InvariantCulture, "&Season={0}", searchInfo.ParentIndexNumber);
|
2016-01-10 05:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 19:26:31 +00:00
|
|
|
var url = OmdbProvider.GetOmdbUrl(urlQuery, _appHost, cancellationToken);
|
2017-05-12 04:54:19 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var response = await OmdbProvider.GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false))
|
2015-01-27 06:50:40 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var stream = response.Content)
|
2015-10-01 01:43:41 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
var resultList = new List<SearchResult>();
|
2015-10-01 01:43:41 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
if (isSearch)
|
2016-01-10 05:19:19 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var searchResultList = await _jsonSerializer.DeserializeFromStreamAsync<SearchResultList>(stream).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
if (searchResultList != null && searchResultList.Search != null)
|
|
|
|
{
|
|
|
|
resultList.AddRange(searchResultList.Search);
|
|
|
|
}
|
2015-01-27 06:50:40 +00:00
|
|
|
}
|
2017-10-20 16:16:56 +00:00
|
|
|
else
|
2016-01-10 05:19:19 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var result = await _jsonSerializer.DeserializeFromStreamAsync<SearchResult>(stream).ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
if (string.Equals(result.Response, "true", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
resultList.Add(result);
|
|
|
|
}
|
2016-01-10 05:19:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
return resultList.Select(result =>
|
2015-01-27 06:50:40 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
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(MetadataProviders.Imdb, result.imdbID);
|
|
|
|
|
|
|
|
if (result.Year.Length > 0
|
2019-01-13 20:46:33 +00:00
|
|
|
&& int.TryParse(result.Year.Substring(0, Math.Min(result.Year.Length, 4)), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedYear))
|
2017-10-20 16:16:56 +00:00
|
|
|
{
|
|
|
|
item.ProductionYear = parsedYear;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(result.Released)
|
2019-01-13 20:46:33 +00:00
|
|
|
&& DateTime.TryParse(result.Released, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out var released))
|
2017-10-20 16:16:56 +00:00
|
|
|
{
|
|
|
|
item.PremiereDate = released;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(result.Poster) && !string.Equals(result.Poster, "N/A", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
item.ImageUrl = result.Poster;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
2015-01-27 06:50:40 +00:00
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-03-19 15:38:05 +00:00
|
|
|
return GetMovieResult<Trailer>(info, cancellationToken);
|
2014-09-22 21:56:54 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 15:38:05 +00:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(TrailerInfo searchInfo, CancellationToken cancellationToken)
|
2014-09-22 21:56:54 +00:00
|
|
|
{
|
2015-01-27 06:50:40 +00:00
|
|
|
return GetSearchResults(searchInfo, "movie", cancellationToken);
|
2014-09-22 21:56:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => "The Open Movie Database";
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2014-02-15 16:36:09 +00:00
|
|
|
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
2014-02-15 16:36:09 +00:00
|
|
|
var result = new MetadataResult<Series>
|
|
|
|
{
|
2016-09-18 04:27:17 +00:00
|
|
|
Item = new Series(),
|
|
|
|
QueriedById = true
|
2014-02-15 16:36:09 +00:00
|
|
|
};
|
|
|
|
|
2015-05-31 18:22:51 +00:00
|
|
|
var imdbId = info.GetProviderId(MetadataProviders.Imdb);
|
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
2014-07-12 14:05:51 +00:00
|
|
|
{
|
2015-05-31 18:22:51 +00:00
|
|
|
imdbId = await GetSeriesImdbId(info, cancellationToken).ConfigureAwait(false);
|
2016-09-18 04:27:17 +00:00
|
|
|
result.QueriedById = false;
|
2014-07-12 14:05:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 23:37:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(imdbId))
|
2014-02-15 16:36:09 +00:00
|
|
|
{
|
|
|
|
result.Item.SetProviderId(MetadataProviders.Imdb, imdbId);
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
await new OmdbProvider(_jsonSerializer, _httpClient, _fileSystem, _appHost, _configurationManager).Fetch(result, imdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 16:36:09 +00:00
|
|
|
public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
2014-02-15 16:36:09 +00:00
|
|
|
return GetMovieResult<Movie>(info, cancellationToken);
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 16:36:09 +00:00
|
|
|
private async Task<MetadataResult<T>> GetMovieResult<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>
|
|
|
|
{
|
2016-09-18 04:27:17 +00:00
|
|
|
Item = new T(),
|
|
|
|
QueriedById = true
|
2014-02-15 16:36:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var imdbId = info.GetProviderId(MetadataProviders.Imdb);
|
2015-05-28 23:37:43 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(imdbId))
|
2014-02-15 16:36:09 +00:00
|
|
|
{
|
2015-05-31 18:22:51 +00:00
|
|
|
imdbId = await GetMovieImdbId(info, cancellationToken).ConfigureAwait(false);
|
2016-09-18 04:27:17 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
result.Item.SetProviderId(MetadataProviders.Imdb, imdbId);
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
await new OmdbProvider(_jsonSerializer, _httpClient, _fileSystem, _appHost, _configurationManager).Fetch(result, imdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-31 18:22:51 +00:00
|
|
|
private async Task<string> GetMovieImdbId(ItemLookupInfo info, CancellationToken cancellationToken)
|
2014-02-15 16:36:09 +00:00
|
|
|
{
|
2016-01-15 19:16:25 +00:00
|
|
|
var results = await GetSearchResultsInternal(info, "movie", false, cancellationToken).ConfigureAwait(false);
|
2015-05-31 18:22:51 +00:00
|
|
|
var first = results.FirstOrDefault();
|
|
|
|
return first == null ? null : first.GetProviderId(MetadataProviders.Imdb);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:22:51 +00:00
|
|
|
private async Task<string> GetSeriesImdbId(SeriesInfo info, CancellationToken cancellationToken)
|
2014-02-15 16:36:09 +00:00
|
|
|
{
|
2016-01-15 19:16:25 +00:00
|
|
|
var results = await GetSearchResultsInternal(info, "series", false, cancellationToken).ConfigureAwait(false);
|
2015-05-31 18:22:51 +00:00
|
|
|
var first = results.FirstOrDefault();
|
|
|
|
return first == null ? null : first.GetProviderId(MetadataProviders.Imdb);
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
2015-01-27 06:50:40 +00:00
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 18:39:41 +00:00
|
|
|
Url = url
|
2015-01-27 06:50:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchResult
|
|
|
|
{
|
|
|
|
public string Title { get; set; }
|
|
|
|
public string Year { get; set; }
|
|
|
|
public string Rated { get; set; }
|
|
|
|
public string Released { get; set; }
|
2016-01-10 05:19:19 +00:00
|
|
|
public string Season { get; set; }
|
|
|
|
public string Episode { get; set; }
|
2015-01-27 06:50:40 +00:00
|
|
|
public string Runtime { get; set; }
|
|
|
|
public string Genre { get; set; }
|
|
|
|
public string Director { get; set; }
|
|
|
|
public string Writer { get; set; }
|
|
|
|
public string Actors { get; set; }
|
|
|
|
public string Plot { get; set; }
|
|
|
|
public string Language { get; set; }
|
|
|
|
public string Country { get; set; }
|
|
|
|
public string Awards { get; set; }
|
|
|
|
public string Poster { get; set; }
|
|
|
|
public string Metascore { get; set; }
|
|
|
|
public string imdbRating { get; set; }
|
|
|
|
public string imdbVotes { get; set; }
|
|
|
|
public string imdbID { get; set; }
|
2016-01-10 05:19:19 +00:00
|
|
|
public string seriesID { get; set; }
|
2015-01-27 06:50:40 +00:00
|
|
|
public string Type { get; set; }
|
|
|
|
public string Response { get; set; }
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
2015-10-01 01:43:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|