Ejemplo n.º 1
0
        /// <summary>
        /// Gets the new path.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="series">The series.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="endingEpisodeNumber">The ending episode number.</param>
        /// <param name="premiereDate">The premiere date.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>System.String.</returns>
        private async Task <string> GetNewPath(string sourcePath,
                                               Series series,
                                               int?seasonNumber,
                                               int?episodeNumber,
                                               int?endingEpisodeNumber,
                                               DateTime?premiereDate,
                                               TvFileOrganizationOptions options,
                                               CancellationToken cancellationToken)
        {
            var episodeInfo = new EpisodeInfo
            {
                IndexNumber         = episodeNumber,
                IndexNumberEnd      = endingEpisodeNumber,
                MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
                MetadataLanguage    = series.GetPreferredMetadataLanguage(),
                ParentIndexNumber   = seasonNumber,
                SeriesProviderIds   = series.ProviderIds,
                PremiereDate        = premiereDate
            };

            var searchResults = await _providerManager.GetRemoteSearchResults <Episode, EpisodeInfo>(new RemoteSearchQuery <EpisodeInfo>
            {
                SearchInfo = episodeInfo
            }, cancellationToken).ConfigureAwait(false);

            var episode = searchResults.FirstOrDefault();

            if (episode == null)
            {
                var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
                _logger.Warn(msg);
                throw new Exception(msg);
            }

            var episodeName = episode.Name;

            //if (string.IsNullOrWhiteSpace(episodeName))
            //{
            //    var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
            //    _logger.Warn(msg);
            //    return null;
            //}

            seasonNumber  = seasonNumber ?? episode.ParentIndexNumber;
            episodeNumber = episodeNumber ?? episode.IndexNumber;

            var newPath = GetSeasonFolderPath(series, seasonNumber.Value, options);

            var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber.Value, episodeNumber.Value, endingEpisodeNumber, episodeName, options);

            if (string.IsNullOrEmpty(episodeFileName))
            {
                // cause failure
                return(string.Empty);
            }

            newPath = Path.Combine(newPath, episodeFileName);

            return(newPath);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Look up metadata for an episode and use it to create an <see cref="Episode"/> object.
        /// </summary>
        /// <param name="series">The series the episode is in.</param>
        /// <param name="seasonNumber">The season number the episode is in.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="endingEpisodeNumber">The ending episode number.</param>
        /// <param name="premiereDate">The premiere date.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A task representing the creation of the <see cref="Episode"/> object.</returns>
        /// <exception cref="OrganizationException">If no metadata can be found for the specified episode parameters.</exception>
        private async Task <Episode> CreateNewEpisode(
            Series series,
            int?seasonNumber,
            int?episodeNumber,
            int?endingEpisodeNumber,
            DateTime?premiereDate,
            CancellationToken cancellationToken)
        {
            var episodeInfo = new EpisodeInfo
            {
                IndexNumber         = episodeNumber,
                IndexNumberEnd      = endingEpisodeNumber,
                MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
                MetadataLanguage    = series.GetPreferredMetadataLanguage(),
                ParentIndexNumber   = seasonNumber,
                SeriesProviderIds   = series.ProviderIds,
                PremiereDate        = premiereDate
            };

            var searchResults = await _providerManager.GetRemoteSearchResults <Episode, EpisodeInfo>(
                new RemoteSearchQuery <EpisodeInfo> {
                SearchInfo = episodeInfo
            },
                cancellationToken).ConfigureAwait(false);

            var episodeSearch = searchResults.FirstOrDefault();

            if (episodeSearch == null)
            {
                var msg = $"No provider metadata found for {series.Name} season {seasonNumber} episode {episodeNumber}";
                _logger.LogWarning(msg);
                throw new OrganizationException(msg);
            }

            seasonNumber        = seasonNumber ?? episodeSearch.ParentIndexNumber;
            episodeNumber       = episodeNumber ?? episodeSearch.IndexNumber;
            endingEpisodeNumber = endingEpisodeNumber ?? episodeSearch.IndexNumberEnd;

            var episode = new Episode
            {
                ParentIndexNumber = seasonNumber,
                SeriesId          = series.Id,
                IndexNumber       = episodeNumber,
                IndexNumberEnd    = endingEpisodeNumber,
                ProviderIds       = episodeSearch.ProviderIds,
                Name = episodeSearch.Name,
            };

            return(episode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the new path.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="series">The series.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="endingEpisodeNumber">The ending episode number.</param>
        /// <param name="premiereDate">The premiere date.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>System.String.</returns>
        private async Task<string> GetNewPath(string sourcePath,
            Series series,
            int? seasonNumber,
            int? episodeNumber,
            int? endingEpisodeNumber,
            DateTime? premiereDate,
            TvFileOrganizationOptions options,
            CancellationToken cancellationToken)
        {
            var episodeInfo = new EpisodeInfo
            {
                IndexNumber = episodeNumber,
                IndexNumberEnd = endingEpisodeNumber,
                MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
                MetadataLanguage = series.GetPreferredMetadataLanguage(),
                ParentIndexNumber = seasonNumber,
                SeriesProviderIds = series.ProviderIds,
                PremiereDate = premiereDate
            };

            var searchResults = await _providerManager.GetRemoteSearchResults<Episode, EpisodeInfo>(new RemoteSearchQuery<EpisodeInfo>
            {
                SearchInfo = episodeInfo

            }, cancellationToken).ConfigureAwait(false);

            var episode = searchResults.FirstOrDefault();

            if (episode == null)
            {
                var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
                _logger.Warn(msg);
                return null;
            }

            var episodeName = episode.Name;

            //if (string.IsNullOrWhiteSpace(episodeName))
            //{
            //    var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
            //    _logger.Warn(msg);
            //    return null;
            //}

            seasonNumber = seasonNumber ?? episode.ParentIndexNumber;
            episodeNumber = episodeNumber ?? episode.IndexNumber;

            var newPath = GetSeasonFolderPath(series, seasonNumber.Value, options);

            // MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256
            // Usually newPath would include the drive component, but use 256 to be sure
            var maxFilenameLength = 256 - newPath.Length;

            if (!newPath.EndsWith(@"\"))
            {
                // Remove 1 for missing backslash combining path and filename
                maxFilenameLength--;
            }

            // Remove additional 4 chars to prevent PathTooLongException for downloaded subtitles (eg. filename.ext.eng.srt)
            maxFilenameLength -= 4;

            var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber.Value, episodeNumber.Value, endingEpisodeNumber, episodeName, options, maxFilenameLength);

            if (string.IsNullOrEmpty(episodeFileName))
            {
                // cause failure
                return string.Empty;
            }

            newPath = Path.Combine(newPath, episodeFileName);

            return newPath;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the new path.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="series">The series.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="endingEpisodeNumber">The ending episode number.</param>
        /// <param name="options">The options.</param>
        /// <returns>System.String.</returns>
        private async Task<string> GetNewPath(string sourcePath, Series series, int seasonNumber, int episodeNumber, int? endingEpisodeNumber, TvFileOrganizationOptions options, CancellationToken cancellationToken)
        {
            var episodeInfo = new EpisodeInfo
            {
                IndexNumber = episodeNumber,
                IndexNumberEnd = endingEpisodeNumber,
                MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
                MetadataLanguage = series.GetPreferredMetadataLanguage(),
                ParentIndexNumber = seasonNumber,
                SeriesProviderIds = series.ProviderIds
            };

            var searchResults = await _providerManager.GetRemoteSearchResults<Episode, EpisodeInfo>(new RemoteSearchQuery<EpisodeInfo>
            {
                SearchInfo = episodeInfo

            }, cancellationToken).ConfigureAwait(false);

            var episode = searchResults.FirstOrDefault();

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

            var newPath = GetSeasonFolderPath(series, seasonNumber, options);

            var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber, episodeNumber, endingEpisodeNumber, episode.Name, options);

            newPath = Path.Combine(newPath, episodeFileName);

            return newPath;
        }
Ejemplo n.º 5
0
        public static Task<Model.Epg> GetMetadata(Model.Epg epg, CancellationToken cancellationToken)
        {
            return Task.Run(async () =>
            {
                var seriesItem = new MediaBrowser.Controller.Entities.TV.Series();
                var seriesDataPath = "";
                var seriesId = "";
                var language = String.IsNullOrWhiteSpace(Plugin.Instance.Configuration.Provider) ? "en" : "pt";
                var country = String.IsNullOrWhiteSpace(Plugin.Instance.Configuration.Provider) ? "us" : "br";

                if (Plugin.Instance.Configuration.Provider == "CLARO" && (epg.IsMovie || epg.IsShopping || epg.IsSports || epg.IsNews)) return epg;

                try
                {
                    var seriesInfo = new SeriesInfo
                    {
                        Name = epg.Title,
                        ParentIndexNumber = epg.SeasonNumber,
                        IndexNumber = epg.EpisodeNumber,
                        MetadataLanguage = language,
                        MetadataCountryCode = country
                    };

                    if (Cache[epg.Title] == null)
                    {
                        MetadataResult<MediaBrowser.Controller.Entities.TV.Series> omdbSeries;

                        var omdbItemProvider = new OmdbItemProvider(LiveTvService.JsonSerializer,
                            LiveTvService.HttpClient, LiveTvService.Logger, LiveTvService.LibraryManager, LiveTvService.FileSystem, LiveTvService.Config);

                        omdbSeries = await omdbItemProvider.GetMetadata(seriesInfo, cancellationToken);

                        var imdbId = omdbSeries.Item.GetProviderId(MetadataProviders.Imdb);

                        if (!String.IsNullOrWhiteSpace(imdbId))
                        {
                            using (var result = await LiveTvService.HttpClient.Get(new HttpRequestOptions
                            {
                                Url = "http://www.thetvdb.com/api/GetSeriesByRemoteID.php?imdbid=" + imdbId  + "&language=" + language,
                                ResourcePool = new SemaphoreSlim(2, 2),
                                CancellationToken = cancellationToken

                            }).ConfigureAwait(false))
                            {
                                var doc = new XmlDocument();
                                doc.Load(result);

                                if (doc.HasChildNodes)
                                {
                                    var node = doc.SelectSingleNode("//Series/seriesid");

                                    if (node != null)
                                    {
                                        seriesId = node.InnerText;
                                    }
                                }
                            }

                            seriesInfo.ProviderIds = new Dictionary<string, string>
                            {
                                {
                                    MetadataProviders.Tvdb.ToString(),
                                    seriesId
                                },
                                {
                                    MetadataProviders.Imdb.ToString(),
                                    imdbId
                                }
                            };
                        }

                        var tvdbSeries = await TvdbSeriesProvider.Current.GetMetadata(seriesInfo, cancellationToken);

                        seriesItem = tvdbSeries.Item;

                        Cache.Add(epg.Title, seriesItem, DateTimeOffset.Now.AddDays(1));
                    }
                    else seriesItem = Cache[epg.Title] as MediaBrowser.Controller.Entities.TV.Series;

                    seriesId = seriesItem.GetProviderId(MetadataProviders.Tvdb);

                    seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(
                        LiveTvService.Config.ApplicationPaths,
                        new Dictionary<string, string>
                        {
                            {
                                MetadataProviders.Tvdb.ToString(),
                                seriesId
                            }
                        });

                    Logger.Debug("seriesDataPath: " + seriesDataPath);

                    foreach (var providerId in seriesItem.ProviderIds)
                    {
                        Logger.Debug("providerId: " + providerId);
                    }

                    epg.EpisodeId = seriesItem.Id.ToString();
                    epg.Etag = seriesItem.ExternalEtag;
                    epg.Name = !String.IsNullOrEmpty(seriesItem.Name) ? seriesItem.Name : epg.Title;
                    epg.Name = !String.IsNullOrEmpty(seriesItem.OriginalTitle) ? seriesItem.OriginalTitle : epg.Name;
                    epg.OriginalAirDate = seriesItem.PremiereDate;
                    epg.ProductionYear = seriesItem.ProductionYear;
                    epg.SeriesId = seriesId;
                    epg.Overview = seriesItem.Overview;
                    epg.Genres = seriesItem.Genres;
                    epg.OfficialRating = seriesItem.OfficialRating;
                    epg.CommunityRating = seriesItem.CommunityRating;
                    epg.ProviderIds = seriesItem.ProviderIds;

                    epg.IsSeries = true;
                    epg.IsMovie = false;
                    epg.IsSports = false;
                    epg.IsNews = false;
                    epg.IsKids = seriesItem.Genres.Any(a => a.Contains(Constant.Genre.Kids) || a.Contains(Constant.Genre.Children));
                    epg.IsSports = seriesItem.Genres.Any(a => a.Contains(Constant.Genre.Sports));
                    epg.IsRepeat = seriesItem.PremiereDate == null || seriesItem.PremiereDate < DateTime.UtcNow.AddDays(-7);
                }
                catch (Exception exception)
                {
                    //Logger.Error("Series provider error: " + exception.Message);
                    //Logger.ErrorException("Series provider error", exception);
                }

                try
                {
                    if (seriesItem != null && epg != null && epg.SeasonNumber > 0 && epg.EpisodeNumber > 0)
                    {
                        var episodeInfo = new EpisodeInfo
                        {
                            ParentIndexNumber = epg.SeasonNumber,
                            IndexNumber = epg.EpisodeNumber,
                            SeriesProviderIds = seriesItem.ProviderIds,
                            MetadataLanguage = seriesItem.GetPreferredMetadataLanguage(),
                            MetadataCountryCode = seriesItem.GetPreferredMetadataCountryCode()
                        };

                        var tvdbEpisode = await TvdbEpisodeProvider.Current.GetMetadata(episodeInfo, cancellationToken);
                        var episode = tvdbEpisode.Item;

                        if (episode != null)
                        {
                            epg.EpisodeId = episode.Id.ToString();
                            epg.Etag = episode.ExternalEtag;
                            epg.EpisodeTitle = episode.Name;
                            epg.OriginalAirDate = episode.PremiereDate;
                            epg.Overview = episode.Overview;
                            epg.ProviderIds = episode.ProviderIds;

                            epg.IsRepeat = episode.PremiereDate == null || episode.PremiereDate < DateTime.UtcNow.AddDays(-7);
                        }
                        else
                        {
                            Logger.Debug("episode null");
                        }
                    }
                }
                catch (Exception exception)
                {
                    //Logger.Error("Series episode error: " + exception.Message);
                    //Logger.ErrorException("Series episode provider error", exception);
                }

                try
                {
                    if (seriesItem != null && epg != null && !String.IsNullOrWhiteSpace(seriesId))
                    {
                        var imageProvider = new FanartSeriesProvider(LiveTvService.Config, LiveTvService.HttpClient, LiveTvService.FileSystem, LiveTvService.JsonSerializer);

                        var images = await imageProvider.GetImages(seriesItem, cancellationToken);

                        if (images.Count(c => c.Type == ImageType.Primary) == 0)
                        {
                            var tvdbSeriesImageProvider = new TvdbSeriesImageProvider(LiveTvService.Config, LiveTvService.HttpClient, LiveTvService.FileSystem);

                            images = await tvdbSeriesImageProvider.GetImages(seriesItem, cancellationToken);
                        }

                        var image = images.OrderByDescending(o => o.VoteCount).FirstOrDefault(q => q.Type == ImageType.Primary && q.Language == language);

                        epg.ImageUrl = image != null ? image.Url : null;
                    }
                }
                catch (Exception exception)
                {
                    //Logger.Error("Series image error: " + exception.Message);
                    //Logger.ErrorException("Series image provider error", exception);
                }

                return epg;
            }, cancellationToken);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the new path.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="series">The series.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="endingEpisodeNumber">The ending episode number.</param>
        /// <param name="premiereDate">The premiere date.</param>
        /// <param name="options">The options.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>System.String.</returns>
        private async Task <string> GetNewPath(string sourcePath,
                                               Series series,
                                               int?seasonNumber,
                                               int?episodeNumber,
                                               int?endingEpisodeNumber,
                                               DateTime?premiereDate,
                                               TvFileOrganizationOptions options,
                                               CancellationToken cancellationToken)
        {
            var episodeInfo = new EpisodeInfo
            {
                IndexNumber         = episodeNumber,
                IndexNumberEnd      = endingEpisodeNumber,
                MetadataCountryCode = series.GetPreferredMetadataCountryCode(),
                MetadataLanguage    = series.GetPreferredMetadataLanguage(),
                ParentIndexNumber   = seasonNumber,
                SeriesProviderIds   = series.ProviderIds,
                PremiereDate        = premiereDate
            };

            var searchResults = await _providerManager.GetRemoteSearchResults <Episode, EpisodeInfo>(new RemoteSearchQuery <EpisodeInfo>
            {
                SearchInfo = episodeInfo
            }, cancellationToken).ConfigureAwait(false);

            var episode = searchResults.FirstOrDefault();

            if (episode == null)
            {
                var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
                _logger.Warn(msg);
                throw new Exception(msg);
            }

            var episodeName = episode.Name;

            //if (string.IsNullOrWhiteSpace(episodeName))
            //{
            //    var msg = string.Format("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber);
            //    _logger.Warn(msg);
            //    return null;
            //}

            seasonNumber  = seasonNumber ?? episode.ParentIndexNumber;
            episodeNumber = episodeNumber ?? episode.IndexNumber;

            var newPath = GetSeasonFolderPath(series, seasonNumber.Value, options);

            // MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256
            // Usually newPath would include the drive component, but use 256 to be sure
            var maxFilenameLength = 256 - newPath.Length;

            if (!newPath.EndsWith(@"\"))
            {
                // Remove 1 for missing backslash combining path and filename
                maxFilenameLength--;
            }

            // Remove additional 4 chars to prevent PathTooLongException for downloaded subtitles (eg. filename.ext.eng.srt)
            maxFilenameLength -= 4;

            var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber.Value, episodeNumber.Value, endingEpisodeNumber, episodeName, options, maxFilenameLength);

            if (string.IsNullOrEmpty(episodeFileName))
            {
                // cause failure
                return(string.Empty);
            }

            newPath = Path.Combine(newPath, episodeFileName);

            return(newPath);
        }