コード例 #1
0
        private async Task DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
        {
            if ((options.DownloadEpisodeSubtitles &&
                 video is Episode) ||
                (options.DownloadMovieSubtitles &&
                 video is Movie))
            {
                var mediaStreams = video.GetMediaSources(false).First().MediaStreams;

                var downloadedLanguages = await new SubtitleDownloader(_logger,
                                                                       _subtitleManager)
                                          .DownloadSubtitles(video,
                                                             mediaStreams,
                                                             options.SkipIfGraphicalSubtitlesPresent,
                                                             options.SkipIfAudioTrackMatches,
                                                             options.DownloadLanguages,
                                                             cancellationToken).ConfigureAwait(false);

                // Rescan
                if (downloadedLanguages.Count > 0)
                {
                    await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
                }
            }
        }
コード例 #2
0
ファイル: SubtitleScheduledTask.cs プロジェクト: Ivan-L/Emby
        private async Task <bool> DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
        {
            if ((options.DownloadEpisodeSubtitles &&
                 video is Episode) ||
                (options.DownloadMovieSubtitles &&
                 video is Movie))
            {
                var mediaStreams = _mediaSourceManager.GetStaticMediaSources(video, false).First().MediaStreams;

                var downloadedLanguages = await new SubtitleDownloader(_logger,
                                                                       _subtitleManager)
                                          .DownloadSubtitles(video,
                                                             mediaStreams,
                                                             options.SkipIfEmbeddedSubtitlesPresent,
                                                             options.SkipIfAudioTrackMatches,
                                                             options.RequirePerfectMatch,
                                                             options.DownloadLanguages,
                                                             cancellationToken).ConfigureAwait(false);

                // Rescan
                if (downloadedLanguages.Count > 0)
                {
                    await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);

                    return(false);
                }

                return(true);
            }

            return(false);
        }
コード例 #3
0
        private async Task <SubtitleResponse> GetSubtitlesInternal(string id,
                                                                   SubtitleOptions options,
                                                                   CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            if (_dailyDownloadCount >= MaxDownloadsPerDay &&
                !options.IsOpenSubtitleVipAccount)
            {
                throw new InvalidOperationException("Open Subtitle's daily download limit has been exceeded. Please try again tomorrow.");
            }

            var idParts = id.Split(new[] { '-' }, 3);

            var format   = idParts[0];
            var language = idParts[1];
            var ossId    = idParts[2];

            var downloadsList = new[] { int.Parse(ossId, _usCulture) };

            await Login(cancellationToken).ConfigureAwait(false);

            var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);

            if (!(resultDownLoad is MethodResponseSubtitleDownload))
            {
                throw new ApplicationException("Invalid response type");
            }

            var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;

            if (results.Count == 0)
            {
                var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
                                        ossId,
                                        resultDownLoad.Name ?? string.Empty,
                                        resultDownLoad.Message ?? string.Empty,
                                        resultDownLoad.Status ?? string.Empty);

                throw new ResourceNotFoundException(msg);
            }

            var data = Convert.FromBase64String(results.First().Data);

            return(new SubtitleResponse
            {
                Format = format,
                Language = language,

                Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
            });
        }
コード例 #4
0
        private async Task <bool> DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
        {
            var mediaStreams = video.GetMediaStreams();

            var libraryOptions = _libraryManager.GetLibraryOptions(video);

            string[] subtitleDownloadLanguages;
            bool     skipIfEmbeddedSubtitlesPresent;
            bool     skipIfAudioTrackMatches;
            bool     requirePerfectMatch;

            if (libraryOptions.SubtitleDownloadLanguages == null)
            {
                subtitleDownloadLanguages      = options.DownloadLanguages;
                skipIfEmbeddedSubtitlesPresent = options.SkipIfEmbeddedSubtitlesPresent;
                skipIfAudioTrackMatches        = options.SkipIfAudioTrackMatches;
                requirePerfectMatch            = options.RequirePerfectMatch;
            }
            else
            {
                subtitleDownloadLanguages      = libraryOptions.SubtitleDownloadLanguages;
                skipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent;
                skipIfAudioTrackMatches        = libraryOptions.SkipSubtitlesIfAudioTrackMatches;
                requirePerfectMatch            = libraryOptions.RequirePerfectSubtitleMatch;
            }

            var downloadedLanguages = await new SubtitleDownloader(
                _logger,
                _subtitleManager).DownloadSubtitles(
                video,
                mediaStreams,
                skipIfEmbeddedSubtitlesPresent,
                skipIfAudioTrackMatches,
                requirePerfectMatch,
                subtitleDownloadLanguages,
                libraryOptions.DisabledSubtitleFetchers,
                libraryOptions.SubtitleFetcherOrder,
                true,
                cancellationToken).ConfigureAwait(false);

            // Rescan
            if (downloadedLanguages.Count > 0)
            {
                await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);

                return(false);
            }

            return(true);
        }
コード例 #5
0
        private async Task <SubtitleResponse> GetSubtitlesInternal(string id,
                                                                   SubtitleOptions options,
                                                                   CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            var idParts = id.Split(new[] { '-' }, 3);

            var format   = idParts[0];
            var language = idParts[1];
            var ossId    = idParts[2];

            var downloadsList = new[] { int.Parse(ossId, _usCulture) };

            await Login(cancellationToken).ConfigureAwait(false);

            if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1)
            {
                throw new ApplicationException("OpenSubtitles rate limit reached");
            }

            var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);

            if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1)
            {
                _lastRateLimitException = DateTime.UtcNow;
                throw new ApplicationException("OpenSubtitles rate limit reached");
            }

            if (!(resultDownLoad is MethodResponseSubtitleDownload))
            {
                throw new ApplicationException("Invalid response type");
            }

            var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;

            _lastRateLimitException = DateTime.MinValue;

            if (results.Count == 0)
            {
                var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
                                        ossId,
                                        resultDownLoad.Name ?? string.Empty,
                                        resultDownLoad.Status ?? string.Empty,
                                        resultDownLoad.Message ?? string.Empty);

                throw new ResourceNotFoundException(msg);
            }

            var data = Convert.FromBase64String(results.First().Data);

            return(new SubtitleResponse
            {
                Format = format,
                Language = language,

                Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
            });
        }
コード例 #6
0
        private async Task<SubtitleResponse> GetSubtitlesInternal(string id,
            SubtitleOptions options,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            if (_dailyDownloadCount >= MaxDownloadsPerDay &&
                !options.IsOpenSubtitleVipAccount)
            {
                throw new InvalidOperationException("Open Subtitle's daily download limit has been exceeded. Please try again tomorrow.");
            }

            var idParts = id.Split(new[] { '-' }, 3);

            var format = idParts[0];
            var language = idParts[1];
            var ossId = idParts[2];

            var downloadsList = new[] { int.Parse(ossId, _usCulture) };

            await Login(cancellationToken).ConfigureAwait(false);

            var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);

            if (!(resultDownLoad is MethodResponseSubtitleDownload))
            {
                throw new ApplicationException("Invalid response type");
            }

            var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;

            if (results.Count == 0)
            {
                var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
                    ossId,
                    resultDownLoad.Name ?? string.Empty,
                    resultDownLoad.Message ?? string.Empty,
                    resultDownLoad.Status ?? string.Empty);

                throw new ResourceNotFoundException(msg);
            }

            var data = Convert.FromBase64String(results.First().Data);

            return new SubtitleResponse
            {
                Format = format,
                Language = language,

                Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
            };
        }
コード例 #7
0
ファイル: OpenSubtitleDownloader.cs プロジェクト: dgz/Emby
        private async Task<SubtitleResponse> GetSubtitlesInternal(string id,
            SubtitleOptions options,
            CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            var idParts = id.Split(new[] { '-' }, 3);

            var format = idParts[0];
            var language = idParts[1];
            var ossId = idParts[2];

            var downloadsList = new[] { int.Parse(ossId, _usCulture) };

            await Login(cancellationToken).ConfigureAwait(false);

            if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1)
            {
                throw new ApplicationException("OpenSubtitles rate limit reached");
            }

            var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);

            if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1)
            {
                _lastRateLimitException = DateTime.UtcNow;
                throw new ApplicationException("OpenSubtitles rate limit reached");
            }

            if (!(resultDownLoad is MethodResponseSubtitleDownload))
            {
                throw new ApplicationException("Invalid response type");
            }

            var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;

            _lastRateLimitException = DateTime.MinValue;

            if (results.Count == 0)
            {
                var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
                    ossId,
                    resultDownLoad.Name ?? string.Empty,
                    resultDownLoad.Status ?? string.Empty,
                    resultDownLoad.Message ?? string.Empty);

                throw new ResourceNotFoundException(msg);
            }

            var data = Convert.FromBase64String(results.First().Data);

            return new SubtitleResponse
            {
                Format = format,
                Language = language,

                Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)))
            };
        }
コード例 #8
0
        private async Task DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
        {
            if ((options.DownloadEpisodeSubtitles &&
                video is Episode) ||
                (options.DownloadMovieSubtitles &&
                video is Movie))
            {
                var mediaStreams = video.GetMediaSources(false).First().MediaStreams;

                var downloadedLanguages = await new SubtitleDownloader(_logger,
                    _subtitleManager)
                    .DownloadSubtitles(video,
                    mediaStreams,
                    options.SkipIfGraphicalSubtitlesPresent,
                    options.SkipIfAudioTrackMatches,
                    options.DownloadLanguages,
                    cancellationToken).ConfigureAwait(false);

                // Rescan
                if (downloadedLanguages.Count > 0)
                {
                    await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
                }
            }
        }