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

133 lines
5.0 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
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.Library;
2014-01-30 21:23:54 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
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
{
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;
private readonly TmdbClientManager _tmdbClientManager;
private readonly ILibraryManager _libraryManager;
2014-01-30 21:23:54 +00:00
public TmdbBoxSetProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager, ILibraryManager libraryManager)
2014-01-30 21:23:54 +00:00
{
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
_libraryManager = libraryManager;
2014-01-30 21:23:54 +00:00
}
public string Name => TmdbUtils.ProviderName;
2019-01-07 23:27:46 +00: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 22:34:27 +00:00
if (tmdbId > 0)
2014-03-01 22:34:27 +00:00
{
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
2014-09-22 21:56:54 +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
{
Name = collection.Name,
SearchProviderName = Name
2014-03-01 22:34:27 +00:00
};
if (collection.Images != null)
2014-01-30 21:23:54 +00:00
{
result.ImageUrl = _tmdbClientManager.GetPosterUrl(collection.PosterPath);
2014-01-30 21:23:54 +00:00
}
result.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
2014-01-30 21:23:54 +00:00
return new[] { result };
2014-01-30 21:47:13 +00:00
}
var collectionSearchResults = await _tmdbClientManager.SearchCollectionAsync(searchInfo.Name, language, cancellationToken).ConfigureAwait(false);
2014-01-30 21:47:13 +00:00
var collections = new List<RemoteSearchResult>();
for (var i = 0; i < collectionSearchResults.Count; i++)
2014-01-30 21:47:13 +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
collections.Add(collection);
2020-06-20 08:35:29 +00:00
}
2014-01-30 21:23:54 +00:00
return collections;
2014-01-30 21:23:54 +00:00
}
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo info, CancellationToken cancellationToken)
2014-01-30 21:23:54 +00:00
{
var tmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
var language = info.MetadataLanguage;
// We don't already have an Id, need to fetch it
if (tmdbId <= 0)
2014-01-30 21:23:54 +00:00
{
// ParseName is required here.
// Caller provides the filename with extension stripped and NOT the parsed filename
var parsedName = _libraryManager.ParseName(info.Name);
var cleanedName = TmdbUtils.CleanName(parsedName.Name);
var searchResults = await _tmdbClientManager.SearchCollectionAsync(cleanedName, language, cancellationToken).ConfigureAwait(false);
2014-01-30 21:23:54 +00:00
if (searchResults != null && searchResults.Count > 0)
{
tmdbId = searchResults[0].Id;
}
}
var result = new MetadataResult<BoxSet>();
2014-01-30 21:23:54 +00:00
if (tmdbId > 0)
2020-08-17 19:59:29 +00:00
{
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, language, TmdbUtils.GetImageLanguagesParam(language), cancellationToken).ConfigureAwait(false);
2014-01-30 21:23:54 +00:00
if (collection != null)
2014-01-30 21:23:54 +00:00
{
var item = new BoxSet
{
Name = collection.Name,
Overview = collection.Overview
};
2020-05-31 06:28:01 +00:00
item.SetProviderId(MetadataProvider.Tmdb, collection.Id.ToString(CultureInfo.InvariantCulture));
2014-01-30 21:23:54 +00:00
result.HasMetadata = true;
result.Item = item;
2014-01-30 21:23:54 +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)
{
2020-08-31 17:05:21 +00:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
2014-01-30 21:23:54 +00:00
}
}