private static async Task <bool> DownloadAlternate(Item itemToDownload, string targetPath, Progress <double> progress = null) { FileDownload fileDownload = null; try { if (File.Exists(targetPath)) { File.Delete(targetPath); } string youTubeVideoId = YoutubeClient.ParseVideoId(itemToDownload.PocketItem.Uri.ToString()); string saveMediaURL = $"https://dev.invidio.us/watch?v={youTubeVideoId}"; //"https://odownloader.com/download?q=" + HttpUtility.UrlEncode(itemToDownload.PocketItem.Uri.ToString()); using (WebClient client = new WebClient()) { client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); string html = client.DownloadString(saveMediaURL); string videoSources = Regex.Match(html, @"<source.*>").Value; string downloadURLWithHighestQuality = "https://www.invidio.us" + Regex.Match(videoSources, @"src=""([^""]*)""").Groups[1].Value; downloadURLWithHighestQuality = Utilities.GetRedirectURL(downloadURLWithHighestQuality); fileDownload = new FileDownload(downloadURLWithHighestQuality, targetPath, progress: progress); FilesDownloading.Add(fileDownload); await fileDownload.Start(); } FilesDownloading.Remove(fileDownload); //Remove download once it is completed return(true); } catch (Exception ex) { itemToDownload.Progress = -1; if (fileDownload != null) { FilesDownloading.Remove(fileDownload); } if (File.Exists(targetPath)) { File.Delete(targetPath); } } return(false); }
private static async Task <bool> DownloadWithYouTubeExplode(Item itemToDownload, string targetPath, YoutubeClient client, Progress <double> progress = null) { FileDownload fileDownload = null; try { if (File.Exists(targetPath)) { File.Delete(targetPath); } string youTubeVideoId = YoutubeClient.ParseVideoId(itemToDownload.PocketItem.Uri.ToString()); Video videoInfo = await itemToDownload.GetOrGenerateVideoInfo(); MediaStreamInfoSet streamInfoSet = await client.GetVideoMediaStreamInfosAsync(youTubeVideoId); List <MuxedStreamInfo> qualities = streamInfoSet.Muxed.OrderByDescending(s => s.VideoQuality).ToList(); //Loop through qualities highest to lowest (in case high qualities fail) as suggested in https://github.com/Tyrrrz/YoutubeExplode/issues/219 foreach (MuxedStreamInfo videoQuality in qualities) { try { //using (MediaStream stream = await client.GetMediaStreamAsync(videoQuality).ConfigureAwait(false)) { fileDownload = new FileDownload(client, videoQuality, targetPath, progress); FilesDownloading.Add(fileDownload); await fileDownload.Start(); } FilesDownloading.Remove(fileDownload); //Remove download once it is completed return(true); } catch (Exception ex) //Catch errors caused by https://github.com/Tyrrrz/YoutubeExplode/issues/219 { itemToDownload.Progress = -1; if (fileDownload != null) { FilesDownloading.Remove(fileDownload); } if (File.Exists(targetPath)) { File.Delete(targetPath); } } } } catch (Exception ex) { if (fileDownload != null) { FilesDownloading.Remove(fileDownload); } return(false); } return(false); }