public async Task DeleteAndDownload(List <VkSongModel> toDownload, List <string> toDelete) { UiSynchronizer.Run(o => { var page = (MusicLoaderPage)o.Content; if (page == null) { return; } page.LbProgress.Content = "Удаление удаленных песен..."; }); var dir = SettingsManager.Get(SettingsRequisites.Directory); foreach (var fileName in toDelete) { File.Delete(Path.Combine(dir, fileName)); } await DownloadSongs(toDownload); UiSynchronizer.Run(o => { var page = (MusicLoaderPage)o.Content; page.LbProgress.Content = "Готово"; if (page == null) { return; } page.SyncLabels(); }); }
private async Task DownloadBatch(IEnumerable <VkSongModel> batch) { batch = batch.Take(BatchSize).ToList(); if (!batch.Any()) { return; } var jsonContent = ""; var trying = 0; while (string.IsNullOrEmpty(jsonContent)) { var client = new RestClient("https://vk.com"); var request = new RestRequest("al_audio.php", Method.POST); request.AddParameter("act", "reload_audio"); request.AddParameter("al", 1); request.AddParameter("ids", string.Join(",", batch.Skip(1).Select(i => i.UniqueId))); jsonContent = await SendAlAudioRequest(Sid, request, client); if (string.IsNullOrEmpty(jsonContent)) { trying++; if (trying > 5) { return; } var msg = $"Кажется VK перестал отдавать треки из-за таймаута ({trying}) :( надо подождать 10 сек"; UiSynchronizer.Run((o, d) => { var page = (MusicLoaderPage)o.Content; if (page == null) { return; } page.LbProgress.Content = d; }, msg); await Task.Delay(1000); } } var data = JsonConvert.DeserializeObject <List <object[]> >(jsonContent) .Select(VkSongModel.FromJson) .ToList(); data.ForEach(x => x.Unmask(Uid)); var abortSignal = false; foreach (var vkSongModel in data) { if (abortSignal) { UiSynchronizer.Run(o => { var page = (MusicLoaderPage)o.Content; if (page == null) { return; } page.LbProgress.Content = string.Empty; page.ProgressBar.Value = 0; abortSignal = page.MarkerToStopDownload; }); throw new AbortedException(); } var webClient = new WebClient(); var filePath = Path.Combine(Directory, vkSongModel.FileName); webClient.DownloadProgressChanged += (sender, args) => { var msg = $"Скачивание {vkSongModel.FileName}"; UiSynchronizer.Run((o, d) => { var page = (MusicLoaderPage)o.Content; if (page == null) { return; } page.LbProgress.Content = d; page.ProgressBar.Value = args.ProgressPercentage; abortSignal = page.MarkerToStopDownload; }, msg); }; await webClient.DownloadFileTaskAsync(vkSongModel.Url, filePath); UiSynchronizer.Run(o => { var page = (MusicLoaderPage)o.Content; if (page == null) { return; } page.SyncLabels(); abortSignal = page.MarkerToStopDownload; }); } }