private void Download() { if (isDownloading) { return; } isDownloading = true; Task.Factory.StartNew(async() => { DownloadWrapper download = null; while ((download = ListDownloads.FirstOrDefault(x => !x.IsComplete)) != null) { if (download.Retry > 3) { download.IsComplete = true; continue; } bool complete = false; complete = download.IsHLS ? await DownloadHLSAsync(download) : await DownloadMultiLinkAsync(download); download.IsComplete = complete; if (complete) { download.OnComplete?.Invoke(); } else { download.Retry++; } } isDownloading = false; }); }
public void AddDownload(List <string> links, string dstFolder, string filename = "", bool isHls = false) { DownloadWrapper wrapper = new DownloadWrapper() { DestinationFolder = dstFolder, Links = links, IsHLS = isHls, Filename = filename }; ListDownloads.Add(wrapper); Download(); }
private async Task <bool> DownloadMultiLinkAsync(DownloadWrapper download) { int completeDownload = 0; var dstFolder = Path.Combine(settings.DownloadFolder, download.DestinationFolder); Directory.CreateDirectory(dstFolder); Debug.WriteLine($"Folder: {dstFolder}"); for (int i = 0; i < download.Links.Count; i++) { download.CurrentLink = i + 1; var link = download.Links[i]; var filename = (!String.IsNullOrEmpty(download.Filename) ? download.Filename + " - " : "") + GetFilenameFromUrl(link); var dstPath = Path.Combine(dstFolder, filename); Debug.WriteLine($"Downloading {i + 1}/{download.Links.Count} - {filename}"); if (File.Exists(dstPath)) { completeDownload++; continue; } byte[] remoteContent = null; int retry = 0; do { try { remoteContent = await client.GetByteArrayAsync(link); } catch (HttpRequestException e) { Debug.WriteLine($"[ERROR] {e.Message} - {link}"); remoteContent = null; retry++; Thread.Sleep(500); } } while (remoteContent == null && retry <= 2); if (remoteContent == null) { continue; } using (var fileStream = new FileStream(dstPath, FileMode.Create)) { fileStream.Write(remoteContent, 0, remoteContent.Length); } completeDownload++; download.CompletePercentage = ((float)(completeDownload * 100)) / download.Links.Count; } return(completeDownload > 0); }
private async Task <bool> DownloadHLSAsync(DownloadWrapper download) { var filename = MakeValidFilename(download.Filename); // var filenameUrl = GetFilenameFromUrl(download.Links.First()); // var fileExt = ".ts";//Path.GetExtension(filenameUrl); var dstFolder = Path.Combine(settings.DownloadFolder, download.DestinationFolder); Directory.CreateDirectory(dstFolder); var dstPath = Path.Combine(dstFolder, $"{filename}.video"); try { using (var fileStream = new FileStream(dstPath, FileMode.Create)) { for (int partIndex = 0; partIndex < download.Links.Count; partIndex++) { Debug.WriteLine($"Downloading part {partIndex + 1}/{download.Links.Count} - {filename}"); download.CurrentLink = partIndex + 1; try { var hlsPart = await client.GetByteArrayAsync(download.Links[partIndex]); if (hls != null) { fileStream.Write(hlsPart, 0, hlsPart.Length); } download.CompletePercentage = ((float)((partIndex + 1) * 100)) / download.Links.Count; } catch (HttpRequestException e) { Debug.WriteLine(e.Message); partIndex--; } } } ChangeToMp4(dstFolder, filename, "", false); return(true); } catch (Exception e) { Debug.WriteLine(e.Message); } return(false); }