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