| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Net.Http; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Jellyfin.Data.Enums; |
| | 9 | | using Jellyfin.Extensions; |
| | 10 | | using MediaBrowser.Common.Net; |
| | 11 | | using MediaBrowser.Controller.Entities; |
| | 12 | | using MediaBrowser.Controller.Entities.TV; |
| | 13 | | using MediaBrowser.Controller.Providers; |
| | 14 | | using MediaBrowser.Model.Entities; |
| | 15 | | using MediaBrowser.Model.Providers; |
| | 16 | |
|
| | 17 | | namespace MediaBrowser.Providers.Plugins.Tmdb.TV |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// TV season provider powered by TheMovieDb. |
| | 21 | | /// </summary> |
| | 22 | | public class TmdbSeasonProvider : IRemoteMetadataProvider<Season, SeasonInfo> |
| | 23 | | { |
| | 24 | | private readonly IHttpClientFactory _httpClientFactory; |
| | 25 | | private readonly TmdbClientManager _tmdbClientManager; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="TmdbSeasonProvider"/> class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param> |
| | 31 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | 32 | | public TmdbSeasonProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager) |
| | 33 | | { |
| 21 | 34 | | _httpClientFactory = httpClientFactory; |
| 21 | 35 | | _tmdbClientManager = tmdbClientManager; |
| 21 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc /> |
| 0 | 39 | | public string Name => TmdbUtils.ProviderName; |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| | 42 | | public async Task<MetadataResult<Season>> GetMetadata(SeasonInfo info, CancellationToken cancellationToken) |
| | 43 | | { |
| | 44 | | var result = new MetadataResult<Season>(); |
| | 45 | | var config = Plugin.Instance.Configuration; |
| | 46 | |
|
| | 47 | | info.SeriesProviderIds.TryGetValue(MetadataProvider.Tmdb.ToString(), out string? seriesTmdbId); |
| | 48 | |
|
| | 49 | | var seasonNumber = info.IndexNumber; |
| | 50 | |
|
| | 51 | | if (string.IsNullOrWhiteSpace(seriesTmdbId) || !seasonNumber.HasValue) |
| | 52 | | { |
| | 53 | | return result; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | var seasonResult = await _tmdbClientManager |
| | 57 | | .GetSeasonAsync(Convert.ToInt32(seriesTmdbId, CultureInfo.InvariantCulture), seasonNumber.Value, info.Me |
| | 58 | | .ConfigureAwait(false); |
| | 59 | |
|
| | 60 | | if (seasonResult is null) |
| | 61 | | { |
| | 62 | | return result; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | result.HasMetadata = true; |
| | 66 | | result.Item = new Season |
| | 67 | | { |
| | 68 | | IndexNumber = seasonNumber, |
| | 69 | | Overview = seasonResult.Overview, |
| | 70 | | PremiereDate = seasonResult.AirDate, |
| | 71 | | ProductionYear = seasonResult.AirDate?.Year |
| | 72 | | }; |
| | 73 | |
|
| | 74 | | if (config.ImportSeasonName) |
| | 75 | | { |
| | 76 | | result.Item.Name = seasonResult.Name; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | result.Item.TrySetProviderId(MetadataProvider.Tvdb, seasonResult.ExternalIds.TvdbId); |
| | 80 | |
|
| | 81 | | // TODO why was this disabled? |
| | 82 | | var credits = seasonResult.Credits; |
| | 83 | |
|
| | 84 | | if (credits?.Cast is not null) |
| | 85 | | { |
| | 86 | | var castQuery = config.HideMissingCastMembers |
| | 87 | | ? credits.Cast.Where(a => !string.IsNullOrEmpty(a.ProfilePath)).OrderBy(a => a.Order) |
| | 88 | | : credits.Cast.OrderBy(a => a.Order); |
| | 89 | |
|
| | 90 | | foreach (var actor in castQuery.Take(config.MaxCastMembers)) |
| | 91 | | { |
| | 92 | | if (string.IsNullOrWhiteSpace(actor.Name)) |
| | 93 | | { |
| | 94 | | continue; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | var personInfo = new PersonInfo |
| | 98 | | { |
| | 99 | | Name = actor.Name.Trim(), |
| | 100 | | Role = actor.Character?.Trim() ?? string.Empty, |
| | 101 | | Type = PersonKind.Actor, |
| | 102 | | SortOrder = actor.Order, |
| | 103 | | ImageUrl = _tmdbClientManager.GetProfileUrl(actor.ProfilePath) |
| | 104 | | }; |
| | 105 | |
|
| | 106 | | if (actor.Id > 0) |
| | 107 | | { |
| | 108 | | personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture)) |
| | 109 | | } |
| | 110 | |
|
| | 111 | | result.AddPerson(personInfo); |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| | 115 | | if (credits?.Crew is not null) |
| | 116 | | { |
| | 117 | | var crewQuery = credits.Crew |
| | 118 | | .Select(crewMember => new |
| | 119 | | { |
| | 120 | | CrewMember = crewMember, |
| | 121 | | PersonType = TmdbUtils.MapCrewToPersonType(crewMember) |
| | 122 | | }) |
| | 123 | | .Where(entry => |
| | 124 | | TmdbUtils.WantedCrewKinds.Contains(entry.PersonType) || |
| | 125 | | TmdbUtils.WantedCrewTypes.Contains(entry.CrewMember.Job ?? string.Empty, StringComparison.Ordina |
| | 126 | |
|
| | 127 | | if (config.HideMissingCrewMembers) |
| | 128 | | { |
| | 129 | | crewQuery = crewQuery.Where(entry => !string.IsNullOrEmpty(entry.CrewMember.ProfilePath)); |
| | 130 | | } |
| | 131 | |
|
| | 132 | | foreach (var entry in crewQuery.Take(config.MaxCrewMembers)) |
| | 133 | | { |
| | 134 | | var crewMember = entry.CrewMember; |
| | 135 | |
|
| | 136 | | if (string.IsNullOrWhiteSpace(crewMember.Name)) |
| | 137 | | { |
| | 138 | | continue; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | var personInfo = new PersonInfo |
| | 142 | | { |
| | 143 | | Name = crewMember.Name.Trim(), |
| | 144 | | Role = crewMember.Job?.Trim() ?? string.Empty, |
| | 145 | | Type = entry.PersonType, |
| | 146 | | ImageUrl = _tmdbClientManager.GetProfileUrl(crewMember.ProfilePath) |
| | 147 | | }; |
| | 148 | |
|
| | 149 | | if (crewMember.Id > 0) |
| | 150 | | { |
| | 151 | | personInfo.SetProviderId(MetadataProvider.Tmdb, crewMember.Id.ToString(CultureInfo.InvariantCult |
| | 152 | | } |
| | 153 | |
|
| | 154 | | result.AddPerson(personInfo); |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| | 158 | | return result; |
| | 159 | | } |
| | 160 | |
|
| | 161 | | /// <inheritdoc /> |
| | 162 | | public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeasonInfo searchInfo, CancellationToken cancellat |
| | 163 | | { |
| 0 | 164 | | return Task.FromResult(Enumerable.Empty<RemoteSearchResult>()); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | /// <inheritdoc /> |
| | 168 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | 169 | | { |
| 0 | 170 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | 171 | | } |
| | 172 | | } |
| | 173 | | } |