2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2019-01-13 20:26:31 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
2020-08-17 21:10:02 +02:00
|
|
|
using System.Net.Http;
|
2019-01-13 20:26:31 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 19:05:21 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-30 22:23:54 +01:00
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2021-03-17 10:29:45 +01:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-01-30 22:23:54 +01:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-20 05:53:15 +01:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2020-05-31 08:23:09 +02:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// BoxSet provider powered by TMDb.
|
|
|
|
/// </summary>
|
2019-08-18 13:20:52 +02:00
|
|
|
public class TmdbBoxSetProvider : IRemoteMetadataProvider<BoxSet, BoxSetInfo>
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-09-24 19:49:35 +02:00
|
|
|
private readonly TmdbClientManager _tmdbClientManager;
|
2021-03-17 10:29:45 +01:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TmdbBoxSetProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param>
|
|
|
|
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
|
|
|
|
/// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
|
2021-03-17 10:29:45 +01:00
|
|
|
public TmdbBoxSetProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager, ILibraryManager libraryManager)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2020-09-24 19:49:35 +02:00
|
|
|
_tmdbClientManager = tmdbClientManager;
|
2021-03-17 10:29:45 +01:00
|
|
|
_libraryManager = libraryManager;
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2020-09-24 19:49:35 +02:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2019-01-08 00:27:46 +01:00
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2014-02-20 05:53:15 +01:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
|
|
|
var language = searchInfo.MetadataLanguage;
|
2014-03-01 23:34:27 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
if (tmdbId > 0)
|
2014-03-01 23:34:27 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
|
2014-09-22 23:56:54 +02:00
|
|
|
|
2022-12-05 15:00:20 +01:00
|
|
|
if (collection is null)
|
2020-09-24 19:49:35 +02:00
|
|
|
{
|
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
|
|
|
}
|
2014-09-22 23:56:54 +02:00
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
var result = new RemoteSearchResult
|
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
Name = collection.Name,
|
|
|
|
SearchProviderName = Name
|
2014-03-01 23:34:27 +01:00
|
|
|
};
|
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (collection.Images is not null)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
result.ImageUrl = _tmdbClientManager.GetPosterUrl(collection.PosterPath);
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
result.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
return new[] { result };
|
2014-01-30 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
var collectionSearchResults = await _tmdbClientManager.SearchCollectionAsync(searchInfo.Name, language, cancellationToken).ConfigureAwait(false);
|
2014-01-30 22:47:13 +01:00
|
|
|
|
2023-03-01 00:44:57 +01:00
|
|
|
var collections = new RemoteSearchResult[collectionSearchResults.Count];
|
2020-09-24 19:49:35 +02:00
|
|
|
for (var i = 0; i < collectionSearchResults.Count; i++)
|
2014-01-30 22:47:13 +01:00
|
|
|
{
|
2023-12-17 05:28:50 +01:00
|
|
|
var result = collectionSearchResults[i];
|
2020-09-24 19:49:35 +02:00
|
|
|
var collection = new RemoteSearchResult
|
|
|
|
{
|
2023-12-17 05:28:50 +01:00
|
|
|
Name = result.Name,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
ImageUrl = _tmdbClientManager.GetPosterUrl(result.PosterPath)
|
2020-09-24 19:49:35 +02:00
|
|
|
};
|
2023-12-17 05:28:50 +01:00
|
|
|
collection.SetProviderId(MetadataProvider.Tmdb, result.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2023-03-01 00:44:57 +01:00
|
|
|
collections[i] = collection;
|
2020-06-20 10:35:29 +02:00
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
return collections;
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2021-06-27 02:00:17 +02:00
|
|
|
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo info, CancellationToken cancellationToken)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2021-06-27 02:00:17 +02:00
|
|
|
var tmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
|
|
|
var language = info.MetadataLanguage;
|
2020-09-24 19:49:35 +02:00
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
if (tmdbId <= 0)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2021-03-17 10:29:45 +01:00
|
|
|
// ParseName is required here.
|
|
|
|
// Caller provides the filename with extension stripped and NOT the parsed filename
|
2021-06-27 02:00:17 +02:00
|
|
|
var parsedName = _libraryManager.ParseName(info.Name);
|
2021-03-17 10:29:45 +01:00
|
|
|
var cleanedName = TmdbUtils.CleanName(parsedName.Name);
|
|
|
|
var searchResults = await _tmdbClientManager.SearchCollectionAsync(cleanedName, language, cancellationToken).ConfigureAwait(false);
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (searchResults is not null && searchResults.Count > 0)
|
2020-09-24 19:49:35 +02:00
|
|
|
{
|
|
|
|
tmdbId = searchResults[0].Id;
|
|
|
|
}
|
2016-03-04 18:38:51 +01:00
|
|
|
}
|
2014-02-18 04:02:41 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
var result = new MetadataResult<BoxSet>();
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
if (tmdbId > 0)
|
2020-08-17 21:59:29 +02:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (collection is not null)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var item = new BoxSet
|
2016-03-04 18:38:51 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
Name = collection.Name,
|
|
|
|
Overview = collection.Overview
|
|
|
|
};
|
2020-05-31 08:28:01 +02:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
result.HasMetadata = true;
|
|
|
|
result.Item = item;
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
return result;
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2020-08-17 21:10:02 +02:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 05:53:15 +01:00
|
|
|
{
|
2020-08-31 19:05:21 +02:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2014-02-20 05:53:15 +01:00
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
}
|