public async Task TvMoviesUpdateAsync(bool getYT) { if (InfoCaches.duringMovieUpdate) { return; } InfoCaches.duringMovieUpdate = true; try { // Log.Information($"Start TV movies upating"); // Console.WriteLine($"{DateTime.Now} Start TV movies upating"); var tvPrograms = new Dictionary <string, string>() { { "TVP1", "https://www.telemagazyn.pl/tvp_1/" }, { "TVP2", "https://www.telemagazyn.pl/tvp_2/" }, { "Polsat", "https://www.telemagazyn.pl/polsat/" }, { "TVN", "https://www.telemagazyn.pl/tvn/" }, { "TVN7", "https://www.telemagazyn.pl/tvn_7/" }, { "TV4", "https://www.telemagazyn.pl/tv_4/" }, { "TV Puls", "https://www.telemagazyn.pl/tv_puls/" }, { "Stopklatka", "https://www.telemagazyn.pl/stopklatka/" }, { "Fokus TV", "https://www.telemagazyn.pl/fokus_tv/" }, { "Nowa TV", "https://www.telemagazyn.pl/nowa_tv/" }, { "Metro", "https://www.telemagazyn.pl/metro/" }, { "WP1", "https://www.telemagazyn.pl/wp1/" }, { "Zoom TV", "https://www.telemagazyn.pl/zoom_tv/" } }; var toManyReq = false; //var lastDate = GetLastDate(); var todayDate = DateTime.Now.Date; var lastDate = DateTime.Now.AddDays(-1).Date; var movies = new List <TVMovie>(); // using(var context = new LiveContext(_sql)) // { movies = await _context.TVMovies.ToListAsync(); // } if (movies.Count > 0) { //var youngest = movies.OrderByDescending(x => x.PlayAt).ToList().FirstOrDefault(); //lastDate = DateTime.Parse(youngest.UrlEmisionDay).Date; var dates = movies.Select(x => DateTime.Parse(x.UrlEmisionDay)).ToList(); lastDate = dates.Max(); } var days = (lastDate - todayDate).Days; //var days1 = (lastDate.AddDays(-2) - todayDate).Days; //var days2 = (lastDate.AddDays(1) - todayDate).Days; //var days3 = (lastDate.AddDays(-4) - todayDate).Days; //Console.WriteLine($"days: {days}"); //Console.WriteLine($"days1: {days1}"); //Console.WriteLine($"days2: {days2}"); //Console.WriteLine($"days3: {days3}"); int from = 0; int future = 0; if (days < 0) { from = 0; future = 4; } if (days == 0) { from = 1; future = 4; } if (days == 1) { from = 2; future = 4; } if (days == 2) { from = 3; future = 4; } /* if(days == 3) * { * from = 3; * future = 4; * } */ if (days >= 3) { from = 0; future = 0; } //Console.Write($"Days: {days}"); // from = 0; //future = 1; var allMovies = new List <TVMovie>(); for (int i = from; i < future; i++) { //string z przeslosci - update dzis jutro i pojutrze i popojutrze //string taki sam jak teraz - update z dzis, jutra i pojutrza i popojutrza //string z jutra - update dla pojutrza i popojutrza // string z pojutrza - update dla popojutrza // string z dalej niz pojutrze - udpusc update var date = System.DateTime.Now.AddDays(i); var day = date.ToString("yyyy-MM-dd"); //Console.WriteLine($"Getting movies from day: {day}"); foreach (var station in tvPrograms) { var progMovies = await _tvMovieRepository.GetMoviesForTvStationAsync(station.Key, station.Value, day); allMovies.AddRange(progMovies); } } var moviesCount = allMovies.Count; //Log.Information($"TVMovies UPDATED with {moviesCount} movies"); foreach (var movie in allMovies) { var now = DateTime.Now; bool inFuture = movie.PlayAt >= now.AddHours(-1); var exists = await _tvMovieRepository.GetTheSameFromActual(movie); if ((exists is null) && inFuture) { //Console.WriteLine(moviesCount); var archiveMovieL = await _tvMovieRepository.GetByNameFromArchive(movie.TrailerSearch); var archiveMovie = archiveMovieL.FirstOrDefault(); if (archiveMovie is null) { if (toManyReq == false && getYT) { movie.SetYoutube(); } else { movie.SetWhileYoutube(); } if (movie.YouTube.VideoID.Contains("FirstError")) { Log.Warning("First ERROR from TVMovie Update"); toManyReq = true; } var rating = movie.getFilmwebRating(); await _tvMovieRepository.AddToArchiveAsync(movie); } else { movie.SetYoutubeFromArchive(archiveMovie); if (archiveMovie.Rating != null) { movie.changeRating(archiveMovie.Rating); } else { var rating = movie.getFilmwebRating(); archiveMovie.changeRating(rating); } } //await _liveContext.Songs.AddAsync(movie); //Console.WriteLine(movie.Title + " " + movie.YouTube.VideoID); movie.Added = DateTime.Now; //Console.WriteLine(movie.Title); await _context.TVMovies.AddAsync(movie); await _context.SaveChangesAsync(); moviesCount = moviesCount - 1; } // _liveContext.TVMovies.RemoveRange(_liveContext.TVMovies.Where(x => x.PlayAt < DateTime.Now.AddHours(-2))); } if (allMovies.Count > 0) { _context.TVMovies.RemoveRange(_context.TVMovies.Where(x => x.PlayAt < DateTime.Now.AddHours(-2))); await _context.SaveChangesAsync(); var errors = allMovies.Where(x => x.YouTube != null) .Where(x => x.YouTube.VideoID.Contains("Error")).ToList(); Log.Information($"Finish TV movies update with {allMovies.Count} and {errors.Count} youtube errors"); } InfoCaches.duringMovieUpdate = false; } catch (Exception ex) { Log.Error($"Error while updating tvmovies: {ex.Message}"); Log.Error(ex.StackTrace); InfoCaches.duringMovieUpdate = false; } }