| | | 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 | | var config = Plugin.Instance.Configuration; |
| | | 85 | | |
| | | 86 | | // Allowing this will dramatically increase scan times |
| | | 87 | | if (info.IsMissingEpisode) |
| | | 88 | | { |
| | | 89 | | return metadataResult; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | info.SeriesProviderIds.TryGetValue(MetadataProvider.Tmdb.ToString(), out string? tmdbId); |
| | | 93 | | |
| | | 94 | | var seriesTmdbId = Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture); |
| | | 95 | | if (seriesTmdbId <= 0) |
| | | 96 | | { |
| | | 97 | | return metadataResult; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | var seasonNumber = info.ParentIndexNumber ?? 1; |
| | | 101 | | var episodeNumber = info.IndexNumber; |
| | | 102 | | |
| | | 103 | | if (!episodeNumber.HasValue) |
| | | 104 | | { |
| | | 105 | | return metadataResult; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | TvEpisode? episodeResult = null; |
| | | 109 | | if (info.IndexNumberEnd.HasValue) |
| | | 110 | | { |
| | | 111 | | var startindex = episodeNumber; |
| | | 112 | | var endindex = info.IndexNumberEnd; |
| | | 113 | | List<TvEpisode>? result = null; |
| | | 114 | | for (int? episode = startindex; episode <= endindex; episode++) |
| | | 115 | | { |
| | | 116 | | var episodeInfo = await _tmdbClientManager.GetEpisodeAsync(seriesTmdbId, seasonNumber, episode.Value |
| | | 117 | | if (episodeInfo is not null) |
| | | 118 | | { |
| | | 119 | | (result ??= new List<TvEpisode>()).Add(episodeInfo); |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | if (result is not null) |
| | | 124 | | { |
| | | 125 | | // Forces a deep copy of the first TvEpisode, so we don't modify the original because it's cached |
| | | 126 | | episodeResult = new TvEpisode() |
| | | 127 | | { |
| | | 128 | | Name = result[0].Name, |
| | | 129 | | Overview = result[0].Overview, |
| | | 130 | | AirDate = result[0].AirDate, |
| | | 131 | | VoteAverage = result[0].VoteAverage, |
| | | 132 | | ExternalIds = result[0].ExternalIds, |
| | | 133 | | Videos = result[0].Videos, |
| | | 134 | | Credits = result[0].Credits |
| | | 135 | | }; |
| | | 136 | | |
| | | 137 | | if (result.Count > 1) |
| | | 138 | | { |
| | | 139 | | var name = new StringBuilder(episodeResult.Name); |
| | | 140 | | var overview = new StringBuilder(episodeResult.Overview); |
| | | 141 | | |
| | | 142 | | for (int i = 1; i < result.Count; i++) |
| | | 143 | | { |
| | | 144 | | name.Append(" / ").Append(result[i].Name); |
| | | 145 | | overview.Append(" / ").Append(result[i].Overview); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | episodeResult.Name = name.ToString(); |
| | | 149 | | episodeResult.Overview = overview.ToString(); |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | else |
| | | 153 | | { |
| | | 154 | | return metadataResult; |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | else |
| | | 158 | | { |
| | | 159 | | episodeResult = await _tmdbClientManager |
| | | 160 | | .GetEpisodeAsync(seriesTmdbId, seasonNumber, episodeNumber.Value, info.SeriesDisplayOrder, info.Meta |
| | | 161 | | .ConfigureAwait(false); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | if (episodeResult is null) |
| | | 165 | | { |
| | | 166 | | return metadataResult; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | metadataResult.HasMetadata = true; |
| | | 170 | | metadataResult.QueriedById = true; |
| | | 171 | | |
| | | 172 | | if (!string.IsNullOrEmpty(episodeResult.Overview)) |
| | | 173 | | { |
| | | 174 | | // if overview is non-empty, we can assume that localized data was returned |
| | | 175 | | metadataResult.ResultLanguage = info.MetadataLanguage; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | var item = new Episode |
| | | 179 | | { |
| | | 180 | | IndexNumber = info.IndexNumber, |
| | | 181 | | ParentIndexNumber = info.ParentIndexNumber, |
| | | 182 | | IndexNumberEnd = info.IndexNumberEnd, |
| | | 183 | | Name = episodeResult.Name, |
| | | 184 | | PremiereDate = episodeResult.AirDate, |
| | | 185 | | ProductionYear = episodeResult.AirDate?.Year, |
| | | 186 | | Overview = episodeResult.Overview, |
| | | 187 | | CommunityRating = Convert.ToSingle(episodeResult.VoteAverage) |
| | | 188 | | }; |
| | | 189 | | |
| | | 190 | | var externalIds = episodeResult.ExternalIds; |
| | | 191 | | item.TrySetProviderId(MetadataProvider.Tvdb, externalIds?.TvdbId); |
| | | 192 | | item.TrySetProviderId(MetadataProvider.Imdb, externalIds?.ImdbId); |
| | | 193 | | item.TrySetProviderId(MetadataProvider.TvRage, externalIds?.TvrageId); |
| | | 194 | | |
| | | 195 | | if (episodeResult.Videos?.Results is not null) |
| | | 196 | | { |
| | | 197 | | foreach (var video in episodeResult.Videos.Results) |
| | | 198 | | { |
| | | 199 | | if (TmdbUtils.IsTrailerType(video)) |
| | | 200 | | { |
| | | 201 | | item.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key); |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | var credits = episodeResult.Credits; |
| | | 207 | | |
| | | 208 | | if (credits?.Cast is not null) |
| | | 209 | | { |
| | | 210 | | var castQuery = config.HideMissingCastMembers |
| | | 211 | | ? credits.Cast.Where(a => !string.IsNullOrEmpty(a.ProfilePath)).OrderBy(a => a.Order) |
| | | 212 | | : credits.Cast.OrderBy(a => a.Order); |
| | | 213 | | |
| | | 214 | | foreach (var actor in castQuery.Take(config.MaxCastMembers)) |
| | | 215 | | { |
| | | 216 | | if (string.IsNullOrWhiteSpace(actor.Name)) |
| | | 217 | | { |
| | | 218 | | continue; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | var personInfo = new PersonInfo |
| | | 222 | | { |
| | | 223 | | Name = actor.Name.Trim(), |
| | | 224 | | Role = actor.Character?.Trim() ?? string.Empty, |
| | | 225 | | Type = PersonKind.Actor, |
| | | 226 | | SortOrder = actor.Order, |
| | | 227 | | ImageUrl = _tmdbClientManager.GetProfileUrl(actor.ProfilePath) |
| | | 228 | | }; |
| | | 229 | | |
| | | 230 | | if (actor.Id > 0) |
| | | 231 | | { |
| | | 232 | | personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture)) |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | metadataResult.AddPerson(personInfo); |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | if (credits?.GuestStars is not null) |
| | | 240 | | { |
| | | 241 | | var guestQuery = config.HideMissingCastMembers |
| | | 242 | | ? credits.GuestStars.Where(a => !string.IsNullOrEmpty(a.ProfilePath)).OrderBy(a => a.Order) |
| | | 243 | | : credits.GuestStars.OrderBy(a => a.Order); |
| | | 244 | | |
| | | 245 | | foreach (var guest in guestQuery.Take(config.MaxCastMembers)) |
| | | 246 | | { |
| | | 247 | | if (string.IsNullOrWhiteSpace(guest.Name)) |
| | | 248 | | { |
| | | 249 | | continue; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | var personInfo = new PersonInfo |
| | | 253 | | { |
| | | 254 | | Name = guest.Name.Trim(), |
| | | 255 | | Role = guest.Character?.Trim() ?? string.Empty, |
| | | 256 | | Type = PersonKind.GuestStar, |
| | | 257 | | SortOrder = guest.Order, |
| | | 258 | | ImageUrl = _tmdbClientManager.GetProfileUrl(guest.ProfilePath) |
| | | 259 | | }; |
| | | 260 | | |
| | | 261 | | if (guest.Id > 0) |
| | | 262 | | { |
| | | 263 | | personInfo.SetProviderId(MetadataProvider.Tmdb, guest.Id.ToString(CultureInfo.InvariantCulture)) |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | metadataResult.AddPerson(personInfo); |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | if (credits?.Crew is not null) |
| | | 271 | | { |
| | | 272 | | var crewQuery = credits.Crew |
| | | 273 | | .Select(crewMember => new |
| | | 274 | | { |
| | | 275 | | CrewMember = crewMember, |
| | | 276 | | PersonType = TmdbUtils.MapCrewToPersonType(crewMember) |
| | | 277 | | }) |
| | | 278 | | .Where(entry => |
| | | 279 | | TmdbUtils.WantedCrewKinds.Contains(entry.PersonType) || |
| | | 280 | | TmdbUtils.WantedCrewTypes.Contains(entry.CrewMember.Job ?? string.Empty, StringComparison.Ordina |
| | | 281 | | |
| | | 282 | | if (config.HideMissingCrewMembers) |
| | | 283 | | { |
| | | 284 | | crewQuery = crewQuery.Where(entry => !string.IsNullOrEmpty(entry.CrewMember.ProfilePath)); |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | foreach (var entry in crewQuery.Take(config.MaxCrewMembers)) |
| | | 288 | | { |
| | | 289 | | var crewMember = entry.CrewMember; |
| | | 290 | | |
| | | 291 | | if (string.IsNullOrWhiteSpace(crewMember.Name)) |
| | | 292 | | { |
| | | 293 | | continue; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | var personInfo = new PersonInfo |
| | | 297 | | { |
| | | 298 | | Name = crewMember.Name.Trim(), |
| | | 299 | | Role = crewMember.Job?.Trim() ?? string.Empty, |
| | | 300 | | Type = entry.PersonType, |
| | | 301 | | ImageUrl = _tmdbClientManager.GetProfileUrl(crewMember.ProfilePath) |
| | | 302 | | }; |
| | | 303 | | |
| | | 304 | | if (crewMember.Id > 0) |
| | | 305 | | { |
| | | 306 | | personInfo.SetProviderId(MetadataProvider.Tmdb, crewMember.Id.ToString(CultureInfo.InvariantCult |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | metadataResult.AddPerson(personInfo); |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | metadataResult.Item = item; |
| | | 314 | | |
| | | 315 | | return metadataResult; |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <inheritdoc /> |
| | | 319 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 320 | | { |
| | 0 | 321 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | | 322 | | } |
| | | 323 | | } |
| | | 324 | | } |