Exemplo n.º 1
0
        public async Task <IList <DownloadedSubtitle> > SearchSubtitlesForTVAsync(IList <string> showNameCandidates,
                                                                                  int seasonNbr,
                                                                                  int episodeNbr,
                                                                                  IList <CultureInfo> cultureInfos,
                                                                                  System.Collections.Concurrent.ConcurrentQueue <ProviderSignal> cQueue)
        {
            _logger.LogTrace($"{GetCaller()}() entered");

            List <DownloadedSubtitle> downloadedSubs = new List <DownloadedSubtitle>();

            try
            {
                if (!_tvShows.Any())
                {
                    _logger.LogInformation("No TV shows available.");
                }
                else
                {
                    // Find our target show
                    bool foundShow = false;
                    UpdateTaskDisplay(cQueue, .1, "Searching for TV show...");

                    _logger.LogDebug($"Searching for subtitles for the following show names: ({String.Join<string>(", ", showNameCandidates).TrimEnd()})");

                    for (int x = 0; x < showNameCandidates.Count && !foundShow; x++)
                    {
                        string showName = showNameCandidates[x];

                        var myShow = _tvShows.FirstOrDefault(x => x.Name.Contains(showName, StringComparison.OrdinalIgnoreCase));

                        if (myShow == null)
                        {
                            _logger.LogDebug($"TV show variation ({x + 1}/{showNameCandidates.Count}) ({showName}) was not in the list of available shows.");
                        }
                        else
                        {
                            foundShow = true;

                            UpdateTaskDisplay(cQueue, .2, $"Matched to series ({showName}). Searching for subtitles for season ({seasonNbr})...");
                            _logger.LogDebug($"TV show variation ({x + 1}/{showNameCandidates.Count}) ({showName}) was found in the list of available shows.");

                            // Find all subtitles for each episode in the target season
                            List <Episode> eps = null;
                            string         SEASON_SUBTITLES_FILENAME = $"{showName}.s{seasonNbr}.cache";
                            IFormatter     formatter = new BinaryFormatter();
                            Stream         stream    = null;

                            if (File.Exists(SEASON_SUBTITLES_FILENAME))
                            {
                                // Load the season info from a file
                                stream = new FileStream(SEASON_SUBTITLES_FILENAME, FileMode.Open, FileAccess.Read);

                                eps = (List <Episode>)formatter.Deserialize(stream);

                                stream.Close();

                                string infoMsg = $"Season subtitles info read from {SEASON_SUBTITLES_FILENAME}. Downloading...";
                                _logger.LogDebug(infoMsg);
                                UpdateTaskDetails(cQueue, infoMsg);
                            }
                            else
                            {
                                // Download the list of episodes for this season
                                UpdateTaskDisplay(cQueue, .5, $"{SEASON_SUBTITLES_FILENAME} not found. Downloading show list...");
                                eps = await _api.GetSeasonSubtitles(myShow.Id, seasonNbr);

                                if (eps.Any())
                                {
                                    stream = new FileStream(SEASON_SUBTITLES_FILENAME, FileMode.Create, FileAccess.Write);
                                    formatter.Serialize(stream, eps);
                                    stream.Close();

                                    string infoMsg = $"Season subtitles info downloaded and written to {SEASON_SUBTITLES_FILENAME}. ";
                                    UpdateTaskDetails(cQueue, infoMsg);
                                }
                                else
                                {
                                    _logger.LogInformation($"No episodes for season ({seasonNbr}) were available.");
                                }
                            }

                            if (eps.Any())
                            {
                                // Find our target episode
                                var myEp = eps.Where(x => x.Number == episodeNbr).FirstOrDefault();

                                if (myEp == null)
                                {
                                    _logger.LogInformation($"No subtitles for series ({showName}) season ({seasonNbr}) episode ({episodeNbr}) were available.");
                                }
                                else
                                {
                                    foreach (CultureInfo language in cultureInfos)
                                    {
                                        // Find our target subtitle. Grab the first english one by default
                                        var found = myEp.Subtitles.FirstOrDefault(x => x.Language == language.DisplayName);

                                        // Try again using parent language
                                        if (found == null)
                                        {
                                            found = myEp.Subtitles.FirstOrDefault(x => x.Language == language.Parent.DisplayName);
                                        }

                                        if (found == null)
                                        {
                                            _logger.LogInformation($"Subtitles for series ({showName}) season ({seasonNbr}) episode ({episodeNbr}) were available, not in the language specified ({language})");
                                        }
                                        else
                                        {
                                            UpdateTaskDisplay(cQueue, .3, $"Found {eps.Count} episodes for season ({seasonNbr}). Episode ({episodeNbr}) subtitle found. Downloading...");
                                            _logger.LogInformation($"Downloading subtitles for series ({showName}) season ({seasonNbr}) episode ({episodeNbr}) in {language}");

                                            var downloadedSub = await _api.DownloadSubtitle(myShow.Id, found.DownloadUri);

                                            downloadedSubs.Add(new DownloadedSubtitle()
                                            {
                                                Contents = downloadedSub.Stream, CultureInfo = language
                                            });

                                            _logger.LogInformation($"Successfully retrieved subtitles for series ({showName}) season ({seasonNbr}) episode ({episodeNbr}) in {language}");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!foundShow)
                    {
                        string warning = $"Could not find show in list of available shows. Used the following show names: ({String.Join<string>(", ", showNameCandidates).TrimEnd()})";

                        _logger.LogWarning(warning);
                    }
                }
            }
            catch (Exception ex)
            {
                // Log the exception as a debug item, as it's presumably logged/handled up the call stack
                _logger.LogDebug(ex, $"Exception: {GetCaller()}()");

                _logger.LogTrace($"{GetCaller()}() exiting (exception)");
                throw;
            }

            _logger.LogTrace($"{GetCaller()}() exiting (normal)");
            return(downloadedSubs);
        }
Exemplo n.º 2
0
        async static Task <bool> SearchAddic7ed(string name, int season, int episode, string outputFile, string language = DEFAULT_LANGUAGE)
        {
            bool retval = false;

            try
            {
                var api = new Addic7ed.Addic7edApi.Api();
                // Get the list of all TV shows
                var tvShows = await api.GetShows();

                if (!tvShows.Any())
                {
                    _logger.Information("SearchAddic7ed(): No TV shows available.");
                }
                else
                {
                    // Find our target show
                    var myShow = tvShows.FirstOrDefault(x => x.Name.Contains(name));

                    if (myShow == null)
                    {
                        _logger.Information($"SearchAddic7ed(): TV show specified ({name}) was not in the list of available shows.");
                    }
                    else
                    {
                        // Find all subtitles for each episode in the target season
                        var eps = await api.GetSeasonSubtitles(myShow.Id, season);

                        if (!eps.Any())
                        {
                            _logger.Information($"SearchAddic7ed(): No episodes for season ({season}) were available.");
                        }
                        else
                        {
                            // Find our target episode
                            var myEp = eps.Where(x => x.Number == episode).FirstOrDefault();

                            if (myEp == null)
                            {
                                _logger.Information($"SearchAddic7ed(): No subtitles for season ({season}) episode ({episode}) were available.");
                            }
                            else
                            {
                                // Find our target subtitle. Grab the first english one by default
                                var found = myEp.Subtitles.FirstOrDefault(x => x.Language == language);

                                if (found == null)
                                {
                                    _logger.Information($"SearchAddic7ed(): Subtitles for season ({season}) episode ({episode}) were available, not in the language specified ({language})");
                                }
                                else
                                {
                                    var downloadedSub = await api.DownloadSubtitle(myShow.Id, found.DownloadUri);

                                    SaveFileStream(outputFile, downloadedSub.Stream);

                                    _logger.Information($"SearchAddic7ed(): Successfully retrieved subtitles for season ({season}) episode ({episode}) in {language}");
                                    retval = true;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error($"Exception in SearchAddic7ed(): {ex.Message}");
                throw;
            }

            return(retval);
        }