Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
Update signature deciphering
Browse files Browse the repository at this point in the history
  • Loading branch information
HaarigerHarald committed Jun 11, 2019
1 parent 39dc2fe commit bccaf9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testUsualVideo() throws Throwable {

@Test
public void testUnembeddable() throws Throwable {
VideoMeta expMeta = new VideoMeta("QH4VHl2uQ9o", "Match Chain Reaction Amazing Fire Art", "BLACKHAND",
VideoMeta expMeta = new VideoMeta("QH4VHl2uQ9o", "Match Chain Reaction Amazing Fire Art - real ghost rider", "BLACKHAND",
"UCl9nsRuGenStMDZfD95w85A", 331, 0, false);
extractorTest("https://www.youtube.com/watch?v=QH4VHl2uQ9o", expMeta);
extractorTestDashManifest("https://www.youtube.com/watch?v=QH4VHl2uQ9o");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public abstract class YouTubeExtractor extends AsyncTask<String, Void, SparseArr
private static final Pattern patItag = Pattern.compile("itag=([0-9]+?)([&,])");
private static final Pattern patEncSig = Pattern.compile("s=([0-9A-F|.]{10,}?)([&,\"])");
private static final Pattern patIsSigEnc = Pattern.compile("s%3D([0-9A-F|.]{10,}?)(%26|%2C)");
private static final Pattern patUrl = Pattern.compile("url=(.+?)([&,])");
private static final Pattern patEncSig2 = Pattern.compile("(\\A|&|\")s=([0-9A-Za-z\\-_=%]{10,}?)([&,\"])");
private static final Pattern patIsSigEnc2 = Pattern.compile("(%26|%3F|%2C)s%3D([0-9A-Za-z\\-_=%]{10,}?)(%26|%2C|\\z)");
private static final Pattern patUrl = Pattern.compile("url=(.+?)([&,\"])");

private static final Pattern patVariableFunction = Pattern.compile("([{; =])([a-zA-Z$][a-zA-Z0-9$]{0,2})\\.([a-zA-Z$][a-zA-Z0-9$]{0,2})\\(");
private static final Pattern patFunction = Pattern.compile("([{; =])([a-zA-Z$_][a-zA-Z0-9$]{0,2})\\(");
Expand Down Expand Up @@ -279,9 +281,8 @@ private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedExcep
// exists int the stream_map.
boolean sigEnc = true, statusFail = false;
if(streamMap != null && streamMap.contains(STREAM_MAP_STRING)){
String streamMapSub = streamMap.substring(streamMap.indexOf(STREAM_MAP_STRING));
mat = patIsSigEnc.matcher(streamMapSub);
if(!mat.find()) {

if(!patIsSigEnc2.matcher(streamMap).find() && !patIsSigEnc.matcher(streamMap).find()) {
sigEnc = false;

if (!patStatusOk.matcher(streamMap).find())
Expand Down Expand Up @@ -354,6 +355,7 @@ private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedExcep
streamMap = URLDecoder.decode(streamMap, "UTF-8");
}

boolean oldSignature = false;
streams = streamMap.split(",|"+STREAM_MAP_STRING+"|&adaptive_fmts=");
SparseArray<YtFile> ytFiles = new SparseArray<>();
for (String encStream : streams) {
Expand Down Expand Up @@ -382,9 +384,16 @@ private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedExcep
}

if (curJsFileName != null) {
mat = patEncSig.matcher(stream);
mat = patEncSig2.matcher(stream);
if (mat.find()) {
encSignatures.append(itag, mat.group(1));
encSignatures.append(itag, URLDecoder.decode(mat.group(2), "UTF-8"));
} else {
mat = patEncSig.matcher(stream);
if (mat.find())
{
encSignatures.append(itag, mat.group(1));
oldSignature = true;
}
}
}
mat = patUrl.matcher(encStream);
Expand All @@ -402,6 +411,7 @@ private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedExcep
}

if (encSignatures != null) {

if (LOGGING)
Log.d(LOG_TAG, "Decipher signatures: " + encSignatures.size()+ ", videos: " + ytFiles.size());
String signature;
Expand All @@ -425,7 +435,7 @@ private SparseArray<YtFile> getStreamUrls() throws IOException, InterruptedExcep
dashMpdUrl = dashMpdUrl.replace("/s/" + encSignatures.get(key), "/signature/" + sigs[i]);
} else {
String url = ytFiles.get(key).getUrl();
url += "&signature=" + sigs[i];
url += (oldSignature ? "&signature=" : "&sig=") + sigs[i];
YtFile newFile = new YtFile(FORMAT_MAP.get(key), url);
ytFiles.put(key, newFile);
}
Expand Down

0 comments on commit bccaf9e

Please sign in to comment.