示例#1
0
        /// <inheritdoc />
        public async Task <IEnumerable <RemoteImageInfo> > GetImages(BaseItem item, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("[GetImages] {name}", item.Name);
                var episode = (Episode)item;
                var series  = episode.Series;
                if (series == null)
                {
                    // Episode or series is null.
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                var tvMazeId = TvHelpers.GetTvMazeId(episode.Series.ProviderIds);
                if (tvMazeId == null)
                {
                    // Requires series TVMaze id.
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                if (episode.IndexNumber == null || episode.ParentIndexNumber == null)
                {
                    // Missing episode or season number.
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                var tvMazeClient  = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default));
                var tvMazeEpisode = await tvMazeClient.Shows.GetEpisodeByNumberAsync(tvMazeId.Value, episode.ParentIndexNumber.Value, episode.IndexNumber.Value).ConfigureAwait(false);

                if (tvMazeEpisode == null)
                {
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                var imageResults = new List <RemoteImageInfo>();
                if (tvMazeEpisode.Image?.Original != null)
                {
                    imageResults.Add(new RemoteImageInfo
                    {
                        Url          = tvMazeEpisode.Image.Original,
                        ProviderName = TvMazePlugin.ProviderName,
                        Language     = "en",
                        Type         = ImageType.Primary
                    });
                }

                _logger.LogInformation("[GetImages] Images found for {name}: {@images}", item.Name, imageResults);
                return(imageResults);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "[GetImages]");
                return(Enumerable.Empty <RemoteImageInfo>());
            }
        }
        /// <inheritdoc />
        public async Task <IEnumerable <RemoteImageInfo> > GetImages(BaseItem item, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("[GetImages] {name}", item.Name);
                var series   = (Series)item;
                var tvMazeId = TvHelpers.GetTvMazeId(series.ProviderIds);
                if (tvMazeId == null)
                {
                    // Requires series tv maze id.
                    _logger.LogWarning("[GetImages] tv maze id is required;");
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                var tvMazeClient = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default));
                var images       = await tvMazeClient.Shows.GetShowImagesAsync(tvMazeId.Value).ConfigureAwait(false);

                if (images == null)
                {
                    _logger.LogDebug("[GetImages] No images found.");
                    return(Enumerable.Empty <RemoteImageInfo>());
                }

                var imageResults = new List <RemoteImageInfo>();
                // Order by type, then by Main=true
                foreach (var image in images.OrderBy(o => o.Type).ThenByDescending(o => o.Main))
                {
                    if (image.Resolutions.Original != null && image.Type.HasValue)
                    {
                        imageResults.Add(new RemoteImageInfo
                        {
                            Url          = image.Resolutions.Original.Url,
                            ProviderName = TvMazePlugin.ProviderName,
                            Language     = "en",
                            Type         = GetImageType(image.Type.Value)
                        });
                    }
                }

                _logger.LogInformation("[GetImages] Images found for {name}: {@images}", item.Name, imageResults);
                return(imageResults);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "[GetImages]");
                return(Enumerable.Empty <RemoteImageInfo>());
            }
        }
        private async Task <Episode?> GetMetadataInternal(EpisodeInfo info)
        {
            var tvMazeId = TvHelpers.GetTvMazeId(info.SeriesProviderIds);

            if (!tvMazeId.HasValue)
            {
                // Requires a TVMaze id.
                return(null);
            }

            // The search query must provide an episode number.
            if (!info.IndexNumber.HasValue || !info.ParentIndexNumber.HasValue)
            {
                return(null);
            }

            var tvMazeClient  = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy());
            var tvMazeEpisode = await tvMazeClient.Shows.GetEpisodeByNumberAsync(tvMazeId.Value, info.ParentIndexNumber.Value, info.IndexNumber.Value).ConfigureAwait(false);

            if (tvMazeEpisode == null)
            {
                // No episode found.
                return(null);
            }

            var episode = new Episode
            {
                Name              = tvMazeEpisode.Name,
                IndexNumber       = tvMazeEpisode.Number,
                ParentIndexNumber = tvMazeEpisode.Season
            };

            if (DateTime.TryParse(tvMazeEpisode.AirDate, out var airDate))
            {
                episode.PremiereDate = airDate;
            }

            if (tvMazeEpisode.Runtime.HasValue)
            {
                episode.RunTimeTicks = TimeSpan.FromTicks(tvMazeEpisode.Runtime.Value).Ticks;
            }

            episode.Overview = TvHelpers.GetStrippedHtml(tvMazeEpisode.Summary);
            episode.SetProviderId(TvMazePlugin.ProviderId, tvMazeEpisode.Id.ToString(CultureInfo.InvariantCulture));

            return(episode);
        }
示例#4
0
        private async Task <Season?> GetSeasonInternal(SeasonInfo info)
        {
            var tvMazeId = TvHelpers.GetTvMazeId(info.SeriesProviderIds);

            if (tvMazeId == null)
            {
                // Requires series TVMaze id.
                return(null);
            }

            var tvMazeClient  = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy());
            var tvMazeSeasons = await tvMazeClient.Shows.GetShowSeasonsAsync(tvMazeId.Value).ConfigureAwait(false);

            if (tvMazeSeasons == null)
            {
                return(null);
            }

            foreach (var tvMazeSeason in tvMazeSeasons)
            {
                if (tvMazeSeason.Number == info.IndexNumber)
                {
                    var season = new Season
                    {
                        Name        = tvMazeSeason.Name,
                        IndexNumber = tvMazeSeason.Number
                    };

                    if (DateTime.TryParse(tvMazeSeason.PremiereDate, out var premiereDate))
                    {
                        season.PremiereDate   = premiereDate;
                        season.ProductionYear = premiereDate.Year;
                    }

                    season.SetProviderId(TvMazePlugin.ProviderId, tvMazeSeason.Id.ToString(CultureInfo.InvariantCulture));
                    return(season);
                }
            }

            // Season not found.
            return(null);
        }
示例#5
0
        private async Task <IEnumerable <RemoteImageInfo> > GetSeasonImagesInternal(IHasProviderIds series, int seasonNumber)
        {
            var tvMazeId = TvHelpers.GetTvMazeId(series.ProviderIds);

            if (tvMazeId == null)
            {
                // Requires series TVMaze id.
                return(Enumerable.Empty <RemoteImageInfo>());
            }

            var tvMazeClient  = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy());
            var tvMazeSeasons = await tvMazeClient.Shows.GetShowSeasonsAsync(tvMazeId.Value).ConfigureAwait(false);

            if (tvMazeSeasons == null)
            {
                return(Enumerable.Empty <RemoteImageInfo>());
            }

            var imageResults = new List <RemoteImageInfo>();

            foreach (var tvMazeSeason in tvMazeSeasons)
            {
                if (tvMazeSeason.Number == seasonNumber)
                {
                    if (tvMazeSeason.Image?.Original != null)
                    {
                        imageResults.Add(new RemoteImageInfo
                        {
                            Url          = tvMazeSeason.Image.Original,
                            ProviderName = TvMazePlugin.ProviderName,
                            Language     = "en",
                            Type         = ImageType.Primary
                        });
                    }

                    break;
                }
            }

            return(imageResults);
        }
        /// <inheritdoc />
        public async Task <IEnumerable <RemoteSearchResult> > GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("[GetSearchResults] Starting for {Name} {SeasonNumber}x{EpisodeNumber}", searchInfo.Name, searchInfo.ParentIndexNumber, searchInfo.IndexNumber);
                var results = new List <RemoteSearchResult>();

                var tvMazeId = TvHelpers.GetTvMazeId(searchInfo.SeriesProviderIds);
                if (!tvMazeId.HasValue)
                {
                    // Requires a TVMaze id.
                    return(results);
                }

                var episode = await GetMetadataInternal(searchInfo).ConfigureAwait(false);

                if (episode != null)
                {
                    results.Add(new RemoteSearchResult
                    {
                        IndexNumber        = episode.IndexNumber,
                        Name               = episode.Name,
                        ParentIndexNumber  = episode.ParentIndexNumber,
                        PremiereDate       = episode.PremiereDate,
                        ProductionYear     = episode.ProductionYear,
                        ProviderIds        = episode.ProviderIds,
                        SearchProviderName = Name,
                        IndexNumberEnd     = episode.IndexNumberEnd
                    });
                }

                _logger.LogDebug("[GetSearchResults] Results for {Name}: {@Episode}", searchInfo.Name, results);
                return(results);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "[GetSearchResults]");
                return(Enumerable.Empty <RemoteSearchResult>());
            }
        }
示例#7
0
        /// <inheritdoc />
        public async Task <MetadataResult <Series> > GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogDebug("[GetMetadata] Starting for {Name}", info.Name);
                var tvMazeClient = new TvMazeClient(_httpClientFactory.CreateClient(NamedClient.Default), new RetryRateLimitingStrategy());
                var result       = new MetadataResult <Series>();

                var  tvMazeId   = TvHelpers.GetTvMazeId(info.ProviderIds);
                Show?tvMazeShow = null;
                if (tvMazeId.HasValue)
                {
                    // Search by TVMaze id.
                    tvMazeShow = await tvMazeClient.Shows.GetShowMainInformationAsync(tvMazeId.Value).ConfigureAwait(false);
                }

                if (tvMazeShow == null &&
                    info.ProviderIds.TryGetValue(MetadataProvider.Imdb.ToString(), out var imdbId) &&
                    !string.IsNullOrEmpty(imdbId))
                {
                    // Lookup by imdb id.
                    tvMazeShow = await tvMazeClient.Lookup.GetShowByImdbIdAsync(imdbId).ConfigureAwait(false);
                }

                if (tvMazeShow == null &&
                    info.ProviderIds.TryGetValue(MetadataProvider.TvRage.ToString(), out var tvRageId) &&
                    !string.IsNullOrEmpty(tvRageId))
                {
                    // Lookup by tv rage id.
                    var id = Convert.ToInt32(tvRageId, CultureInfo.InvariantCulture);
                    tvMazeShow = await tvMazeClient.Lookup.GetShowByTvRageIdAsync(id).ConfigureAwait(false);
                }

                if (tvMazeShow == null &&
                    info.ProviderIds.TryGetValue(MetadataProvider.Tvdb.ToString(), out var tvdbId) &&
                    !string.IsNullOrEmpty(tvdbId))
                {
                    var id = Convert.ToInt32(tvdbId, CultureInfo.InvariantCulture);
                    tvMazeShow = await tvMazeClient.Lookup.GetShowByTheTvdbIdAsync(id).ConfigureAwait(false);
                }

                if (tvMazeShow == null)
                {
                    // Series still not found, search by name.
                    var parsedName = _libraryManager.ParseName(info.Name);
                    _logger.LogDebug("[GetMetadata] No TVMaze Id, searching by parsed name: {@Name}", parsedName);
                    tvMazeShow = await GetIdentifyShow(parsedName, tvMazeClient).ConfigureAwait(false);
                }

                if (tvMazeShow == null)
                {
                    // Invalid TVMaze id.
                    _logger.LogDebug("[GetMetadata] No TVMaze result found for {Name}", info.Name);
                    return(result);
                }

                var series = new Series();
                series.Name   = tvMazeShow.Name;
                series.Genres = tvMazeShow.Genres.ToArray();

                if (!string.IsNullOrWhiteSpace(tvMazeShow.Network?.Name))
                {
                    var networkName = tvMazeShow.Network.Name;
                    if (!string.IsNullOrWhiteSpace(tvMazeShow.Network?.Country?.Code))
                    {
                        networkName = $"{tvMazeShow.Network.Name} ({tvMazeShow.Network.Country.Code})";
                    }

                    series.Studios = new[] { networkName };
                }

                if (DateTime.TryParse(tvMazeShow.Premiered, out var premiereDate))
                {
                    series.PremiereDate   = premiereDate;
                    series.ProductionYear = premiereDate.Year;
                }

                if (tvMazeShow.Rating?.Average != null)
                {
                    series.CommunityRating = (float?)tvMazeShow.Rating.Average;
                }

                if (tvMazeShow.Runtime.HasValue)
                {
                    series.RunTimeTicks = TimeSpan.FromMinutes(tvMazeShow.Runtime.Value).Ticks;
                }

                if (string.Equals(tvMazeShow.Status, "Running", StringComparison.OrdinalIgnoreCase))
                {
                    series.Status = SeriesStatus.Continuing;
                }
                else if (string.Equals(tvMazeShow.Status, "Ended", StringComparison.OrdinalIgnoreCase))
                {
                    series.Status = SeriesStatus.Ended;
                }

                series.Overview    = TvHelpers.GetStrippedHtml(tvMazeShow.Summary);
                series.HomePageUrl = tvMazeShow.Url;
                SetProviderIds(tvMazeShow, series);

                // Set cast.
                var castMembers = await tvMazeClient.Shows.GetShowCastAsync(tvMazeShow.Id).ConfigureAwait(false);

                foreach (var castMember in castMembers)
                {
                    var personInfo = new PersonInfo();
                    personInfo.SetProviderId(TvMazePlugin.ProviderId, castMember.Person.Id.ToString(CultureInfo.InvariantCulture));
                    personInfo.Name     = castMember.Person.Name;
                    personInfo.Role     = castMember.Character.Name;
                    personInfo.Type     = PersonType.Actor;
                    personInfo.ImageUrl = castMember.Person.Image?.Original
                                          ?? castMember.Person.Image?.Medium;

                    result.AddPerson(personInfo);
                }

                result.Item        = series;
                result.HasMetadata = true;

                _logger.LogDebug("[GetMetadata] Metadata result: {@series}", tvMazeShow);
                return(result);
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, "[GetMetadata]");
                return(new MetadataResult <Series>());
            }
        }