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); } }
public static void AttemptDownload(object state) { try { Dictionary <SubtitleDownloader, List <SubtitleInfo> > subtitleDownloadInfo = new Dictionary <SubtitleDownloader, List <SubtitleInfo> >(); int totalSubtitlesFound = 0; string movieFilePath = state as string; if (!string.IsNullOrEmpty(movieFilePath)) { SetCurrentSubtitle(movieFilePath, string.Empty); lock (__filesInProgress) { __filesInProgress.Add(movieFilePath.ToLowerInvariant()); // This is for enforcing playlist refresh EventDispatch.DispatchEvent(LocalEvents.UpdatePlaylistNames, false); } Logger.LogTrace("A subtitle was not found for this movie. Attempting to download one ..."); try { string[] subtitleDownloadURIs = StringUtils.ToStringArray(ProTONEConfig.SubtitleDownloadURIs, '\\'); if (subtitleDownloadURIs != null) { int prio = 1; foreach (string subtitleDownloadURI in subtitleDownloadURIs) { SubtitleDownloader sd = null; List <SubtitleInfo> foundSubtitles = null; try { sd = SubtitleDownloader.FromDownloadURI(subtitleDownloadURI); if (sd.IsActive) { sd.Priority = prio++; foundSubtitles = sd.GetSubtitles(movieFilePath); } } catch (Exception ex) { Logger.LogException(ex); FireNotify(Translator.Translate("TXT_SUB_DOWNLOAD_ERROR"), Translator.Translate("TXT_ERROR"), MessageBoxIcon.Warning); } finally { if (sd != null) { if (foundSubtitles != null && foundSubtitles.Count > 0) { subtitleDownloadInfo.Add(sd, foundSubtitles); totalSubtitlesFound += foundSubtitles.Count; } } } } } if (totalSubtitlesFound < 1) { // No subtitles found, give message and exit FireNotify(Translator.Translate("TXT_NO_SUBS_FOUND"), Translator.Translate("TXT_CAUTION"), MessageBoxIcon.Warning); return; } else { MainThread.Post(delegate(object x) { SubtitleDownloadNotifyForm dlg = new SubtitleDownloadNotifyForm(movieFilePath, subtitleDownloadInfo); dlg.SubtitleDownloadNotify += new SubtitleDownloadNotifyHandler(dlg_SubtitleDownloadNotify); dlg.Show(); //dlg.CenterToScreen(); }); } // Perform cleanup foreach (SubtitleDownloader sd in subtitleDownloadInfo.Keys) { if (sd != null) { sd.Dispose(); } } } finally { lock (__filesInProgress) { __filesInProgress.Remove(movieFilePath.ToLowerInvariant()); // This is for enforcing playlist refresh EventDispatch.DispatchEvent(LocalEvents.UpdatePlaylistNames, false); } } } } finally { // This is for enforcing playlist refresh EventDispatch.DispatchEvent(LocalEvents.UpdatePlaylistNames, false); } }
void SubtitleDownloadNotifyForm_Load(object sender, EventArgs e) { foreach (SubtitleDownloader sd in _subtitleDownloadInfo.Keys) { if (sd.Priority < _highestPrio) _highestPrio = sd.Priority; } foreach (KeyValuePair<SubtitleDownloader, List<SubtitleInfo>> kvp in _subtitleDownloadInfo) { SubtitleDownloader sd = kvp.Key; List<SubtitleInfo> list = kvp.Value; if (sd != null && list != null) { foreach (SubtitleInfo si in list) { string[] data = new string[] { string.IsNullOrEmpty(si.SubFileName) ? none : si.SubFileName, string.IsNullOrEmpty(sd.ServerUrl) ? none : sd.ServerUrl, sd.Priority.ToString(), string.IsNullOrEmpty(si.LanguageName) ? none : si.LanguageName, string.IsNullOrEmpty(si.SubSize) ? none : si.SubSize }; ListViewItem item = new ListViewItem(data); SubtitleDownloadInfo sdi = new SubtitleDownloadInfo(); sdi.sd = sd; sdi.si = si; item.Tag = sdi; lvSubtitles.Items.Add(item); //item.UseItemStyleForSubItems = true; if (SubtitleLanguage.IsPrefferedLanguage(si.LanguageName)) { item.ForeColor = ThemeManager.HighlightColor; foreach (ListViewItem.ListViewSubItem lvsi in item.SubItems) { lvsi.ForeColor = ThemeManager.HighlightColor; } } if (_highestPrio == sd.Priority) { item.Font = ThemeManager.SmallFont; foreach (ListViewItem.ListViewSubItem lvsi in item.SubItems) { lvsi.Font = ThemeManager.SmallFont; } } } } } int count = lvSubtitles.Items.Count; string movieName = string.Format("'{0}'", Path.GetFileName(_movieFilePath)); lvSubtitles.ListViewItemSorter = new SubtitleFormComparer(Columns.Language, _sortOrderParam); lvSubtitles.Sort(); SetTitle(Translator.Translate("TXT_SUBTITLEDOWNLOADNOTIFY", count, movieName)); }