protected override async Task ExecuteJob() { if (animes.Any()) { ConcurrentBag <AnimeModel> updatedAnimeEntries = new ConcurrentBag <AnimeModel>(); AnimeApiEnumerator animeApi = new AnimeApiEnumerator(); List <Task> animeRequests = new List <Task>(); JobEvent.NotifyJobProgressChange(0, "Querying animes"); for (int i = 0; i < animes.Count(); i = i + EntriesPerRequest) { var currEntries = animes.Skip(i).Take(EntriesPerRequest); animeRequests.Add( Task.Run(async() => { var requestResult = await animeApi.GetByIdSet(currEntries.Select(e => e.Id).ToArray()); foreach (AnimeModel entry in requestResult) { updatedAnimeEntries.Add(entry); JobEvent.NotifyJobProgressChange(1, isIncremental: true); } })); } await Task.WhenAll(animeRequests); JobEvent.NotifyJobProgressChange(0, "Updating animes", true); animeService.AddOrUpdateRange(updatedAnimeEntries, (progress) => { JobEvent.NotifyJobProgressChange((int)progress, isIncremental: true); }); await Task.Delay(100); } if (mangas.Any()) { ConcurrentBag <MangaModel> updatedMangaEntries = new ConcurrentBag <MangaModel>(); MangaApiEnumerator mangaApi = new MangaApiEnumerator(); List <Task> mangaRequests = new List <Task>(); JobEvent.NotifyJobProgressChange(0, "Querying mangas", true); for (int i = 0; i < mangas.Count(); i = i + EntriesPerRequest) { var currEntries = mangas.Skip(i).Take(EntriesPerRequest); mangaRequests.Add( Task.Run(async() => { var requestResult = await mangaApi.GetByIdSet(currEntries.Select(e => e.Id).ToArray(), false); foreach (MangaModel entry in requestResult) { updatedMangaEntries.Add(entry); JobEvent.NotifyJobProgressChange(1, isIncremental: true); } })); } await Task.WhenAll(mangaRequests); JobEvent.NotifyJobProgressChange(0, "Updating mangas", true); mangaService.AddOrUpdateRange(updatedMangaEntries, (progress) => { JobEvent.NotifyJobProgressChange((int)progress, isIncremental: true); }); } }
protected override async Task ExecuteJob() { List <Task> requests = new List <Task>(); AnimeApiEnumerator api = new AnimeApiEnumerator(); ConcurrentBag <AnimeModel> persistableEntries = new ConcurrentBag <AnimeModel>(); JobEvent.NotifyJobProgressChange(0, "Querying API"); for (int i = 0; i < entries.Count(); i = i + EntriesPerRequest) { var currEntries = entries.Skip(i).Take(EntriesPerRequest); requests.Add( Task.Run(async() => { var requestResult = await api.GetByMalIdSet(currEntries.Select(entry => int.Parse(entry.Element("id").Value)).ToArray()); foreach (AnimeModel entry in requestResult) { var currEntry = currEntries.FirstOrDefault(e => e.Element("id").Value == entry.IdMal.ToString()); entry.DateAdded = DateTime.Now; var viewingStatus = currEntry.Element("viewingstatus").Value; switch (viewingStatus) { case "Watched": viewingStatus = StatusView.VIEWED; break; case "Dropped": viewingStatus = StatusView.DROPPEDANIME; break; case "Watching": viewingStatus = StatusView.WATCHING; break; default: viewingStatus = StatusView.TOWATCH; break; } entry.ViewingStatus = viewingStatus; string review = currEntry.Element("review").Value; bool isReviewEmpty = string.IsNullOrEmpty(review); string dropReason = currEntry.Element("dropreason").Value; bool isDropReasonEmpty = string.IsNullOrEmpty(dropReason); if (!isReviewEmpty && isDropReasonEmpty) { entry.PersonalReview = review; } else if (isReviewEmpty && !isDropReasonEmpty) { entry.PersonalReview = dropReason; } else if (!isReviewEmpty && !isDropReasonEmpty) { entry.PersonalReview = $"--- DW1 REVIEW ---{Environment.NewLine}{Environment.NewLine}{review}{Environment.NewLine}{Environment.NewLine}--- DW1 DROP REASON ---{Environment.NewLine}{Environment.NewLine}{dropReason}"; } if (double.TryParse(currEntry.Element("personal_score").Value, out double personalScore) && personalScore != -1) { entry.PersonalScore = (int)(personalScore * 10); } if (int.TryParse(currEntry.Element("currepisode").Value, out int currentEpisode)) { entry.CurrentEpisode = currentEpisode; } if (int.TryParse(currEntry.Element("watch_priority").Value, out int watchPriority) && watchPriority != -1) { entry.WatchPriority = watchPriority; } persistableEntries.Add(entry); JobEvent.NotifyJobProgressChange(1, isIncremental: true); } })); } await Task.WhenAll(requests); JobEvent.NotifyJobProgressChange(0, "Saving results", true); _animeService.AddOrUpdateRange(persistableEntries, (progress) => { JobEvent.NotifyJobProgressChange((int)progress, isIncremental: true); }); }