Ejemplo n.º 1
0
        public async void UpdateExistingEpisode()
        {
            // Given
            var newhope = await _episodeRepository.Get(4);

            newhope.Title = "Episode4";

            // When
            _episodeRepository.Update(newhope);
            var saved = await _episodeRepository.SaveChangesAsync();

            // Then
            Assert.True(saved);
            using (var db = new AmstramgramContext(_options, _dbLogger.Object))
            {
                var episode = await db.Episodes.FindAsync(4);

                Assert.NotNull(episode);
                Assert.Equal(4, episode.Id);
                Assert.Equal("Episode4", episode.Title);

                // Cleanup
                episode.Title = "NEWHOPE";
                db.Episodes.Update(episode);
                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
        public bool Logic()
        {
            var logs = new List <Log>();

            try
            {
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Starting Downloader logic", null));

                var showRepo    = new ShowRepository(ConnectionString);
                var episodeRepo = new EpisodeRepository(ConnectionString);

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting shows from database", null));
                var showsInDatabase = showRepo.Select().Where(x => x.Download == true && x.Enabled == true).ToList();
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {showsInDatabase.Count} shows in database", null));

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting episodes from database", null));
                var episodesInDatabase = episodeRepo.Select();
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesInDatabase.Count} in database", null));

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Looping through shows that are enabled to download", null));
                foreach (var show in showsInDatabase)
                {
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Checking episodes scraped", null));
                    var episodesByShow = episodesInDatabase.Where(x => x.ShowId == show.Id && x.Status == EpisodeStatus.SCRAPED).OrderBy(y => y.Filename).ToList();
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesByShow.Count} to download", null));

                    foreach (var episode in episodesByShow)
                    {
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Downloading {episode.Filename}", null));
                        var path = System.IO.Path.Combine(Path, show.Title);
                        episode.Status = DownloadHelpers.DownloadFile(episode.Url, path, episode.Filename) ? EpisodeStatus.DOWNLOADED : EpisodeStatus.FAILED;

                        var downloadMessage = episode.Status == EpisodeStatus.DOWNLOADED ? "successfully" : "failed";
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Downladed {downloadMessage}", null));

                        episodeRepo.Update(episode);
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Updated show", null));
                    }
                }
            }
            catch (Exception e)
            {
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, e.Message, e.StackTrace));
            }

            logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Finished Downloader logic", null));

            LogRepository.Create(logs);

            return(true);
        }
Ejemplo n.º 3
0
        public bool Logic()
        {
            var logs = new List <Log>();

            try
            {
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Starting Enqueuer logic", null));

                var showRepo    = new ShowRepository(ConnectionString);
                var episodeRepo = new EpisodeRepository(ConnectionString);

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting shows from database", null));
                var showsInDatabase = showRepo.Select().Where(x => x.Download ?? true).ToList();
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {showsInDatabase.Count} shows in database", null));

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Looping through shows", null));
                foreach (var show in showsInDatabase)
                {
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Checking {show.Title}", null));

                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting episodes in database of show: {show.Title}", null));
                    var episodesInDatabase = episodeRepo.Select().Where(x => x.ShowId == show.Id).ToList();
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesInDatabase.Count} episodes in database of show: {show.Title}", null));

                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting episodes of show: {show.Title} that status is not downloaded", null));
                    var episodesByShow = episodesInDatabase.Where(x => x.Status == EpisodeStatus.UNKNOWN || x.Status == EpisodeStatus.FAILED && x.Status != EpisodeStatus.DOWNLOADED).ToList();
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesByShow} episodes in database of show: {show.Title}", null));

                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Getting episodes of show: {show.Title} in sources", null));
                    var newepisodes = GetEpisodesBySource(episodesByShow, show);
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {newepisodes.Count} episodes of show: {show.Title} in sources", null));


                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Looping through {show.Title} episodes", null));
                    foreach (var item in newepisodes)
                    {
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Checking {item.Filename}", null));

                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Checking if episode exists", null));
                        var ep = episodesByShow.SingleOrDefault(x => x.Filename == item.Filename);

                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Checking if episode exists as scraped or downloaded", null));
                        var existsAsScraperOrDownloaded = episodesInDatabase.SingleOrDefault(x => x.Filename == item.Filename && (x.Status == EpisodeStatus.SCRAPED || x.Status == EpisodeStatus.DOWNLOADED));

                        if (existsAsScraperOrDownloaded != null)
                        {
                            logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Episode already downloaded", null));
                            continue;
                        }

                        if (ep != null)
                        {
                            if (item.Status != ep.Status && ep.Status != EpisodeStatus.DOWNLOADED)
                            {
                                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Episode type updated to download", null));
                                episodeRepo.Update(item);
                                showRepo.Update(show);
                            }
                        }
                        else
                        {
                            logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Episode added to download queue", null));
                            episodeRepo.Create(item);
                            showRepo.Update(show);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, e.Message, e.StackTrace));
            }

            logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Finished Enqueuer logic", null));

            LogRepository.Create(logs);

            return(true);
        }