private List<SubtitleItem> QueryDownloaders(BasicMediaDetail mediaDetail, Dictionary<string, CustomSubtitleDownloader> downloaders, ref List<SubtitleItem> allResults) { SubtitleDownloader.Core.EpisodeSearchQuery episodeQuery = null; SubtitleDownloader.Core.SearchQuery movieQuery = null; SubtitleDownloader.Core.ImdbSearchQuery queryIMDB = null; logger.Info("Searching for subtitles..."); SubtitlesSearchType subtitlesSearchType = _searchType; //SubCentralUtils.getSubtitlesSearchTypeFromMediaDetail(mediaDetail); switch (subtitlesSearchType) { case SubtitlesSearchType.IMDb: logger.Debug("...using IMDb query: '{0}'", mediaDetail.ImdbID); queryIMDB = new SubtitleDownloader.Core.ImdbSearchQuery(mediaDetail.ImdbID); queryIMDB.LanguageCodes = _languageCodes.ToArray(); if (SubCentralUtils.canSearchMediaDetailWithType(mediaDetail, SubtitlesSearchType.MOVIE)) { logger.Debug(" ...additional movie query: '{0} ({1})'", mediaDetail.Title, mediaDetail.Year); movieQuery = new SubtitleDownloader.Core.SearchQuery(mediaDetail.Title); movieQuery.Year = mediaDetail.Year; movieQuery.LanguageCodes = _languageCodes.ToArray(); } break; case SubtitlesSearchType.TVSHOW: logger.Debug("...using TV show query: '{0} {1}x{2:00} TvdbId: {3}", mediaDetail.Title, mediaDetail.SeasonProper, mediaDetail.EpisodeProper, mediaDetail.TvdbId); episodeQuery = new SubtitleDownloader.Core.EpisodeSearchQuery(mediaDetail.Title, mediaDetail.SeasonProper, mediaDetail.EpisodeProper, mediaDetail.TvdbId); episodeQuery.LanguageCodes = _languageCodes.ToArray(); break; case SubtitlesSearchType.MOVIE: logger.Debug("...using movie query: '{0} ({1})'", mediaDetail.Title, mediaDetail.Year); movieQuery = new SubtitleDownloader.Core.SearchQuery(mediaDetail.Title); movieQuery.Year = mediaDetail.Year; movieQuery.LanguageCodes = _languageCodes.ToArray(); break; case SubtitlesSearchType.NONE: return allResults; } int providerCount = 1; foreach (KeyValuePair<string, CustomSubtitleDownloader> kvp in downloaders) { if (IsCanceled()) { //allResults.Clear(); break; } SubtitleDownloader.Core.ISubtitleDownloader subsDownloader = kvp.Value.downloader; string providerName = kvp.Value.downloaderTitle; subtitlesSearchType = _searchType; //reset search type //testing SearchTimeout subsDownloader.SearchTimeout = SettingsManager.Properties.GeneralSettings.SearchTimeout; List<SubtitleDownloader.Core.Subtitle> resultsFromDownloader = null; int percent = (100 / downloaders.Count) * providerCount; logger.Debug("Searching site {0}...", providerName); OnProgress(Localization.QueryingProviders + " (" + Convert.ToString(providerCount) + "/" + Convert.ToString(downloaders.Count) + "):", providerName, Localization.FoundSubtitles + ": " + Convert.ToString(allResults.Count), percent); try { switch (subtitlesSearchType) { case SubtitlesSearchType.IMDb: bool shouldThrowNotSupportedException = false; try { resultsFromDownloader = subsDownloader.SearchSubtitles(queryIMDB); if (resultsFromDownloader.Count == 0 && movieQuery != null) { List<SubtitleDownloader.Core.Subtitle> resultsFromDownloaderForMovieQuery = subsDownloader.SearchSubtitles(movieQuery); if (resultsFromDownloaderForMovieQuery.Count > 0) { // means that the site does not support imdb queries, should throw later logger.Error("Site {0} does not support IMDb ID search, got the results using regular movie search", providerName); resultsFromDownloader.AddRange(resultsFromDownloaderForMovieQuery); shouldThrowNotSupportedException = true; } } } catch (Exception e) { if (e is NotImplementedException || e is NotSupportedException) { if (movieQuery != null) { logger.Error("Site {0} does not support IMDb ID search, will try regular movie search", providerName); try { resultsFromDownloader = subsDownloader.SearchSubtitles(movieQuery); } catch (Exception e1) { // if the error happens now, we're probably searching some sites that support only tv shows? // so instead of returning not supported, we're gonna return no results // perhaps in the future, new exception type should be thrown to indicade that site does not support movie search // 19.06.2010, SE: it's the future! :) if (e1 is NotImplementedException || e1 is NotSupportedException) { subtitlesSearchType = SubtitlesSearchType.MOVIE; throw new NotSupportedException(string.Format("Site {0} does not support movie search!", providerName)); } else { throw e1; } } } else { //logger.Error("Site {0} does not support IMDb ID search", providerName); throw new NotSupportedException(string.Format("Site {0} does not support IMDb ID search!", providerName)); } } else { // throw it! throw e; } } if (shouldThrowNotSupportedException) { // meh, do not throw, since we already have results and it's logged //throw new NotSupportedException("Site {0} does not support IMDb ID search"); } break; case SubtitlesSearchType.TVSHOW: try { resultsFromDownloader = subsDownloader.SearchSubtitles(episodeQuery); } catch (Exception e) { if (e is NotImplementedException || e is NotSupportedException) { throw new NotSupportedException(string.Format("Site {0} does not support TV Show search!", providerName)); } else { throw e; } } break; case SubtitlesSearchType.MOVIE: try { resultsFromDownloader = subsDownloader.SearchSubtitles(movieQuery); } catch (Exception e) { if (e is NotImplementedException || e is NotSupportedException) { throw new NotSupportedException(string.Format("Site {0} does not support movie search!", providerName)); } else { throw e; } } break; } } catch (ThreadAbortException e) { throw e; } catch (Exception e) { if (e.InnerException != null && e.InnerException is ThreadAbortException) { throw e.InnerException; } if (e is NotSupportedException) { logger.Error(e.Message); } else { logger.ErrorException(string.Format("Error while querying site {0}{1}", providerName, Environment.NewLine), e); } if (OnProviderSearchErrorEvent != null) { if (GUIGraphicsContext.form.InvokeRequired) { OnProviderSearchErrorDelegate d = OnProviderSearchErrorEvent; GUIGraphicsContext.form.Invoke(d, mediaDetail, subtitlesSearchType, e); } else { OnProviderSearchErrorEvent(mediaDetail, subtitlesSearchType, e); } } } if (resultsFromDownloader != null && resultsFromDownloader.Count > 0) { foreach (SubtitleDownloader.Core.Subtitle subtitle in resultsFromDownloader) { if (!subtitle.FileName.IsNullOrWhiteSpace() && !string.IsNullOrEmpty(subtitle.LanguageCode)) { SubtitleItem subItem = new SubtitleItem(); subItem.Downloader = subsDownloader; subItem.Subtitle = subtitle; subItem.ProviderTitle = kvp.Key; subItem.LanguageName = subtitle.LanguageCode; if (!string.IsNullOrEmpty(SubCentralUtils.SubsLanguages[subtitle.LanguageCode])) subItem.LanguageName = SubCentralUtils.SubsLanguages[subtitle.LanguageCode]; allResults.Add(subItem); } } } providerCount++; } return allResults; }
private void PerformDownload(SubtitleItem subtitleItem, bool skipDefaults) { if (CurrentHandler == null) return; FileInfo fileInfo = null; if (CurrentHandler.MediaDetail.Files != null && CurrentHandler.MediaDetail.Files.Count > 0) fileInfo = CurrentHandler.MediaDetail.Files[0]; List<FolderSelectionItem> items = SubCentralUtils.getEnabledAndValidFoldersForMedia(fileInfo, true); if (items == null || items.Count == 0) { GUIUtils.ShowNotifyDialog(Localization.Warning, Localization.NoDownloadFolders); return; } SubtitlesSearchType searchType = getRealCurrentSearchSearchType(CurrentHandler.MediaDetail); bool readOnly = true; bool create = false; int selectedFolderIndex = -1; bool selectedFromDefault = false; while (readOnly || !create) { selectedFolderIndex = -1; if (!selectedFromDefault && !skipDefaults && Settings.SettingsManager.Properties.FolderSettings.OnDownload == OnDownload.DefaultFolders) { for (int i = 0; i < items.Count; i++) { FolderSelectionItem folderSelectionItem = items[i]; if ((searchType == SubtitlesSearchType.IMDb || searchType == SubtitlesSearchType.MOVIE) && folderSelectionItem.DefaultForMovies) { selectedFolderIndex = i; selectedFromDefault = true; break; } else if (searchType == SubtitlesSearchType.TVSHOW && folderSelectionItem.DefaultForTVShows) { selectedFolderIndex = i; selectedFromDefault = true; break; } } } if (selectedFolderIndex < 0) { if (!skipDefaults && Settings.SettingsManager.Properties.FolderSettings.OnDownload == OnDownload.DefaultFolders) { logger.Info("Default folder for media type {0} could not be found, manually input folder for download", searchType.ToString()); } selectedFolderIndex = SubCentralUtils.ShowFolderMenuDialog(Localization.SelectDownloadFolder, items, selectedFolderIndex); } if (selectedFolderIndex >= 0) { readOnly = false; create = true; string folderName = items[selectedFolderIndex].FolderName; if (items[selectedFolderIndex].FolderErrorInfo == FolderErrorInfo.ReadOnly) { readOnly = true; if (selectedFromDefault) { logger.Info("Default folder {0} for media type {1} is not writable, manually input folder for download", folderName, searchType.ToString()); GUIUtils.ShowNotifyDialog(Localization.Warning, string.Format(Localization.DefaultFolderNotWritable, folderName)); } } else if (items[selectedFolderIndex].FolderErrorInfo == FolderErrorInfo.Inaccessible) { readOnly = true; if (selectedFromDefault) { logger.Info("Default folder {0} for media type {1} is inaccessible, manually input folder for download", folderName, searchType.ToString()); GUIUtils.ShowNotifyDialog(Localization.Warning, string.Format(Localization.DefaultFolderInaccessible, folderName)); } } else if (items[selectedFolderIndex].FolderErrorInfo == FolderErrorInfo.NonExistant) { bool dlgResult = false; if (selectedFromDefault) { logger.Info("Default folder {0} for media type {1} does not exist", folderName, searchType.ToString()); dlgResult = GUIUtils.ShowYesNoDialog(Localization.Confirm, string.Format(Localization.DefaultDownloadFoderDoesNotExistCreate, folderName), true); } else { dlgResult = GUIUtils.ShowYesNoDialog(Localization.Confirm, Localization.DownloadFoderDoesNotExistCreate, true); } if (dlgResult) { try { Directory.CreateDirectory(folderName); } catch (Exception e) { logger.ErrorException(string.Format("Error creating directory {0}{1}", folderName, Environment.NewLine), e); create = false; GUIUtils.ShowOKDialog(Localization.Error, Localization.ErrorWhileCreatingDirectory); } } else { create = false; } } } else { break; } } if (selectedFolderIndex >= 0) { retriever.FillData(false, null, null, SubtitlesSearchType.NONE ); retriever.DownloadSubtitle(subtitleItem, CurrentHandler.MediaDetail, items[selectedFolderIndex], searchType, skipDefaults); } }
public void DownloadSubtitle(SubtitleItem subtitleItem, BasicMediaDetail mediaDetail, FolderSelectionItem folderSelectionItem, SubtitlesSearchType searchType, bool skipDefaults) { if (IsCanceled()) Kill(); if (_subtitlesDownloaderThread != null && _subtitlesDownloaderThread.IsAlive) return; _isCanceled = false; _status = ThreadStatus.StatusEnded; DownloadData downloadData = new DownloadData { SubtitleItem = subtitleItem, MediaDetail = mediaDetail, FolderSelectionItem = folderSelectionItem, SearchType = searchType, SkipDefaults = skipDefaults, StatusList = new List<SubtitleDownloadStatus>() }; _subtitlesDownloaderThread = new Thread(DownloadSubtitleAsync); _subtitlesDownloaderThread.IsBackground = true; _subtitlesDownloaderThread.Name = "Subtitles Downloader Thread"; _subtitlesDownloaderThread.Start(downloadData); }
private void PerformDownload(SubtitleItem subtitleItem) { PerformDownload(subtitleItem, false); }