| | | 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.Library; |
| | | 14 | | using MediaBrowser.Controller.Providers; |
| | | 15 | | using MediaBrowser.Model.Entities; |
| | | 16 | | using MediaBrowser.Model.Providers; |
| | | 17 | | using TMDbLib.Objects.Find; |
| | | 18 | | using TMDbLib.Objects.Search; |
| | | 19 | | using TMDbLib.Objects.TvShows; |
| | | 20 | | |
| | | 21 | | namespace MediaBrowser.Providers.Plugins.Tmdb.TV |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// TV series provider powered by TheMovieDb. |
| | | 25 | | /// </summary> |
| | | 26 | | public class TmdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder |
| | | 27 | | { |
| | | 28 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 29 | | private readonly ILibraryManager _libraryManager; |
| | | 30 | | private readonly TmdbClientManager _tmdbClientManager; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="TmdbSeriesProvider"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param> |
| | | 36 | | /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param> |
| | | 37 | | /// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param> |
| | | 38 | | public TmdbSeriesProvider( |
| | | 39 | | ILibraryManager libraryManager, |
| | | 40 | | IHttpClientFactory httpClientFactory, |
| | | 41 | | TmdbClientManager tmdbClientManager) |
| | | 42 | | { |
| | 21 | 43 | | _libraryManager = libraryManager; |
| | 21 | 44 | | _httpClientFactory = httpClientFactory; |
| | 21 | 45 | | _tmdbClientManager = tmdbClientManager; |
| | 21 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 0 | 49 | | public string Name => TmdbUtils.ProviderName; |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 0 | 52 | | public int Order => 1; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken can |
| | | 56 | | { |
| | | 57 | | if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var tmdbId)) |
| | | 58 | | { |
| | | 59 | | var series = await _tmdbClientManager |
| | | 60 | | .GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, |
| | | 61 | | .ConfigureAwait(false); |
| | | 62 | | |
| | | 63 | | if (series is not null) |
| | | 64 | | { |
| | | 65 | | var remoteResult = MapTvShowToRemoteSearchResult(series); |
| | | 66 | | |
| | | 67 | | return new[] { remoteResult }; |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out var imdbId)) |
| | | 72 | | { |
| | | 73 | | var findResult = await _tmdbClientManager |
| | | 74 | | .FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, searchInfo.MetadataLanguage, searchInfo.Meta |
| | | 75 | | .ConfigureAwait(false); |
| | | 76 | | |
| | | 77 | | var tvResults = findResult?.TvResults; |
| | | 78 | | if (tvResults is not null) |
| | | 79 | | { |
| | | 80 | | var imdbIdResults = new RemoteSearchResult[tvResults.Count]; |
| | | 81 | | for (var i = 0; i < tvResults.Count; i++) |
| | | 82 | | { |
| | | 83 | | var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]); |
| | | 84 | | remoteResult.SetProviderId(MetadataProvider.Imdb, imdbId); |
| | | 85 | | imdbIdResults[i] = remoteResult; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | return imdbIdResults; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId)) |
| | | 93 | | { |
| | | 94 | | var findResult = await _tmdbClientManager |
| | | 95 | | .FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, searchInfo.MetadataLanguage, searchInfo.Meta |
| | | 96 | | .ConfigureAwait(false); |
| | | 97 | | |
| | | 98 | | var tvResults = findResult?.TvResults; |
| | | 99 | | if (tvResults is not null) |
| | | 100 | | { |
| | | 101 | | var tvIdResults = new RemoteSearchResult[tvResults.Count]; |
| | | 102 | | for (var i = 0; i < tvResults.Count; i++) |
| | | 103 | | { |
| | | 104 | | var remoteResult = MapSearchTvToRemoteSearchResult(tvResults[i]); |
| | | 105 | | remoteResult.SetProviderId(MetadataProvider.Tvdb, tvdbId); |
| | | 106 | | tvIdResults[i] = remoteResult; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | return tvIdResults; |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | var tvSearchResults = await _tmdbClientManager.SearchSeriesAsync(searchInfo.Name, searchInfo.MetadataLanguag |
| | | 114 | | .ConfigureAwait(false); |
| | | 115 | | |
| | | 116 | | var remoteResults = new RemoteSearchResult[tvSearchResults.Count]; |
| | | 117 | | for (var i = 0; i < tvSearchResults.Count; i++) |
| | | 118 | | { |
| | | 119 | | remoteResults[i] = MapSearchTvToRemoteSearchResult(tvSearchResults[i]); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | return remoteResults; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | private RemoteSearchResult MapTvShowToRemoteSearchResult(TvShow series) |
| | | 126 | | { |
| | 0 | 127 | | var remoteResult = new RemoteSearchResult |
| | 0 | 128 | | { |
| | 0 | 129 | | Name = series.Name ?? series.OriginalName, |
| | 0 | 130 | | SearchProviderName = Name, |
| | 0 | 131 | | ImageUrl = _tmdbClientManager.GetPosterUrl(series.PosterPath), |
| | 0 | 132 | | Overview = series.Overview |
| | 0 | 133 | | }; |
| | | 134 | | |
| | 0 | 135 | | remoteResult.SetProviderId(MetadataProvider.Tmdb, series.Id.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 136 | | if (series.ExternalIds is not null) |
| | | 137 | | { |
| | 0 | 138 | | remoteResult.TrySetProviderId(MetadataProvider.Imdb, series.ExternalIds.ImdbId); |
| | | 139 | | |
| | 0 | 140 | | remoteResult.TrySetProviderId(MetadataProvider.Tvdb, series.ExternalIds.TvdbId); |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | remoteResult.PremiereDate = series.FirstAirDate?.ToUniversalTime(); |
| | | 144 | | |
| | 0 | 145 | | return remoteResult; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private RemoteSearchResult MapSearchTvToRemoteSearchResult(SearchTv series) |
| | | 149 | | { |
| | 0 | 150 | | var remoteResult = new RemoteSearchResult |
| | 0 | 151 | | { |
| | 0 | 152 | | Name = series.Name ?? series.OriginalName, |
| | 0 | 153 | | SearchProviderName = Name, |
| | 0 | 154 | | ImageUrl = _tmdbClientManager.GetPosterUrl(series.PosterPath), |
| | 0 | 155 | | Overview = series.Overview |
| | 0 | 156 | | }; |
| | | 157 | | |
| | 0 | 158 | | remoteResult.SetProviderId(MetadataProvider.Tmdb, series.Id.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 159 | | remoteResult.PremiereDate = series.FirstAirDate?.ToUniversalTime(); |
| | | 160 | | |
| | 0 | 161 | | return remoteResult; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <inheritdoc /> |
| | | 165 | | public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken) |
| | | 166 | | { |
| | | 167 | | var result = new MetadataResult<Series> |
| | | 168 | | { |
| | | 169 | | QueriedById = true |
| | | 170 | | }; |
| | | 171 | | |
| | | 172 | | var tmdbId = info.GetProviderId(MetadataProvider.Tmdb); |
| | | 173 | | |
| | | 174 | | if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Imdb, out var imdbId)) |
| | | 175 | | { |
| | | 176 | | var searchResult = await _tmdbClientManager.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, info. |
| | | 177 | | if (searchResult?.TvResults.Count > 0) |
| | | 178 | | { |
| | | 179 | | tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | if (string.IsNullOrEmpty(tmdbId) && info.TryGetProviderId(MetadataProvider.Tvdb, out var tvdbId)) |
| | | 184 | | { |
| | | 185 | | var searchResult = await _tmdbClientManager.FindByExternalIdAsync(tvdbId, FindExternalSource.TvDb, info. |
| | | 186 | | if (searchResult?.TvResults.Count > 0) |
| | | 187 | | { |
| | | 188 | | tmdbId = searchResult.TvResults[0].Id.ToString(CultureInfo.InvariantCulture); |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | if (string.IsNullOrEmpty(tmdbId)) |
| | | 193 | | { |
| | | 194 | | result.QueriedById = false; |
| | | 195 | | // ParseName is required here. |
| | | 196 | | // Caller provides the filename with extension stripped and NOT the parsed filename |
| | | 197 | | var parsedName = _libraryManager.ParseName(info.Name); |
| | | 198 | | var cleanedName = TmdbUtils.CleanName(parsedName.Name); |
| | | 199 | | var searchResults = await _tmdbClientManager.SearchSeriesAsync(cleanedName, info.MetadataLanguage, info. |
| | | 200 | | |
| | | 201 | | if (searchResults.Count > 0) |
| | | 202 | | { |
| | | 203 | | tmdbId = searchResults[0].Id.ToString(CultureInfo.InvariantCulture); |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | if (!int.TryParse(tmdbId, CultureInfo.InvariantCulture, out int tmdbIdInt)) |
| | | 208 | | { |
| | | 209 | | return result; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 213 | | |
| | | 214 | | var tvShow = await _tmdbClientManager |
| | | 215 | | .GetSeriesAsync(tmdbIdInt, info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage |
| | | 216 | | .ConfigureAwait(false); |
| | | 217 | | |
| | | 218 | | if (tvShow is null) |
| | | 219 | | { |
| | | 220 | | return result; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | result = new MetadataResult<Series> |
| | | 224 | | { |
| | | 225 | | Item = MapTvShowToSeries(tvShow, info.MetadataCountryCode), |
| | | 226 | | ResultLanguage = info.MetadataLanguage ?? tvShow.OriginalLanguage |
| | | 227 | | }; |
| | | 228 | | |
| | | 229 | | foreach (var person in GetPersons(tvShow)) |
| | | 230 | | { |
| | | 231 | | result.AddPerson(person); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | result.HasMetadata = result.Item is not null; |
| | | 235 | | |
| | | 236 | | return result; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private static Series MapTvShowToSeries(TvShow seriesResult, string preferredCountryCode) |
| | | 240 | | { |
| | 0 | 241 | | var series = new Series |
| | 0 | 242 | | { |
| | 0 | 243 | | Name = seriesResult.Name, |
| | 0 | 244 | | OriginalTitle = seriesResult.OriginalName |
| | 0 | 245 | | }; |
| | | 246 | | |
| | 0 | 247 | | series.SetProviderId(MetadataProvider.Tmdb, seriesResult.Id.ToString(CultureInfo.InvariantCulture)); |
| | | 248 | | |
| | 0 | 249 | | series.CommunityRating = Convert.ToSingle(seriesResult.VoteAverage); |
| | | 250 | | |
| | 0 | 251 | | series.Overview = seriesResult.Overview; |
| | | 252 | | |
| | 0 | 253 | | if (seriesResult.Networks is not null) |
| | | 254 | | { |
| | 0 | 255 | | series.Studios = seriesResult.Networks.Select(i => i.Name).ToArray(); |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | if (seriesResult.Genres is not null) |
| | | 259 | | { |
| | 0 | 260 | | series.Genres = seriesResult.Genres.Select(i => i.Name).ToArray(); |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | if (seriesResult.Keywords?.Results is not null) |
| | | 264 | | { |
| | 0 | 265 | | for (var i = 0; i < seriesResult.Keywords.Results.Count; i++) |
| | | 266 | | { |
| | 0 | 267 | | series.AddTag(seriesResult.Keywords.Results[i].Name); |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | series.HomePageUrl = seriesResult.Homepage; |
| | | 272 | | |
| | 0 | 273 | | series.RunTimeTicks = seriesResult.EpisodeRunTime.Select(i => TimeSpan.FromMinutes(i).Ticks).FirstOrDefault( |
| | | 274 | | |
| | 0 | 275 | | if (Emby.Naming.TV.TvParserHelpers.TryParseSeriesStatus(seriesResult.Status, out var seriesStatus)) |
| | | 276 | | { |
| | 0 | 277 | | series.Status = seriesStatus; |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | series.EndDate = seriesResult.LastAirDate; |
| | 0 | 281 | | series.PremiereDate = seriesResult.FirstAirDate; |
| | | 282 | | |
| | 0 | 283 | | var ids = seriesResult.ExternalIds; |
| | 0 | 284 | | if (ids is not null) |
| | | 285 | | { |
| | 0 | 286 | | series.TrySetProviderId(MetadataProvider.Imdb, ids.ImdbId); |
| | 0 | 287 | | series.TrySetProviderId(MetadataProvider.TvRage, ids.TvrageId); |
| | 0 | 288 | | series.TrySetProviderId(MetadataProvider.Tvdb, ids.TvdbId); |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | var contentRatings = seriesResult.ContentRatings.Results ?? new List<ContentRating>(); |
| | | 292 | | |
| | 0 | 293 | | var ourRelease = contentRatings.FirstOrDefault(c => string.Equals(c.Iso_3166_1, preferredCountryCode, String |
| | 0 | 294 | | var usRelease = contentRatings.FirstOrDefault(c => string.Equals(c.Iso_3166_1, "US", StringComparison.Ordina |
| | 0 | 295 | | var minimumRelease = contentRatings.FirstOrDefault(); |
| | | 296 | | |
| | 0 | 297 | | if (ourRelease is not null) |
| | | 298 | | { |
| | 0 | 299 | | series.OfficialRating = TmdbUtils.BuildParentalRating(ourRelease.Iso_3166_1, ourRelease.Rating); |
| | | 300 | | } |
| | 0 | 301 | | else if (usRelease is not null) |
| | | 302 | | { |
| | 0 | 303 | | series.OfficialRating = usRelease.Rating; |
| | | 304 | | } |
| | 0 | 305 | | else if (minimumRelease is not null) |
| | | 306 | | { |
| | 0 | 307 | | series.OfficialRating = minimumRelease.Rating; |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | if (seriesResult.Videos?.Results is not null) |
| | | 311 | | { |
| | 0 | 312 | | foreach (var video in seriesResult.Videos.Results) |
| | | 313 | | { |
| | 0 | 314 | | if (TmdbUtils.IsTrailerType(video)) |
| | | 315 | | { |
| | 0 | 316 | | series.AddTrailerUrl("https://www.youtube.com/watch?v=" + video.Key); |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | return series; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | private IEnumerable<PersonInfo> GetPersons(TvShow seriesResult) |
| | | 325 | | { |
| | | 326 | | var config = Plugin.Instance.Configuration; |
| | | 327 | | |
| | | 328 | | if (seriesResult.Credits?.Cast is not null) |
| | | 329 | | { |
| | | 330 | | IEnumerable<Cast> castQuery = seriesResult.Credits.Cast.OrderBy(a => a.Order); |
| | | 331 | | |
| | | 332 | | if (config.HideMissingCastMembers) |
| | | 333 | | { |
| | | 334 | | castQuery = castQuery.Where(a => !string.IsNullOrEmpty(a.ProfilePath)); |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | foreach (var actor in castQuery.Take(config.MaxCastMembers)) |
| | | 338 | | { |
| | | 339 | | if (string.IsNullOrWhiteSpace(actor.Name)) |
| | | 340 | | { |
| | | 341 | | continue; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | var personInfo = new PersonInfo |
| | | 345 | | { |
| | | 346 | | Name = actor.Name.Trim(), |
| | | 347 | | Role = actor.Character?.Trim() ?? string.Empty, |
| | | 348 | | Type = PersonKind.Actor, |
| | | 349 | | SortOrder = actor.Order, |
| | | 350 | | ImageUrl = _tmdbClientManager.GetProfileUrl(actor.ProfilePath) |
| | | 351 | | }; |
| | | 352 | | |
| | | 353 | | if (actor.Id > 0) |
| | | 354 | | { |
| | | 355 | | personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture)) |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | yield return personInfo; |
| | | 359 | | } |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | if (seriesResult.Credits?.Crew is not null) |
| | | 363 | | { |
| | | 364 | | var crewQuery = seriesResult.Credits.Crew |
| | | 365 | | .Select(crewMember => new |
| | | 366 | | { |
| | | 367 | | CrewMember = crewMember, |
| | | 368 | | PersonType = TmdbUtils.MapCrewToPersonType(crewMember) |
| | | 369 | | }) |
| | | 370 | | .Where(entry => |
| | | 371 | | TmdbUtils.WantedCrewKinds.Contains(entry.PersonType) || |
| | | 372 | | TmdbUtils.WantedCrewTypes.Contains(entry.CrewMember.Job ?? string.Empty, StringComparison.Ordina |
| | | 373 | | |
| | | 374 | | if (config.HideMissingCrewMembers) |
| | | 375 | | { |
| | | 376 | | crewQuery = crewQuery.Where(entry => !string.IsNullOrEmpty(entry.CrewMember.ProfilePath)); |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | foreach (var entry in crewQuery.Take(config.MaxCrewMembers)) |
| | | 380 | | { |
| | | 381 | | var crewMember = entry.CrewMember; |
| | | 382 | | |
| | | 383 | | if (string.IsNullOrWhiteSpace(crewMember.Name)) |
| | | 384 | | { |
| | | 385 | | continue; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | var personInfo = new PersonInfo |
| | | 389 | | { |
| | | 390 | | Name = crewMember.Name.Trim(), |
| | | 391 | | Role = crewMember.Job?.Trim() ?? string.Empty, |
| | | 392 | | Type = entry.PersonType, |
| | | 393 | | ImageUrl = _tmdbClientManager.GetProfileUrl(crewMember.ProfilePath) |
| | | 394 | | }; |
| | | 395 | | |
| | | 396 | | if (crewMember.Id > 0) |
| | | 397 | | { |
| | | 398 | | personInfo.SetProviderId(MetadataProvider.Tmdb, crewMember.Id.ToString(CultureInfo.InvariantCult |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | yield return personInfo; |
| | | 402 | | } |
| | | 403 | | } |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | /// <inheritdoc /> |
| | | 407 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 408 | | { |
| | 0 | 409 | | return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken); |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | } |