YT: do not check player ID if actions are cached

This commit is contained in:
Rafał Dzięgiel
2021-03-15 13:07:12 +01:00
parent 06f8e5d259
commit 5b6141ee8c

View File

@@ -52,7 +52,8 @@ var YouTubeClient = GObject.registerClass({
debug(`obtaining YouTube video info: ${videoId}`); debug(`obtaining YouTube video info: ${videoId}`);
this.downloadingVideoId = videoId; this.downloadingVideoId = videoId;
const [info, isAborted] = await this._getInfoPromise(videoId).catch(debug); let [info, isAborted] = await this._getInfoPromise(videoId).catch(debug);
if(!info) { if(!info) {
if(isAborted) if(isAborted)
return reject(new Error('download aborted')); return reject(new Error('download aborted'));
@@ -84,18 +85,19 @@ var YouTubeClient = GObject.registerClass({
if(!info.streamingData.adaptiveFormats) if(!info.streamingData.adaptiveFormats)
info.streamingData.adaptiveFormats = []; info.streamingData.adaptiveFormats = [];
const isCipher = this._getIsCipher(info.streamingData); if(this._getIsCipher(info.streamingData)) {
if(isCipher) {
debug('video requires deciphering'); debug('video requires deciphering');
/* Decipher actions do not change too often, so try
* to reuse without triggering too many requests ban */
let actions = this.cachedSig.actions;
if(!actions) {
const embedUri = `https://www.youtube.com/embed/${videoId}`; const embedUri = `https://www.youtube.com/embed/${videoId}`;
const [body, isAbortedBody] = const [body, isAbortedBody] =
await this._downloadDataPromise(embedUri).catch(debug); await this._downloadDataPromise(embedUri).catch(debug);
if(isAbortedBody) if(isAbortedBody)
break; break;
/* We need matching info, so start from beginning */
if(!body) if(!body)
continue; continue;
@@ -116,12 +118,6 @@ var YouTubeClient = GObject.registerClass({
debug(`found player URI: ${ytUri}`); debug(`found player URI: ${ytUri}`);
const ytId = ytPath.split('/').find(el => Misc.isHex(el)); const ytId = ytPath.split('/').find(el => Misc.isHex(el));
let actions;
if(this.cachedSig.id === ytId) {
debug('reusing cached cipher actions');
actions = this.cachedSig.actions;
}
/* TODO: load cache from file */ /* TODO: load cache from file */
@@ -135,7 +131,6 @@ var YouTubeClient = GObject.registerClass({
actions = YTDL.sig.extractActions(pBody); actions = YTDL.sig.extractActions(pBody);
this._createCacheFileAsync(ytId, actions); this._createCacheFileAsync(ytId, actions);
} }
if(!actions || !actions.length) { if(!actions || !actions.length) {
debug(new Error('could not extract decipher actions')); debug(new Error('could not extract decipher actions'));
break; break;
@@ -145,7 +140,8 @@ var YouTubeClient = GObject.registerClass({
if(this.cachedSig.id !== ytId) { if(this.cachedSig.id !== ytId) {
this.cachedSig.id = ytId; this.cachedSig.id = ytId;
this.cachedSig.actions = actions; this.cachedSig.actions = actions;
debug('set current decipher actions for reuse'); debug('remembered current decipher actions for reuse');
}
} }
const isDeciphered = this._decipherStreamingData( const isDeciphered = this._decipherStreamingData(
@@ -214,6 +210,8 @@ var YouTubeClient = GObject.registerClass({
debug(`got chunk of data, length: ${chunk.length}`); debug(`got chunk of data, length: ${chunk.length}`);
const chunkData = chunk.get_data(); const chunkData = chunk.get_data();
if(!chunkData) return;
data += (chunkData instanceof Uint8Array) data += (chunkData instanceof Uint8Array)
? ByteArray.toString(chunkData) ? ByteArray.toString(chunkData)
: chunkData; : chunkData;