public void ProvideSubtitleForVideo(Video video)
    {
        try
        {

            var message = string.Format("Finding subtitle for {0}", video.Name);
            InformUser(message);

            var dataSource = DataSourceFactory.CreateDataSource();
            var blackListingProvider = new BlackListingProvider(video, dataSource);
            var subtitleSelector = new SubtitleSelector(blackListingProvider);

            var languageProvider = new LanguageProvider();
            var languages = languageProvider.CreateLanguageCollectionFromString(Plugin.PluginOptions.Instance.Languages);
            var finder = new RemoteSubtitleFinder(video, subtitleSelector);
            var subtitle = finder.FindSubtitle(languages, blackListingProvider);

            if (subtitle == null)
            {
                var failureMessage = string.Format("Downloading subtitle failed. No subtitle found for {0}", video.Name);
                InformUser(failureMessage);
                return;
            }

            var filePath = Path.Combine(ApplicationPaths.AppCachePath, Path.GetRandomFileName() + ".zip");

            var subtitleDownloader = new SubtitleDownloader();
            subtitleDownloader.GetSubtitleToPath(subtitle, filePath);

            var subtitleExtractorFactory = new SubtitleExtractorFactory();
            var subtitleExtractor = subtitleExtractorFactory.CreateSubtitleExtractorByVideo(video);

            try
            {
                subtitleExtractor.ExtractSubtitleFile(filePath);
            }
            catch (InvalidSubtitleFileException)
            {
                blackListingProvider.BlackList(subtitle);
                throw new Exception("Invalid subtitle file, blacklisting: " + subtitle.UrlToFile);
            }

            dataSource.SetCurrentSubtitle(video, subtitle);
            var successMessage = string.Format("Subtitle downloaded for {0} - {1}", video.Name, subtitle.Langugage);
            InformUser(successMessage);

        }
        catch (Exception ex)
        {
            var reportedError =
                string.Format("Error when getting subtitle for video: {0}.", video.Name);

            InformUser(reportedError);

            Logger.ReportException(reportedError, ex);
        }
    }
示例#2
0
        public RemoteSubtitleFinder(Video video, SubtitleSelector selector)
        {
            this.video    = video;
            this.selector = selector;

            if (sources == null)
            {
                sources = new List <SubtitleSourceBase>();
            }

            sources.Add(new OpenSubtitlesSubtitleSource());
            sources.Add(new SubtitleSource());
        }