Beispiel #1
0
        public bool Logic()
        {
            var logs = new List <Log>();

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

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

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, "Getting enabled shows in database", null));
                var showsInDatabase = showRepo.Select().Where(x => x.Enabled == true && 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.DEBUG, "Getting episodes in database", null));
                var episodesInDatabase = episodeRepo.Select();
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesInDatabase.Count} episodes in database", null));

                logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Looping through shows", null));
                foreach (var show in showsInDatabase)
                {
                    // Get total episodes and status
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, "Getting show data from source", null));
                    var status = GetStatus(show.Source, show.Url);

                    // Check if current amount of episodes == total episodes and status is downloaded
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, "Getting episodes downloaded", null));
                    var episodesByShow = episodesInDatabase.Where(x => x.ShowId == show.Id && x.Status == EpisodeStatus.DOWNLOADED).ToList();
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.DEBUG, $"Found {episodesByShow} episodes downloaded", null));

                    // if yes then disable drama -> Enabled = false
                    logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, "Checking if it has to be disabled", null));
                    if (episodesByShow.Count == status.Item1 && status.Item2)
                    {
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"Disabling show {show.Title}", null));
                        show.Enabled = false;
                        showRepo.Update(show);
                    }
                    else
                    {
                        logs.Add(new Log(Core.Enums.LogHelpers.LogType.INFO, $"{show.Title} is still in progres", null));
                    }
                }
            }
            catch (Exception e)
            {
                logs.Add(new Log(Core.Enums.LogHelpers.LogType.ERROR, e.Message, e.StackTrace));
            }

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

            LogRepository.Create(logs);

            return(true);
        }
Beispiel #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);
        }
Beispiel #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);
        }