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

269 lines
10 KiB
C#
Raw Normal View History

2015-06-01 14:49:23 +00:00
using MediaBrowser.Common.Net;
2014-09-22 21:56:54 +00:00
using MediaBrowser.Controller.Channels;
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;
2015-06-01 14:49:23 +00:00
using MediaBrowser.Controller.LiveTv;
2014-02-03 20:51:28 +00:00
using MediaBrowser.Controller.Providers;
2014-09-22 21:56:54 +00:00
using MediaBrowser.Model.Channels;
2014-02-15 16:36:09 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Providers;
2014-02-03 20:51:28 +00:00
using MediaBrowser.Model.Serialization;
2014-02-15 16:36:09 +00:00
using System;
using System.Collections.Generic;
2015-01-27 06:50:40 +00:00
using System.Globalization;
2015-06-01 14:49:23 +00:00
using System.Linq;
2015-01-27 06:50:40 +00:00
using System.Net;
2014-02-03 20:51:28 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Omdb
{
2014-02-15 16:36:09 +00:00
public class OmdbItemProvider : IRemoteMetadataProvider<Series, SeriesInfo>,
2015-06-01 14:49:23 +00:00
IRemoteMetadataProvider<Movie, MovieInfo>, IRemoteMetadataProvider<ChannelVideoItem, ChannelItemLookupInfo>, IRemoteMetadataProvider<LiveTvProgram, LiveTvProgramLookupInfo>
2014-02-03 20:51:28 +00:00
{
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClient _httpClient;
2014-02-15 16:36:09 +00:00
private readonly ILogger _logger;
2014-11-18 02:48:22 +00:00
private readonly ILibraryManager _libraryManager;
2014-02-03 20:51:28 +00:00
2014-11-18 02:48:22 +00:00
public OmdbItemProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogger logger, ILibraryManager libraryManager)
2014-02-03 20:51:28 +00:00
{
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
2014-02-15 16:36:09 +00:00
_logger = logger;
2014-11-18 02:48:22 +00:00
_libraryManager = libraryManager;
2014-02-03 20:51:28 +00:00
}
2015-01-27 06:50:40 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
2015-01-27 06:50:40 +00:00
return GetSearchResults(searchInfo, "series", cancellationToken);
}
2015-01-27 06:50:40 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
{
2015-01-27 06:50:40 +00:00
return GetSearchResults(searchInfo, "movie", cancellationToken);
}
2015-06-01 14:49:23 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(LiveTvProgramLookupInfo searchInfo, CancellationToken cancellationToken)
{
if (!searchInfo.IsMovie)
{
return Task.FromResult<IEnumerable<RemoteSearchResult>>(new List<RemoteSearchResult>());
}
return GetSearchResults(searchInfo, "movie", cancellationToken);
}
2015-01-27 06:50:40 +00:00
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ItemLookupInfo searchInfo, string type, CancellationToken cancellationToken)
{
2015-01-27 06:50:40 +00:00
var list = new List<RemoteSearchResult>();
var imdbId = searchInfo.GetProviderId(MetadataProviders.Imdb);
var url = "http://www.omdbapi.com/?plot=short&r=json";
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;
year = year ?? yearInName;
2015-01-27 06:50:40 +00:00
}
2015-05-31 18:22:51 +00:00
if (string.IsNullOrWhiteSpace(imdbId))
{
if (year.HasValue)
{
url += "&y=" + year.Value.ToString(CultureInfo.InvariantCulture);
}
2015-01-27 06:50:40 +00:00
2015-05-31 18:22:51 +00:00
url += "&t=" + WebUtility.UrlEncode(name);
url += "&type=" + type;
}
else
{
url += "&i=" + imdbId;
}
2015-01-27 06:50:40 +00:00
using (var stream = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = OmdbProvider.ResourcePool,
CancellationToken = cancellationToken,
CacheMode = CacheMode.Unconditional,
2015-05-31 18:22:51 +00:00
CacheLength = TimeSpan.FromDays(2)
2015-01-27 06:50:40 +00:00
}).ConfigureAwait(false))
{
var result = _jsonSerializer.DeserializeFromStream<SearchResult>(stream);
if (string.Equals(result.Response, "true", StringComparison.OrdinalIgnoreCase))
{
var item = new RemoteSearchResult();
item.SearchProviderName = Name;
item.Name = result.Title;
item.SetProviderId(MetadataProviders.Imdb, result.imdbID);
int parsedYear;
if (int.TryParse(result.Year, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedYear))
{
item.ProductionYear = parsedYear;
}
if (!string.IsNullOrWhiteSpace(result.Poster) && !string.Equals(result.Poster, "N/A", StringComparison.OrdinalIgnoreCase))
{
item.ImageUrl = result.Poster;
}
list.Add(item);
}
}
return list;
}
2014-09-22 21:56:54 +00:00
public Task<MetadataResult<ChannelVideoItem>> GetMetadata(ChannelItemLookupInfo info, CancellationToken cancellationToken)
{
2014-09-28 15:27:26 +00:00
if (info.ContentType != ChannelMediaContentType.MovieExtra || info.ExtraType != ExtraType.Trailer)
2014-09-22 21:56:54 +00:00
{
return Task.FromResult(new MetadataResult<ChannelVideoItem>());
}
return GetMovieResult<ChannelVideoItem>(info, cancellationToken);
}
2015-01-27 06:50:40 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ChannelItemLookupInfo searchInfo, CancellationToken cancellationToken)
2014-09-22 21:56:54 +00:00
{
2015-01-27 06:50:40 +00:00
if (searchInfo.ContentType != ChannelMediaContentType.MovieExtra || searchInfo.ExtraType != ExtraType.Trailer)
{
return Task.FromResult<IEnumerable<RemoteSearchResult>>(new List<RemoteSearchResult>());
}
return GetSearchResults(searchInfo, "movie", cancellationToken);
2014-09-22 21:56:54 +00:00
}
public string Name
{
2014-02-20 21:10:26 +00:00
get { return "The Open Movie Database"; }
}
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>
{
Item = new Series()
};
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);
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;
2015-05-31 18:22:51 +00:00
await new OmdbProvider(_jsonSerializer, _httpClient).Fetch(result.Item, imdbId, cancellationToken).ConfigureAwait(false);
2014-02-15 16:36:09 +00:00
}
return result;
2014-02-03 20:51:28 +00:00
}
2015-06-01 14:49:23 +00:00
public Task<MetadataResult<LiveTvProgram>> GetMetadata(LiveTvProgramLookupInfo info, CancellationToken cancellationToken)
{
if (!info.IsMovie)
{
return Task.FromResult(new MetadataResult<LiveTvProgram>());
}
return GetMovieResult<LiveTvProgram>(info, cancellationToken);
}
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-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>
{
Item = new T()
};
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);
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;
2015-05-31 18:22:51 +00:00
await new OmdbProvider(_jsonSerializer, _httpClient).Fetch(result.Item, imdbId, 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
{
2015-05-31 18:22:51 +00:00
var results = await GetSearchResults(info, "movie", cancellationToken).ConfigureAwait(false);
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
{
2015-05-31 18:22:51 +00:00
var results = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
var first = results.FirstOrDefault();
return first == null ? null : first.GetProviderId(MetadataProviders.Imdb);
2014-02-03 20:51:28 +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,
Url = url,
ResourcePool = OmdbProvider.ResourcePool
});
}
class SearchResult
{
public string Title { get; set; }
public string Year { get; set; }
public string Rated { get; set; }
public string Released { get; set; }
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; }
public string Type { get; set; }
public string Response { get; set; }
}
2014-02-03 20:51:28 +00:00
}
}