public static async Task<List<YouTubeUri>> GetVideoAllUrisAsync(string youTubeId, YouTubeFormat youTubeFormat = YouTubeFormat.All) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"); var response = await client.GetAsync("https://www.youtube.com/watch?v=" + youTubeId + "&nomobile=1"); var task = new TaskCompletionSource<List<YouTubeUri>>(); OnHtmlParse(await response.Content.ReadAsStringAsync(), (u, e) => { if (u != null) { List<YouTubeUri> result = null; if (youTubeFormat == YouTubeFormat.All) { result = u; } else if (youTubeFormat == YouTubeFormat.Flv || youTubeFormat == YouTubeFormat.Mp3) { //List<int> result = u.Select(uAll => uAll.Itag).Concat(FormatCodeFlvOrMp3).ToList(); result = (from uAll in u from flvOrMp3 in FormatCodeFlvOrMp3 where uAll.Itag == flvOrMp3 select uAll).ToList(); } else if (youTubeFormat == YouTubeFormat.Mp4) { result = (from uAll in u from mp4 in FormatCodeMp4 where uAll.Itag == mp4 select uAll).ToList(); } else if (youTubeFormat == YouTubeFormat.MP4OrFlv_mp3) { result = (from uAll in u from mP4OrFlv_mp3 in FormatCodeMP4OrFlv_mp3 where uAll.Itag == mP4OrFlv_mp3 select uAll).ToList(); } else { result = (from uAll in u from quality in FormatCodeQuality where uAll.Itag == quality select uAll).ToList(); } task.SetResult(result); } else if (e == null) { //task.SetCanceled(); task.SetResult(u); } else task.SetException(e); }); return await task.Task; } }
public static Task <List <YouTubeUri> > GetVideoAllUrisAsync(string youTubeId, YouTubeFormat youTubeFormat = YouTubeFormat.All) { var task = new TaskCompletionSource <List <YouTubeUri> >(); GetVideoAllUris(youTubeId, (u, e) => { if (u != null) { List <YouTubeUri> result = null; if (youTubeFormat == YouTubeFormat.All) { result = u; } else if (youTubeFormat == YouTubeFormat.Flv || youTubeFormat == YouTubeFormat.Mp3) { //List<int> result = u.Select(uAll => uAll.Itag).Concat(FormatCodeFlvOrMp3).ToList(); result = (from uAll in u from flvOrMp3 in FormatCodeFlvOrMp3 where uAll.Itag == flvOrMp3 select uAll).ToList(); } else if (youTubeFormat == YouTubeFormat.Mp4) { result = (from uAll in u from mp4 in FormatCodeMp4 where uAll.Itag == mp4 select uAll).ToList(); } else if (youTubeFormat == YouTubeFormat.MP4OrFlv_mp3) { result = (from uAll in u from mP4OrFlv_mp3 in FormatCodeMP4OrFlv_mp3 where uAll.Itag == mP4OrFlv_mp3 select uAll).ToList(); } else { result = (from uAll in u from quality in FormatCodeQuality where uAll.Itag == quality select uAll).ToList(); } task.SetResult(result); } else if (e == null) { //task.SetCanceled(); task.SetResult(u); } else { task.SetException(e); } }); return(task.Task); }