Exemplo n.º 1
0
        public async Task InitAsync(ConcurrentQueue <ProviderSignal> cQueue)
        {
            _logger.LogTrace($"{GetCaller()}() entered");
            Stream stream = null;

            try
            {
                // Create an API instance
                _api = new Addic7ed.Addic7edApi.Api();

                IFormatter formatter = new BinaryFormatter();

                if (File.Exists(SHOWS_CACHE_FILENAME))
                {
                    stream = new FileStream(SHOWS_CACHE_FILENAME, FileMode.Open, FileAccess.Read);

                    _tvShows = (List <TvShow>)formatter.Deserialize(stream);

                    stream.Close();

                    string message = $"Show list read from {SHOWS_CACHE_FILENAME}";
                    _logger.LogDebug(message);
                    UpdateTaskDetails(cQueue, $"Plugin initialized. {message}");
                }
                else
                {
                    // Get the list of all TV shows
                    UpdateTaskDisplay(cQueue, .5, $"{SHOWS_CACHE_FILENAME} not found. Downloading show list...");
                    _tvShows = await _api.GetShows();

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

                        string message = $"Show list downloaded and written to {SHOWS_CACHE_FILENAME}";
                        UpdateTaskDetails(cQueue, $"Plugin initialized. {message}");
                        _logger.LogDebug(message);
                    }
                    else
                    {
                        _logger.LogWarning($"No TV shows on site!");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Exception: {GetCaller()}()");
                _logger.LogTrace($"{GetCaller()}() exiting (exception)");
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            _logger.LogTrace($"{GetCaller()}() exiting (normal)");
        }
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);
        }