jellyfin/MediaBrowser.Providers/Plugins/Tmdb/BoxSets/TmdbBoxSetProvider.cs

131 lines
5.0 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
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;
using MediaBrowser.Controller.Library;
2014-01-30 14:23:54 -07:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
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
{
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;
private readonly TmdbClientManager _tmdbClientManager;
private readonly ILibraryManager _libraryManager;
2014-01-30 14:23:54 -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;
_tmdbClientManager = tmdbClientManager;
_libraryManager = libraryManager;
2014-01-30 14:23:54 -07:00
}
public string Name => TmdbUtils.ProviderName;
2019-01-07 16:27:46 -07:00
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
{
var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
var language = searchInfo.MetadataLanguage;
2014-03-01 15:34:27 -07:00
if (tmdbId > 0)
2014-03-01 15:34:27 -07:00
{
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
2014-09-22 14:56:54 -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
{
Name = collection.Name,
SearchProviderName = Name
2014-03-01 15:34:27 -07:00
};
if (collection.Images != null)
2014-01-30 14:23:54 -07:00
{
result.ImageUrl = _tmdbClientManager.GetPosterUrl(collection.PosterPath);
2014-01-30 14:23:54 -07:00
}
result.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
2014-01-30 14:23:54 -07:00
return new[] { result };
2014-01-30 14:47:13 -07:00
}
var collectionSearchResults = await _tmdbClientManager.SearchCollectionAsync(searchInfo.Name, language, cancellationToken).ConfigureAwait(false);
2014-01-30 14:47:13 -07:00
var collections = new List<RemoteSearchResult>();
for (var i = 0; i < collectionSearchResults.Count; i++)
2014-01-30 14:47:13 -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
collections.Add(collection);
2020-06-20 01:35:29 -07:00
}
2014-01-30 14:23:54 -07:00
return collections;
2014-01-30 14:23:54 -07:00
}
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo id, CancellationToken cancellationToken)
2014-01-30 14:23:54 -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
{
// 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
if (searchResults != null && searchResults.Count > 0)
{
tmdbId = searchResults[0].Id;
}
}
var result = new MetadataResult<BoxSet>();
2014-01-30 14:23:54 -07:00
if (tmdbId > 0)
2020-08-17 12:59:29 -07:00
{
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
2014-01-30 14:23:54 -07:00
if (collection != null)
2014-01-30 14:23:54 -07:00
{
var item = new BoxSet
{
Name = collection.Name,
Overview = collection.Overview
};
2020-05-30 23:28:01 -07:00
item.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
2014-01-30 14:23:54 -07:00
result.HasMetadata = true;
result.Item = item;
2014-01-30 14:23:54 -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)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
2014-01-30 14:23:54 -07:00
}
}