2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
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;
|
2020-08-17 19:10:02 +00:00
|
|
|
using System.Net.Http;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 17:05:21 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-30 21:23:54 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-20 04:53:15 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-05-31 06:23:09 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbBoxSetProvider : IRemoteMetadataProvider<BoxSet, BoxSetInfo>
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-09-24 17:49:35 +00:00
|
|
|
private readonly TmdbClientManager _tmdbClientManager;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
public TmdbBoxSetProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2020-09-24 17:49:35 +00:00
|
|
|
_tmdbClientManager = tmdbClientManager;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
|
|
|
var language = searchInfo.MetadataLanguage;
|
2014-03-01 22:34:27 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (tmdbId > 0)
|
2014-03-01 22:34:27 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
|
2014-09-22 21:56:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (collection == null)
|
|
|
|
{
|
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
|
|
|
}
|
2014-09-22 21:56:54 +00:00
|
|
|
|
2014-03-01 22:34:27 +00:00
|
|
|
var result = new RemoteSearchResult
|
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
Name = collection.Name,
|
|
|
|
SearchProviderName = Name
|
2014-03-01 22:34:27 +00:00
|
|
|
};
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (collection.Images != null)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
result.ImageUrl = _tmdbClientManager.GetPosterUrl(collection.PosterPath);
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
result.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return new[] { result };
|
2014-01-30 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var collectionSearchResults = await _tmdbClientManager.SearchCollectionAsync(searchInfo.Name, language, cancellationToken).ConfigureAwait(false);
|
2014-01-30 21:47:13 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var collections = new List<RemoteSearchResult>();
|
|
|
|
for (var i = 0; i < collectionSearchResults.Count; i++)
|
2014-01-30 21:47:13 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var collection = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = collectionSearchResults[i].Name,
|
|
|
|
SearchProviderName = Name
|
|
|
|
};
|
|
|
|
collection.SetProviderId(MetadataProvider.Tmdb, collectionSearchResults[i].Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
collections.Add(collection);
|
2020-06-20 08:35:29 +00:00
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return collections;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo id, CancellationToken cancellationToken)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var tmdbId = Convert.ToInt32(id.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
|
|
|
var language = id.MetadataLanguage;
|
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
if (tmdbId <= 0)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var searchResults = await _tmdbClientManager.SearchCollectionAsync(id.Name, language, cancellationToken).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (searchResults != null && searchResults.Count > 0)
|
|
|
|
{
|
|
|
|
tmdbId = searchResults[0].Id;
|
|
|
|
}
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2014-02-18 03:02:41 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var result = new MetadataResult<BoxSet>();
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (tmdbId > 0)
|
2020-08-17 19:59:29 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (collection != null)
|
2014-01-30 21:23:54 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var item = new BoxSet
|
2016-03-04 17:38:51 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
Name = collection.Name,
|
|
|
|
Overview = collection.Overview
|
|
|
|
};
|
2020-05-31 06:28:01 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
item.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
result.HasMetadata = true;
|
|
|
|
result.Item = item;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return result;
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2020-08-31 17:05:21 +00:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
}
|
|
|
|
}
|