| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Net.Http; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Jellyfin.Data.Enums; |
| | 10 | | using Jellyfin.Extensions; |
| | 11 | | using MediaBrowser.Common.Net; |
| | 12 | | using MediaBrowser.Controller.Entities; |
| | 13 | | using MediaBrowser.Controller.Entities.TV; |
| | 14 | | using MediaBrowser.Controller.Providers; |
| | 15 | | using MediaBrowser.Model.Entities; |
| | 16 | | using MediaBrowser.Model.Providers; |
| | 17 | | using TMDbLib.Objects.TvShows; |
| | 18 | |
|
| | 19 | | namespace MediaBrowser.Providers.Plugins.Tmdb.TV |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// TV episode provider powered by TheMovieDb. |
| | 23 | | /// </summary> |
| | 24 | | public class TmdbEpisodeProvider : IRemoteMetadataProvider<Episode, EpisodeInfo>, IHasOrder |
| | 25 | | { |
| | 26 | | private readonly IHttpClientFactory _httpClientFactory; |
| | 27 | | private readonly TmdbClientManager _tmdbClientManager; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of the <see cref="TmdbEpisodeProvider"/> class. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param> |
| | 33 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | 34 | | public TmdbEpisodeProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager) |
| | 35 | | { |
| 21 | 36 | | _httpClientFactory = httpClientFactory; |
| 21 | 37 | | _tmdbClientManager = tmdbClientManager; |
| 21 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| 0 | 41 | | public int Order => 1; |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| 0 | 44 | | public string Name => TmdbUtils.ProviderName; |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| | 47 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken ca |
| | 48 | | { |
| | 49 | | // The search query must either provide an episode number or date |
| | 50 | | if (!searchInfo.IndexNumber.HasValue) |
| | 51 | | { |
| | 52 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | var metadataResult = await GetMetadata(searchInfo, cancellationToken).ConfigureAwait(false); |
| | 56 | |
|
| | 57 | | if (!metadataResult.HasMetadata) |
| | 58 | | { |
| | 59 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | var item = metadataResult.Item; |
| | 63 | |
|
| | 64 | | return new[] |
| | 65 | | { |
| | 66 | | new RemoteSearchResult |
| | 67 | | { |
| | 68 | | IndexNumber = item.IndexNumber, |
| | 69 | | Name = item.Name, |
| | 70 | | ParentIndexNumber = item.ParentIndexNumber, |
| | 71 | | PremiereDate = item.PremiereDate, |
| | 72 | | ProductionYear = item.ProductionYear, |
| | 73 | | ProviderIds = item.ProviderIds, |
| | 74 | | SearchProviderName = Name, |
| | 75 | | IndexNumberEnd = item.IndexNumberEnd |
| | 76 | | } |
| | 77 | | }; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationToken cancellationToken) |
| | 82 | | { |
| | 83 | | var metadataResult = new MetadataResult<Episode>(); |
| | 84 | |
|
| | 85 | | // Allowing this will dramatically increase scan times |
| | 86 | | if (info.IsMissingEpisode) |
| | 87 | | { |
| | 88 | | return metadataResult; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | info.SeriesProviderIds.TryGetValue(MetadataProvider.Tmdb.ToString(), out string? tmdbId); |
| | 92 | |
|
| | 93 | | var seriesTmdbId = Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture); |
| | 94 | | if (seriesTmdbId <= 0) |
| | 95 | | { |
| | 96 | | return metadataResult; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | var seasonNumber = info.ParentIndexNumber ?? 1; |
| | 100 | | var episodeNumber = info.IndexNumber; |
| | 101 | |
|
| | 102 | | if (!episodeNumber.HasValue) |
| | 103 | | { |
| | 104 | | return metadataResult; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | TvEpisode? episodeResult = null; |
| | 108 | | if (info.IndexNumberEnd.HasValue) |
| | 109 | | { |
| | 110 | | var startindex = episodeNumber; |
| | 111 | | var endindex = info.IndexNumberEnd; |
| | 112 | | List<TvEpisode>? result = null; |
| | 113 | | for (int? episode = startindex; episode <= endindex; episode++) |
| | 114 | | { |
| | 115 | | var episodeInfo = await _tmdbClientManager.GetEpisodeAsync(seriesTmdbId, seasonNumber, episode.Value |
| | 116 | | if (episodeInfo is not null) |
| | 117 | | { |
| | 118 | | (result ??= new List<TvEpisode>()).Add(episodeInfo); |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | if (result is not null) |
| | 123 | | { |
| | 124 | | // Forces a deep copy of the first TvEpisode, so we don't modify the original because it's cached |
| | 125 | | episodeResult = new TvEpisode() |
| | 126 | | { |
| | 127 | | Name = result[0].Name, |
| | 128 | | Overview = result[0].Overview, |
| | 129 | | AirDate = result[0].AirDate, |
| | 130 | | VoteAverage = result[0].VoteAverage, |
| | 131 | | ExternalIds = result[0].ExternalIds, |
| | 132 | | Videos = result[0].Videos, |
| | 133 | | Credits = result[0].Credits |
| | 134 | | }; |
| | 135 | |
|
| | 136 | | if (result.Count > 1) |
| | 137 | | { |
| | 138 | | var name = new StringBuilder(episodeResult.Name); |
| | 139 | | var overview = new StringBuilder(episodeResult.Overview); |
| | 140 | |
|
| | 141 | | for (int i = 1; i < result.Count; i++) |
| | 142 | | { |
| | 143 | | name.Append(" / ").Append(result[i].Name); |
| | 144 | | overview.Append(" / ").Append(result[i].Overview); |
| | 145 | | } |
| | 146 | |
|
| | 147 | | episodeResult.Name = name.ToString(); |
| | 148 | | episodeResult.Overview = overview.ToString(); |
| | 149 | | } |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| | 153 | | return metadataResult; |
| | 154 | | } |
| | 155 | | } |
| | 156 | | else |
| | 157 | | { |
| | 158 | | episodeResult = await _tmdbClientManager |
| | 159 | | .GetEpisodeAsync(seriesTmdbId, seasonNumber, episodeNumber.Value, info.SeriesDisplayOrder, info.Meta |
| | 160 | | .ConfigureAwait(false); |
| | 161 | | } |
| | 162 | |
|
| | 163 | | if (episodeResult is null) |
| | 164 | | { |
| | 165 | | return metadataResult; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | metadataResult.HasMetadata = true; |
| | 169 | | metadataResult.QueriedById = true; |
| | 170 | |
|
| | 171 | | if (!string.IsNullOrEmpty(episodeResult.Overview)) |
| | 172 | | { |
| | 173 | | // if overview is non-empty, we can assume that localized data was returned |
| | 174 | | metadataResult.ResultLanguage = info.MetadataLanguage; |
| | 175 | | } |
| | 176 | |
|
| | 177 | | var item = new Episode |
| | 178 | | { |
| | 179 | | IndexNumber = info.IndexNumber, |
| | 180 | | ParentIndexNumber = info.ParentIndexNumber, |
| | 181 | | IndexNumberEnd = info.IndexNumberEnd, |
| | 182 | | Name = episodeResult.Name, |
| | 183 | | PremiereDate = episodeResult.AirDate, |
| | 184 | | ProductionYear = episodeResult.AirDate?.Year, |
| | 185 | | Overview = episodeResult.Overview, |
| | 186 | | CommunityRating = Convert.ToSingle(episodeResult.VoteAverage) |
| | 187 | | }; |
| | 188 | |
|
| | 189 | | var externalIds = episodeResult.ExternalIds; |
| | 190 | | item.TrySetProviderId(MetadataProvider.Tvdb, externalIds?.TvdbId); |
| | 191 | | item.TrySetProviderId(MetadataProvider.Imdb, externalIds?.ImdbId); |
| | 192 | | item.TrySetProviderId(MetadataProvider.TvRage, externalIds?.TvrageId); |
| | 193 | |
|
| | 194 | | if (episodeResult.Videos?.Results is not null) |
| | 195 | | { |
| | 196 | | foreach (var video in episodeResult.Videos.Results) |
| | 197 | | { |
| | 198 | | if (TmdbUtils.IsTrailerType(video)) |
| | 199 | | { |
| | 200 | | item.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key); |
| | 201 | | } |
| | 202 | | } |
| | 203 | | } |
| | 204 | |
|
| | 205 | | var credits = episodeResult.Credits; |
| | 206 | |
|
| | 207 | | if (credits?.Cast is not null) |
| | 208 | | { |
| | 209 | | foreach (var actor in credits.Cast.OrderBy(a => a.Order).Take(Plugin.Instance.Configuration.MaxCastMembe |
| | 210 | | { |
| | 211 | | metadataResult.AddPerson(new PersonInfo |
| | 212 | | { |
| | 213 | | Name = actor.Name.Trim(), |
| | 214 | | Role = actor.Character.Trim(), |
| | 215 | | Type = PersonKind.Actor, |
| | 216 | | SortOrder = actor.Order |
| | 217 | | }); |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| | 221 | | if (credits?.GuestStars is not null) |
| | 222 | | { |
| | 223 | | foreach (var guest in credits.GuestStars.OrderBy(a => a.Order).Take(Plugin.Instance.Configuration.MaxCas |
| | 224 | | { |
| | 225 | | metadataResult.AddPerson(new PersonInfo |
| | 226 | | { |
| | 227 | | Name = guest.Name.Trim(), |
| | 228 | | Role = guest.Character.Trim(), |
| | 229 | | Type = PersonKind.GuestStar, |
| | 230 | | SortOrder = guest.Order |
| | 231 | | }); |
| | 232 | | } |
| | 233 | | } |
| | 234 | |
|
| | 235 | | // and the rest from crew |
| | 236 | | if (credits?.Crew is not null) |
| | 237 | | { |
| | 238 | | foreach (var person in credits.Crew) |
| | 239 | | { |
| | 240 | | // Normalize this |
| | 241 | | var type = TmdbUtils.MapCrewToPersonType(person); |
| | 242 | |
|
| | 243 | | if (!TmdbUtils.WantedCrewKinds.Contains(type) |
| | 244 | | && !TmdbUtils.WantedCrewTypes.Contains(person.Job ?? string.Empty, StringComparison.OrdinalIgnor |
| | 245 | | { |
| | 246 | | continue; |
| | 247 | | } |
| | 248 | |
|
| | 249 | | metadataResult.AddPerson(new PersonInfo |
| | 250 | | { |
| | 251 | | Name = person.Name.Trim(), |
| | 252 | | Role = person.Job?.Trim(), |
| | 253 | | Type = type |
| | 254 | | }); |
| | 255 | | } |
| | 256 | | } |
| | 257 | |
|
| | 258 | | metadataResult.Item = item; |
| | 259 | |
|
| | 260 | | return metadataResult; |
| | 261 | | } |
| | 262 | |
|
| | 263 | | /// <inheritdoc /> |
| | 264 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | 265 | | { |
| 0 | 266 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | 267 | | } |
| | 268 | | } |
| | 269 | | } |