/
YoutubeClient.cs
992 lines (838 loc) · 41.8 KB
/
YoutubeClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using YoutubeExplode.Exceptions;
using YoutubeExplode.Internal;
using YoutubeExplode.Internal.CipherOperations;
using YoutubeExplode.Models;
using YoutubeExplode.Models.ClosedCaptions;
using YoutubeExplode.Models.MediaStreams;
using YoutubeExplode.Services;
#if NET45 || NETCOREAPP1_0
using System.IO;
using System.Text;
using System.Threading;
#endif
namespace YoutubeExplode
{
/// <summary>
/// YoutubeClient
/// </summary>
public partial class YoutubeClient
{
private readonly IHttpService _httpService;
private readonly Dictionary<string, PlayerSource> _playerSourceCache;
/// <summary>
/// Creates an instance of <see cref="YoutubeClient"/> with custom services
/// </summary>
public YoutubeClient(IHttpService httpService)
{
_httpService = httpService ?? throw new ArgumentNullException(nameof(httpService));
_playerSourceCache = new Dictionary<string, PlayerSource>();
}
/// <summary>
/// Creates an instance of <see cref="YoutubeClient"/> with default services
/// </summary>
public YoutubeClient()
: this(HttpService.Instance)
{
}
private async Task<PlayerContext> GetPlayerContextAsync(string videoId)
{
// Get the embed video page
var request = $"https://www.youtube.com/embed/{videoId}";
var response = await _httpService.GetStringAsync(request).ConfigureAwait(false);
// Extract values
var sourceUrl = Regex.Match(response, @"""js""\s*:\s*""(.*?)""").Groups[1].Value.Replace("\\", "");
var sts = Regex.Match(response, @"""sts""\s*:\s*(\d+)").Groups[1].Value;
// Check if successful
if (sourceUrl.IsBlank() || sts.IsBlank())
throw new ParseException("Could not parse player context");
// Append host to source url
sourceUrl = "https://www.youtube.com" + sourceUrl;
return new PlayerContext(sourceUrl, sts);
}
private async Task<PlayerSource> GetPlayerSourceAsync(string sourceUrl)
{
// Original code credit: Decipherer class of https://github.com/flagbug/YoutubeExtractor
// Try to resolve from cache first
var playerSource = _playerSourceCache.GetOrDefault(sourceUrl);
if (playerSource != null)
return playerSource;
// Get player source code
var response = await _httpService.GetStringAsync(sourceUrl).ConfigureAwait(false);
// Find the name of the function that handles deciphering
var funcName = Regex.Match(response, @"\""signature"",\s?([a-zA-Z0-9\$]+)\(").Groups[1].Value;
if (funcName.IsBlank())
throw new ParseException("Could not find the entry function for signature deciphering");
// Find the body of the function
var funcPattern = @"(?!h\.)" + Regex.Escape(funcName) + @"=function\(\w+\)\{(.*?)\}";
var funcBody = Regex.Match(response, funcPattern, RegexOptions.Singleline).Groups[1].Value;
if (funcBody.IsBlank())
throw new ParseException("Could not find the signature decipherer function body");
var funcLines = funcBody.Split(";").ToArray();
// Identify cipher functions
string reverseFuncName = null;
string sliceFuncName = null;
string charSwapFuncName = null;
var operations = new List<ICipherOperation>();
// Analyze the function body to determine the names of cipher functions
foreach (var line in funcLines)
{
// Break when all functions are found
if (reverseFuncName.IsNotBlank() && sliceFuncName.IsNotBlank() && charSwapFuncName.IsNotBlank())
break;
// Get the function called on this line
var calledFunctionName = Regex.Match(line, @"\w+\.(\w+)\(").Groups[1].Value;
if (calledFunctionName.IsBlank())
continue;
// Find cipher function names
if (Regex.IsMatch(response, $@"{Regex.Escape(calledFunctionName)}:\bfunction\b\(\w+\)"))
{
reverseFuncName = calledFunctionName;
}
else if (Regex.IsMatch(response,
$@"{Regex.Escape(calledFunctionName)}:\bfunction\b\([a],b\).(\breturn\b)?.?\w+\."))
{
sliceFuncName = calledFunctionName;
}
else if (Regex.IsMatch(response,
$@"{Regex.Escape(calledFunctionName)}:\bfunction\b\(\w+\,\w\).\bvar\b.\bc=a\b"))
{
charSwapFuncName = calledFunctionName;
}
}
// Analyze the function body again to determine the operation set and order
foreach (var line in funcLines)
{
// Get the function called on this line
var calledFunctionName = Regex.Match(line, @"\w+\.(\w+)\(").Groups[1].Value;
if (calledFunctionName.IsBlank())
continue;
// Swap operation
if (calledFunctionName == charSwapFuncName)
{
var index = Regex.Match(line, @"\(\w+,(\d+)\)").Groups[1].Value.ParseInt();
operations.Add(new SwapCipherOperation(index));
}
// Slice operation
else if (calledFunctionName == sliceFuncName)
{
var index = Regex.Match(line, @"\(\w+,(\d+)\)").Groups[1].Value.ParseInt();
operations.Add(new SliceCipherOperation(index));
}
// Reverse operation
else if (calledFunctionName == reverseFuncName)
{
operations.Add(new ReverseCipherOperation());
}
}
return _playerSourceCache[sourceUrl] = new PlayerSource(operations);
}
/// <summary>
/// Checks whether a video with the given ID exists
/// </summary>
public async Task<bool> CheckVideoExistsAsync(string videoId)
{
if (videoId == null)
throw new ArgumentNullException(nameof(videoId));
if (!ValidateVideoId(videoId))
throw new ArgumentException("Invalid Youtube video ID", nameof(videoId));
// Get
var request = $"https://www.youtube.com/get_video_info?video_id={videoId}&el=info&ps=default&hl=en";
var response = await _httpService.GetStringAsync(request).ConfigureAwait(false);
// Parse
var videoInfoDic = UrlHelper.GetDictionaryFromUrlQuery(response);
// Check error code
if (videoInfoDic.ContainsKey("errorcode"))
{
var errorCode = videoInfoDic.Get("errorcode").ParseInt();
return errorCode != 100 && errorCode != 150;
}
return true;
}
/// <summary>
/// Gets video info by ID
/// </summary>
public async Task<VideoInfo> GetVideoInfoAsync(string videoId)
{
if (videoId == null)
throw new ArgumentNullException(nameof(videoId));
if (!ValidateVideoId(videoId))
throw new ArgumentException("Invalid Youtube video ID", nameof(videoId));
// Get player context
var playerContext = await GetPlayerContextAsync(videoId).ConfigureAwait(false);
// Get video info
var request = $"https://www.youtube.com/get_video_info?video_id={videoId}&sts={playerContext.Sts}&el=info&ps=default&hl=en";
var response = await _httpService.GetStringAsync(request).ConfigureAwait(false);
var videoInfoDic = UrlHelper.GetDictionaryFromUrlQuery(response);
// Check for error
if (videoInfoDic.ContainsKey("errorcode"))
{
var errorCode = videoInfoDic.Get("errorcode").ParseInt();
var errorReason = videoInfoDic.Get("reason");
throw new VideoNotAvailableException(errorCode, errorReason);
}
// Check for paid content
if (videoInfoDic.GetOrDefault("requires_purchase") == "1")
{
var previewVideoId = videoInfoDic.Get("ypc_vid");
throw new VideoRequiresPurchaseException(previewVideoId);
}
// Parse metadata
var title = videoInfoDic.Get("title");
var duration = TimeSpan.FromSeconds(videoInfoDic.Get("length_seconds").ParseDouble());
var viewCount = videoInfoDic.Get("view_count").ParseLong();
var keywords = videoInfoDic.Get("keywords").Split(",");
var watermarks = videoInfoDic.Get("watermark").Split(",");
var isListed = videoInfoDic.GetOrDefault("is_listed") == "1"; // https://github.com/Tyrrrz/YoutubeExplode/issues/45
var isRatingAllowed = videoInfoDic.Get("allow_ratings") == "1";
var isMuted = videoInfoDic.Get("muted") == "1";
var isEmbeddingAllowed = videoInfoDic.Get("allow_embed") == "1";
// Parse mixed streams
var mixedStreams = new List<MixedStreamInfo>();
var mixedStreamsEncoded = videoInfoDic.GetOrDefault("url_encoded_fmt_stream_map");
if (mixedStreamsEncoded.IsNotBlank())
{
foreach (var streamEncoded in mixedStreamsEncoded.Split(","))
{
var streamDic = UrlHelper.GetDictionaryFromUrlQuery(streamEncoded);
var itag = streamDic.Get("itag").ParseInt();
#if RELEASE
// Skip unknown itags on RELEASE
if (!MediaStreamInfo.IsKnown(itag)) continue;
#endif
var url = streamDic.Get("url");
var sig = streamDic.GetOrDefault("s");
// Decipher signature if needed
if (sig.IsNotBlank())
{
var playerSource = await GetPlayerSourceAsync(playerContext.SourceUrl).ConfigureAwait(false);
sig = playerSource.Decipher(sig);
url = UrlHelper.SetUrlQueryParameter(url, "signature", sig);
}
// Get content length
long contentLength;
using (var reqMsg = new HttpRequestMessage(HttpMethod.Head, url))
using (var resMsg = await _httpService.PerformRequestAsync(reqMsg).ConfigureAwait(false))
{
// Check status code (https://github.com/Tyrrrz/YoutubeExplode/issues/36)
if (resMsg.StatusCode == HttpStatusCode.NotFound ||
resMsg.StatusCode == HttpStatusCode.Gone)
continue;
// Ensure success
resMsg.EnsureSuccessStatusCode();
// Extract content length
contentLength = resMsg.Content.Headers.ContentLength ?? -1;
if (contentLength < 0)
throw new ParseException("Could not extract content length");
}
// Set rate bypass
url = UrlHelper.SetUrlQueryParameter(url, "ratebypass", "yes");
var stream = new MixedStreamInfo(itag, url, contentLength);
mixedStreams.Add(stream);
}
}
// Parse adaptive streams
var audioStreams = new List<AudioStreamInfo>();
var videoStreams = new List<VideoStreamInfo>();
var adaptiveStreamsEncoded = videoInfoDic.GetOrDefault("adaptive_fmts");
if (adaptiveStreamsEncoded.IsNotBlank())
{
foreach (var streamEncoded in adaptiveStreamsEncoded.Split(","))
{
var streamDic = UrlHelper.GetDictionaryFromUrlQuery(streamEncoded);
var itag = streamDic.Get("itag").ParseInt();
#if RELEASE
// Skip unknown itags on RELEASE
if (!MediaStreamInfo.IsKnown(itag)) continue;
#endif
var url = streamDic.Get("url");
var sig = streamDic.GetOrDefault("s");
var contentLength = streamDic.Get("clen").ParseLong();
var bitrate = streamDic.Get("bitrate").ParseLong();
// Decipher signature if needed
if (sig.IsNotBlank())
{
var playerSource = await GetPlayerSourceAsync(playerContext.SourceUrl).ConfigureAwait(false);
sig = playerSource.Decipher(sig);
url = UrlHelper.SetUrlQueryParameter(url, "signature", sig);
}
// Set rate bypass
url = UrlHelper.SetUrlQueryParameter(url, "ratebypass", "yes");
// Check if audio
var isAudio = streamDic.Get("type").Contains("audio/");
// If audio stream
if (isAudio)
{
var stream = new AudioStreamInfo(itag, url, contentLength, bitrate);
audioStreams.Add(stream);
}
// If video stream
else
{
// Parse additional data
var size = streamDic.Get("size");
var width = size.SubstringUntil("x").ParseInt();
var height = size.SubstringAfter("x").ParseInt();
var resolution = new VideoResolution(width, height);
var framerate = streamDic.Get("fps").ParseDouble();
var stream = new VideoStreamInfo(itag, url, contentLength, bitrate, resolution, framerate);
videoStreams.Add(stream);
}
}
}
// Parse adaptive streams from dash
var dashManifestUrl = videoInfoDic.GetOrDefault("dashmpd");
if (dashManifestUrl.IsNotBlank())
{
// Parse signature
var sig = Regex.Match(dashManifestUrl, @"/s/(.*?)(?:/|$)").Groups[1].Value;
// Decipher signature if needed
if (sig.IsNotBlank())
{
var playerSource = await GetPlayerSourceAsync(playerContext.SourceUrl).ConfigureAwait(false);
sig = playerSource.Decipher(sig);
dashManifestUrl = UrlHelper.SetUrlPathParameter(dashManifestUrl, "signature", sig);
}
// Get the manifest
response = await _httpService.GetStringAsync(dashManifestUrl).ConfigureAwait(false);
var dashManifestXml = XElement.Parse(response).StripNamespaces();
var streamsXml = dashManifestXml.Descendants("Representation");
// Filter out partial streams
streamsXml = streamsXml
.Where(x => !(x.Descendant("Initialization")
?.Attribute("sourceURL")
?.Value.Contains("sq/") ?? false));
// Parse streams
foreach (var streamXml in streamsXml)
{
var itag = (int) streamXml.AttributeStrict("id");
#if RELEASE
// Skip unknown itags on RELEASE
if (!MediaStreamInfo.IsKnown(itag)) continue;
#endif
var url = streamXml.ElementStrict("BaseURL").Value;
var bitrate = (long) streamXml.AttributeStrict("bandwidth");
// Parse content length
var contentLength = Regex.Match(url, @"clen[/=](\d+)").Groups[1].Value.ParseLong();
// Set rate bypass
url = url.Contains("&")
? UrlHelper.SetUrlQueryParameter(url, "ratebypass", "yes")
: UrlHelper.SetUrlPathParameter(url, "ratebypass", "yes");
// Check if audio stream
var isAudio = streamXml.Element("AudioChannelConfiguration") != null;
// If audio stream
if (isAudio)
{
var stream = new AudioStreamInfo(itag, url, contentLength, bitrate);
audioStreams.Add(stream);
}
// If video stream
else
{
// Parse additional data
var width = (int) streamXml.AttributeStrict("width");
var height = (int) streamXml.AttributeStrict("height");
var resolution = new VideoResolution(width, height);
var framerate = (double) streamXml.AttributeStrict("frameRate");
var stream = new VideoStreamInfo(itag, url, contentLength, bitrate, resolution, framerate);
videoStreams.Add(stream);
}
}
}
// Finalize stream lists
mixedStreams = mixedStreams.Distinct(s => s.Itag).OrderByDescending(s => s.VideoQuality).ToList();
audioStreams = audioStreams.Distinct(s => s.Itag).OrderByDescending(s => s.Bitrate).ToList();
videoStreams = videoStreams.Distinct(s => s.Itag).OrderByDescending(s => s.VideoQuality).ToList();
// Parse closed caption tracks
var captions = new List<ClosedCaptionTrackInfo>();
var captionsEncoded = videoInfoDic.GetOrDefault("caption_tracks");
if (captionsEncoded.IsNotBlank())
{
foreach (var captionEncoded in captionsEncoded.Split(","))
{
var captionDic = UrlHelper.GetDictionaryFromUrlQuery(captionEncoded);
var url = captionDic.Get("u");
var isAuto = captionDic.Get("v").Contains("a.");
var code = captionDic.Get("lc");
var name = captionDic.Get("n");
var language = new Language(code, name);
var caption = new ClosedCaptionTrackInfo(url, language, isAuto);
captions.Add(caption);
}
}
// Get metadata extension
request = $"https://www.youtube.com/get_video_metadata?video_id={videoId}";
response = await _httpService.GetStringAsync(request).ConfigureAwait(false);
var videoInfoExtXml = XElement.Parse(response).StripNamespaces().ElementStrict("html_content");
// Parse
var description = videoInfoExtXml.ElementStrict("video_info").ElementStrict("description").Value;
var likeCount = (long) videoInfoExtXml.ElementStrict("video_info").ElementStrict("likes_count_unformatted");
var dislikeCount = (long) videoInfoExtXml.ElementStrict("video_info").ElementStrict("dislikes_count_unformatted");
// Parse author info
var authorId = videoInfoExtXml.ElementStrict("user_info").ElementStrict("channel_external_id").Value;
var authorName = videoInfoExtXml.ElementStrict("user_info").ElementStrict("username").Value;
var authorTitle = videoInfoExtXml.ElementStrict("user_info").ElementStrict("channel_title").Value;
var authorIsPaid = videoInfoExtXml.ElementStrict("user_info").ElementStrict("channel_paid").Value == "1";
var authorLogoUrl = videoInfoExtXml.ElementStrict("user_info").ElementStrict("channel_logo_url").Value;
var authorBannerUrl = videoInfoExtXml.ElementStrict("user_info").ElementStrict("channel_banner_url").Value;
var author = new ChannelInfo(
authorId, authorName, authorTitle,
authorIsPaid, authorLogoUrl, authorBannerUrl);
return new VideoInfo(
videoId, title, author,
duration, description, keywords, watermarks,
viewCount, likeCount, dislikeCount,
isListed, isRatingAllowed, isMuted, isEmbeddingAllowed,
mixedStreams, audioStreams, videoStreams, captions);
}
/// <summary>
/// Gets playlist info by ID, truncating resulting video list at given number of pages (1 page ≤ 200 videos)
/// </summary>
public async Task<PlaylistInfo> GetPlaylistInfoAsync(string playlistId, int maxPages)
{
if (playlistId == null)
throw new ArgumentNullException(nameof(playlistId));
if (!ValidatePlaylistId(playlistId))
throw new ArgumentException("Invalid Youtube playlist ID", nameof(playlistId));
if (maxPages <= 0)
throw new ArgumentOutOfRangeException(nameof(maxPages), "Needs to be a positive number");
// Get all videos across pages
var pagesDone = 0;
var offset = 0;
XElement playlistInfoXml;
var videos = new List<VideoInfoSnippet>();
var videoIds = new HashSet<string>();
do
{
// Get
var request = $"https://www.youtube.com/list_ajax?style=xml&action_get_list=1&list={playlistId}&index={offset}";
var response = await _httpService.GetStringAsync(request).ConfigureAwait(false);
playlistInfoXml = XElement.Parse(response).StripNamespaces();
// Parse videos
var total = 0;
var delta = 0;
foreach (var videoInfoSnippetXml in playlistInfoXml.Elements("video"))
{
// Basic info
var videoId = videoInfoSnippetXml.ElementStrict("encrypted_id").Value;
var videoTitle = videoInfoSnippetXml.ElementStrict("title").Value;
var videoDuration =
TimeSpan.FromSeconds(videoInfoSnippetXml.ElementStrict("length_seconds").Value.ParseDouble());
var videoDescription = videoInfoSnippetXml.ElementStrict("description").Value;
var videoViewCount =
Regex.Replace(videoInfoSnippetXml.ElementStrict("views").Value, @"\D", "").ParseLong();
var videoLikeCount =
Regex.Replace(videoInfoSnippetXml.ElementStrict("likes").Value, @"\D", "").ParseLong();
var videoDislikeCount =
Regex.Replace(videoInfoSnippetXml.ElementStrict("dislikes").Value, @"\D", "").ParseLong();
// Keywords
var videoKeywordsJoined = videoInfoSnippetXml.ElementStrict("keywords").Value;
var videoKeywords = Regex
.Matches(videoKeywordsJoined, @"(?<=(^|\s)(?<quote>""?))([^""]|(""""))*?(?=\<quote>(?=\s|$))")
.Cast<Match>()
.Select(m => m.Value)
.Where(s => s.IsNotBlank());
var snippet = new VideoInfoSnippet(videoId, videoTitle, videoDuration, videoDescription,
videoKeywords, videoViewCount, videoLikeCount, videoDislikeCount);
// Add to list if not already there
if (videoIds.Add(snippet.Id))
{
videos.Add(snippet);
delta++;
}
total++;
}
// Break if the videos started repeating
if (delta <= 0) break;
// Prepare for next page
pagesDone++;
offset += total;
} while (pagesDone < maxPages);
// Parse metadata
var title = playlistInfoXml.ElementStrict("title").Value;
var author = playlistInfoXml.Element("author")?.Value ?? "";
var description = playlistInfoXml.ElementStrict("description").Value;
var viewCount = (long) playlistInfoXml.ElementStrict("views");
return new PlaylistInfo(playlistId, title, author, description, viewCount, videos);
}
/// <summary>
/// Gets playlist info by ID
/// </summary>
public async Task<PlaylistInfo> GetPlaylistInfoAsync(string playlistId)
=> await GetPlaylistInfoAsync(playlistId, int.MaxValue).ConfigureAwait(false);
/// <summary>
/// Gets videos uploaded to a channel with given ID, truncating resulting video list at given number of pages (1 page ≤ 200 videos)
/// </summary>
public async Task<IEnumerable<VideoInfoSnippet>> GetChannelUploadsAsync(string channelId, int maxPages)
{
if (channelId == null)
throw new ArgumentNullException(nameof(channelId));
if (!ValidateChannelId(channelId))
throw new ArgumentException("Invalid Youtube channel ID", nameof(channelId));
if (maxPages <= 0)
throw new ArgumentOutOfRangeException(nameof(maxPages), "Needs to be a positive number");
// Compose a playlist ID
var playlistId = "UU" + channelId.SubstringAfter("UC");
// Get playlist info
var playlistInfo = await GetPlaylistInfoAsync(playlistId, maxPages).ConfigureAwait(false);
return playlistInfo.Videos;
}
/// <summary>
/// Gets videos uploaded to a channel with given ID
/// </summary>
public async Task<IEnumerable<VideoInfoSnippet>> GetChannelUploadsAsync(string channelId)
=> await GetChannelUploadsAsync(channelId, int.MaxValue).ConfigureAwait(false);
/// <summary>
/// Gets channel info by ID
/// </summary>
public async Task<ChannelInfo> GetChannelInfoAsync(string channelId)
{
if (channelId == null)
throw new ArgumentNullException(nameof(channelId));
if (!ValidateChannelId(channelId))
throw new ArgumentException("Invalid Youtube channel ID", nameof(channelId));
// Get channel uploads
var uploads = await GetChannelUploadsAsync(channelId, 1).ConfigureAwait(false);
var videoInfoSnippet = uploads.FirstOrDefault();
if (videoInfoSnippet == null)
throw new ParseException("Cannot get channel info because it doesn't have any uploaded videos");
// Get video info of the first video
var videoInfo = await GetVideoInfoAsync(videoInfoSnippet.Id).ConfigureAwait(false);
return videoInfo.Author;
}
/// <summary>
/// Gets the actual media stream represented by given metadata
/// </summary>
public async Task<MediaStream> GetMediaStreamAsync(MediaStreamInfo mediaStreamInfo)
{
if (mediaStreamInfo == null)
throw new ArgumentNullException(nameof(mediaStreamInfo));
// Get
var stream = await _httpService.GetStreamAsync(mediaStreamInfo.Url).ConfigureAwait(false);
return new MediaStream(mediaStreamInfo, stream);
}
/// <summary>
/// Gets the actual closed caption track represented by given metadata
/// </summary>
public async Task<ClosedCaptionTrack> GetClosedCaptionTrackAsync(ClosedCaptionTrackInfo closedCaptionTrackInfo)
{
if (closedCaptionTrackInfo == null)
throw new ArgumentNullException(nameof(closedCaptionTrackInfo));
// Get
var response = await _httpService.GetStringAsync(closedCaptionTrackInfo.Url).ConfigureAwait(false);
var captionTrackXml = XElement.Parse(response).StripNamespaces();
// Parse
var captions = new List<ClosedCaption>();
foreach (var captionXml in captionTrackXml.Descendants("text"))
{
var text = captionXml.Value;
var offset = TimeSpan.FromSeconds((double) captionXml.AttributeStrict("start"));
var duration = TimeSpan.FromSeconds((double) captionXml.AttributeStrict("dur"));
var caption = new ClosedCaption(text, offset, duration);
captions.Add(caption);
}
return new ClosedCaptionTrack(closedCaptionTrackInfo, captions);
}
}
// Platform-specific extensions
public partial class YoutubeClient
{
#if NET45 || NETCOREAPP1_0
/// <summary>
/// Downloads a media stream to file
/// </summary>
public async Task DownloadMediaStreamAsync(MediaStreamInfo mediaStreamInfo, string filePath,
IProgress<double> progress, CancellationToken cancellationToken, int bufferSize)
{
if (mediaStreamInfo == null)
throw new ArgumentNullException(nameof(mediaStreamInfo));
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
// Save to file
using (var input = await GetMediaStreamAsync(mediaStreamInfo).ConfigureAwait(false))
using (var output = File.Create(filePath, bufferSize))
{
var buffer = new byte[bufferSize];
int bytesRead;
long totalBytesRead = 0;
do
{
// Read
totalBytesRead += bytesRead =
await input.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
// Write
await output.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
// Report progress
progress?.Report(1.0 * totalBytesRead / input.Length);
} while (bytesRead > 0);
}
}
/// <summary>
/// Downloads a media stream to file
/// </summary>
public async Task DownloadMediaStreamAsync(MediaStreamInfo mediaStreamInfo, string filePath,
IProgress<double> progress, CancellationToken cancellationToken)
=> await DownloadMediaStreamAsync(mediaStreamInfo, filePath, progress, cancellationToken, 4096)
.ConfigureAwait(false);
/// <summary>
/// Downloads a media stream to file
/// </summary>
public async Task DownloadMediaStreamAsync(MediaStreamInfo mediaStreamInfo, string filePath,
IProgress<double> progress)
=> await DownloadMediaStreamAsync(mediaStreamInfo, filePath, progress, CancellationToken.None)
.ConfigureAwait(false);
/// <summary>
/// Downloads a media stream to file
/// </summary>
public async Task DownloadMediaStreamAsync(MediaStreamInfo mediaStreamInfo, string filePath)
=> await DownloadMediaStreamAsync(mediaStreamInfo, filePath, null)
.ConfigureAwait(false);
/// <summary>
/// Downloads a closed caption track to file
/// </summary>
public async Task DownloadClosedCaptionTrackAsync(ClosedCaptionTrackInfo closedCaptionTrackInfo,
string filePath, IProgress<double> progress, CancellationToken cancellationToken, int bufferSize)
{
if (closedCaptionTrackInfo == null)
throw new ArgumentNullException(nameof(closedCaptionTrackInfo));
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
// Get and create streams
var closedCaptionTrack = await GetClosedCaptionTrackAsync(closedCaptionTrackInfo).ConfigureAwait(false);
// Save to file as SRT
using (var output = File.Create(filePath, bufferSize))
using (var sw = new StreamWriter(output, Encoding.Unicode, bufferSize))
{
for (int i = 0; i < closedCaptionTrack.Captions.Count; i++)
{
// Make sure cancellation was not requested
cancellationToken.ThrowIfCancellationRequested();
var closedCaption = closedCaptionTrack.Captions[i];
var buffer = new StringBuilder();
// Line number
buffer.AppendLine((i + 1).ToString());
// Time start --> time end
buffer.Append(closedCaption.Offset.ToString(@"hh\:mm\:ss\,fff"));
buffer.Append(" --> ");
buffer.Append((closedCaption.Offset + closedCaption.Duration).ToString(@"hh\:mm\:ss\,fff"));
buffer.AppendLine();
// Actual text
buffer.AppendLine(closedCaption.Text);
// Write to stream
await sw.WriteLineAsync(buffer.ToString()).ConfigureAwait(false);
// Report progress
progress?.Report((i + 1.0) / closedCaptionTrack.Captions.Count);
}
}
}
/// <summary>
/// Downloads a closed caption track to file
/// </summary>
public async Task DownloadClosedCaptionTrackAsync(ClosedCaptionTrackInfo closedCaptionTrackInfo,
string filePath, IProgress<double> progress, CancellationToken cancellationToken)
=> await DownloadClosedCaptionTrackAsync(closedCaptionTrackInfo, filePath, progress,
cancellationToken, 4096)
.ConfigureAwait(false);
/// <summary>
/// Downloads a closed caption track to file
/// </summary>
public async Task DownloadClosedCaptionTrackAsync(ClosedCaptionTrackInfo closedCaptionTrackInfo,
string filePath, IProgress<double> progress)
=> await DownloadClosedCaptionTrackAsync(closedCaptionTrackInfo, filePath, progress,
CancellationToken.None)
.ConfigureAwait(false);
/// <summary>
/// Downloads a closed caption track to file
/// </summary>
public async Task DownloadClosedCaptionTrackAsync(ClosedCaptionTrackInfo closedCaptionTrackInfo,
string filePath)
=> await DownloadClosedCaptionTrackAsync(closedCaptionTrackInfo, filePath, null)
.ConfigureAwait(false);
#endif
}
// Methods for validation and parsing of IDs
public partial class YoutubeClient
{
/// <summary>
/// Verifies that the given string is syntactically a valid Youtube video ID
/// </summary>
public static bool ValidateVideoId(string videoId)
{
if (videoId.IsBlank())
return false;
if (videoId.Length != 11)
return false;
return !Regex.IsMatch(videoId, @"[^0-9a-zA-Z_\-]");
}
/// <summary>
/// Tries to parse video ID from a Youtube video URL
/// </summary>
public static bool TryParseVideoId(string videoUrl, out string videoId)
{
videoId = default(string);
if (videoUrl.IsBlank())
return false;
// https://www.youtube.com/watch?v=yIVRs6YSbOM
var regularMatch =
Regex.Match(videoUrl, @"youtube\..+?/watch.*?v=(.*?)(?:&|/|$)").Groups[1].Value;
if (regularMatch.IsNotBlank() && ValidateVideoId(regularMatch))
{
videoId = regularMatch;
return true;
}
// https://youtu.be/yIVRs6YSbOM
var shortMatch =
Regex.Match(videoUrl, @"youtu\.be/(.*?)(?:\?|&|/|$)").Groups[1].Value;
if (shortMatch.IsNotBlank() && ValidateVideoId(shortMatch))
{
videoId = shortMatch;
return true;
}
// https://www.youtube.com/embed/yIVRs6YSbOM
var embedMatch =
Regex.Match(videoUrl, @"youtube\..+?/embed/(.*?)(?:\?|&|/|$)").Groups[1].Value;
if (embedMatch.IsNotBlank() && ValidateVideoId(embedMatch))
{
videoId = embedMatch;
return true;
}
return false;
}
/// <summary>
/// Parses video ID from a Youtube video URL
/// </summary>
public static string ParseVideoId(string videoUrl)
{
if (videoUrl == null)
throw new ArgumentNullException(nameof(videoUrl));
var success = TryParseVideoId(videoUrl, out string result);
if (success)
return result;
throw new FormatException($"Could not parse video ID from given string [{videoUrl}]");
}
/// <summary>
/// Verifies that the given string is syntactically a valid Youtube playlist ID
/// </summary>
public static bool ValidatePlaylistId(string playlistId)
{
if (playlistId.IsBlank())
return false;
if (playlistId.Length != 2 &&
playlistId.Length != 13 &&
playlistId.Length != 18 &&
playlistId.Length != 24 &&
playlistId.Length != 26 &&
playlistId.Length != 34)
return false;
return !Regex.IsMatch(playlistId, @"[^0-9a-zA-Z_\-]");
}
/// <summary>
/// Tries to parse playlist ID from a Youtube playlist URL
/// </summary>
public static bool TryParsePlaylistId(string playlistUrl, out string playlistId)
{
playlistId = default(string);
if (playlistUrl.IsBlank())
return false;
// https://www.youtube.com/playlist?list=PLOU2XLYxmsIJGErt5rrCqaSGTMyyqNt2H
var regularMatch =
Regex.Match(playlistUrl, @"youtube\..+?/playlist.*?list=(.*?)(?:&|/|$)").Groups[1].Value;
if (regularMatch.IsNotBlank() && ValidatePlaylistId(regularMatch))
{
playlistId = regularMatch;
return true;
}
// https://www.youtube.com/watch?v=b8m9zhNAgKs&list=PL9tY0BWXOZFuFEG_GtOBZ8-8wbkH-NVAr
var compositeMatch =
Regex.Match(playlistUrl, @"youtube\..+?/watch.*?list=(.*?)(?:&|/|$)").Groups[1].Value;
if (compositeMatch.IsNotBlank() && ValidatePlaylistId(compositeMatch))
{
playlistId = compositeMatch;
return true;
}
// https://youtu.be/b8m9zhNAgKs/?list=PL9tY0BWXOZFuFEG_GtOBZ8-8wbkH-NVAr
var shortCompositeMatch =
Regex.Match(playlistUrl, @"youtu\.be/.*?/.*?list=(.*?)(?:&|/|$)").Groups[1].Value;
if (shortCompositeMatch.IsNotBlank() && ValidatePlaylistId(shortCompositeMatch))
{
playlistId = shortCompositeMatch;
return true;
}
// https://www.youtube.com/embed/b8m9zhNAgKs/?list=PL9tY0BWXOZFuFEG_GtOBZ8-8wbkH-NVAr
var embedCompositeMatch =
Regex.Match(playlistUrl, @"youtube\..+?/embed/.*?/.*?list=(.*?)(?:&|/|$)").Groups[1].Value;
if (embedCompositeMatch.IsNotBlank() && ValidatePlaylistId(embedCompositeMatch))
{
playlistId = embedCompositeMatch;
return true;
}
return false;
}
/// <summary>
/// Parses playlist ID from a Youtube playlist URL
/// </summary>
public static string ParsePlaylistId(string playlistUrl)
{
if (playlistUrl == null)
throw new ArgumentNullException(nameof(playlistUrl));
var success = TryParsePlaylistId(playlistUrl, out string result);
if (success)
return result;
throw new FormatException($"Could not parse playlist ID from given string [{playlistUrl}]");
}
/// <summary>
/// Verifies that the given string is syntactically a valid Youtube channel ID
/// </summary>
public static bool ValidateChannelId(string channelId)
{
if (channelId.IsBlank())
return false;
if (channelId.Length != 24)
return false;
if (!channelId.StartsWith("UC", StringComparison.OrdinalIgnoreCase))
return false;
return !Regex.IsMatch(channelId, @"[^0-9a-zA-Z_\-]");
}
/// <summary>
/// Tries to parse channel ID from a Youtube channel URL
/// </summary>
public static bool TryParseChannelId(string channelUrl, out string channelId)
{
channelId = default(string);
if (channelUrl.IsBlank())
return false;
// https://www.youtube.com/channel/UC3xnGqlcL3y-GXz5N3wiTJQ
var regularMatch =
Regex.Match(channelUrl, @"youtube\..+?/channel/(.*?)(?:&|/|$)").Groups[1].Value;
if (regularMatch.IsNotBlank() && ValidateChannelId(regularMatch))
{
channelId = regularMatch;
return true;
}
return false;
}
/// <summary>
/// Parses channel ID from a Youtube channel URL
/// </summary>
public static string ParseChannelId(string channelUrl)
{
if (channelUrl == null)
throw new ArgumentNullException(nameof(channelUrl));
var success = TryParseChannelId(channelUrl, out string result);
if (success)
return result;
throw new FormatException($"Could not parse channel ID from given string [{channelUrl}]");
}
}
}