Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Commit 86a5c9c

Browse files
committed
Decipher signatures only if they require it
1 parent 219c0e9 commit 86a5c9c

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

YoutubeExtractor/YoutubeExtractor/DownloadUrlResolver.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static void DecryptDownloadUrl(VideoInfo videoInfo)
4545
}
4646

4747
videoInfo.DownloadUrl = HttpHelper.ReplaceQueryStringParameter(videoInfo.DownloadUrl, SignatureQuery, decrypted);
48+
videoInfo.RequiresDecryption = false;
4849
}
4950
}
5051

@@ -88,7 +89,7 @@ public static IEnumerable<VideoInfo> GetDownloadUrls(string videoUrl, bool decry
8889

8990
string videoTitle = GetVideoTitle(json);
9091

91-
IEnumerable<Uri> downloadUrls = ExtractDownloadUrls(json);
92+
IEnumerable<ExtractionInfo> downloadUrls = ExtractDownloadUrls(json);
9293

9394
IEnumerable<VideoInfo> infos = GetVideoInfos(downloadUrls, videoTitle).ToList();
9495

@@ -98,7 +99,7 @@ public static IEnumerable<VideoInfo> GetDownloadUrls(string videoUrl, bool decry
9899
{
99100
info.HtmlPlayerVersion = htmlPlayerVersion;
100101

101-
if (decryptSignature)
102+
if (decryptSignature && info.RequiresDecryption)
102103
{
103104
DecryptDownloadUrl(info);
104105
}
@@ -168,7 +169,7 @@ public static bool TryNormalizeYoutubeUrl(string url, out string normalizedUrl)
168169
return true;
169170
}
170171

171-
private static IEnumerable<Uri> ExtractDownloadUrls(JObject json)
172+
private static IEnumerable<ExtractionInfo> ExtractDownloadUrls(JObject json)
172173
{
173174
string[] splitByUrls = GetStreamMap(json).Split(',');
174175
string[] adaptiveFmtSplitByUrls = GetAdaptiveStreamMap(json).Split(',');
@@ -179,8 +180,11 @@ private static IEnumerable<Uri> ExtractDownloadUrls(JObject json)
179180
IDictionary<string, string> queries = HttpHelper.ParseQueryString(s);
180181
string url;
181182

183+
bool requiresDecryption = false;
184+
182185
if (queries.ContainsKey("s") || queries.ContainsKey("sig"))
183186
{
187+
requiresDecryption = queries.ContainsKey("s");
184188
string signature = queries.ContainsKey("s") ? queries["s"] : queries["sig"];
185189

186190
url = string.Format("{0}&{1}={2}", queries["url"], SignatureQuery, signature);
@@ -198,7 +202,7 @@ private static IEnumerable<Uri> ExtractDownloadUrls(JObject json)
198202
url = HttpHelper.UrlDecode(url);
199203
url = HttpHelper.UrlDecode(url);
200204

201-
yield return new Uri(url);
205+
yield return new ExtractionInfo { RequiresDecryption = requiresDecryption, Uri = new Uri(url) };
202206
}
203207
}
204208

@@ -242,13 +246,13 @@ private static string GetStreamMap(JObject json)
242246
return streamMapString;
243247
}
244248

245-
private static IEnumerable<VideoInfo> GetVideoInfos(IEnumerable<Uri> downloadUrls, string videoTitle)
249+
private static IEnumerable<VideoInfo> GetVideoInfos(IEnumerable<ExtractionInfo> extractionInfos, string videoTitle)
246250
{
247251
var downLoadInfos = new List<VideoInfo>();
248252

249-
foreach (Uri url in downloadUrls)
253+
foreach (ExtractionInfo extractionInfo in extractionInfos)
250254
{
251-
string itag = HttpHelper.ParseQueryString(url.Query)["itag"];
255+
string itag = HttpHelper.ParseQueryString(extractionInfo.Uri.Query)["itag"];
252256

253257
int formatCode = int.Parse(itag);
254258

@@ -258,16 +262,17 @@ private static IEnumerable<VideoInfo> GetVideoInfos(IEnumerable<Uri> downloadUrl
258262
{
259263
info = new VideoInfo(info)
260264
{
261-
DownloadUrl = url.ToString(),
262-
Title = videoTitle
265+
DownloadUrl = extractionInfo.Uri.ToString(),
266+
Title = videoTitle,
267+
RequiresDecryption = extractionInfo.RequiresDecryption
263268
};
264269
}
265270

266271
else
267272
{
268273
info = new VideoInfo(formatCode)
269274
{
270-
DownloadUrl = url.ToString()
275+
DownloadUrl = extractionInfo.Uri.ToString()
271276
};
272277
}
273278

@@ -313,5 +318,12 @@ private static void ThrowYoutubeParseException(Exception innerException)
313318
"This may be due to a change of the Youtube page structure.\n" +
314319
"Please report this bug at www.github.com/flagbug/YoutubeExtractor/issues", innerException);
315320
}
321+
322+
private class ExtractionInfo
323+
{
324+
public bool RequiresDecryption { get; set; }
325+
326+
public Uri Uri { get; set; }
327+
}
316328
}
317329
}

YoutubeExtractor/YoutubeExtractor/VideoInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ public bool CanExtractAudio
134134

135135
public bool Is3D { get; private set; }
136136

137+
/// <summary>
138+
/// Gets a value indicating whether this video info requires a signature decryption before
139+
/// the download URL can be used.
140+
///
141+
/// This can be achieved with the <see cref="DownloadUrlResolver.DecryptDownloadUrl"/>
142+
/// </summary>
143+
public bool RequiresDecryption { get; internal set; }
144+
137145
/// <summary>
138146
/// Gets the resolution of the video.
139147
/// </summary>

0 commit comments

Comments
 (0)