YT: cache current decipher actions

This commit is contained in:
Rafostar
2021-03-14 21:00:18 +01:00
parent 6370e1126b
commit 06f8e5d259
2 changed files with 45 additions and 2 deletions

View File

@@ -109,3 +109,8 @@ function decodeURIPlus(uri)
{ {
return decodeURI(uri.replace(/\+/g, ' ')); return decodeURI(uri.replace(/\+/g, ' '));
} }
function isHex(num)
{
return Boolean(num.match(/[0-9a-f]+$/i));
}

View File

@@ -1,6 +1,7 @@
const { GLib, GObject, Gst, Soup } = imports.gi; const { Gio, GLib, GObject, Gst, Soup } = imports.gi;
const ByteArray = imports.byteArray; const ByteArray = imports.byteArray;
const Debug = imports.src.debug; const Debug = imports.src.debug;
const Misc = imports.src.misc;
const YTDL = imports.src.assets['node-ytdl-core']; const YTDL = imports.src.assets['node-ytdl-core'];
const { debug } = Debug; const { debug } = Debug;
@@ -23,6 +24,10 @@ var YouTubeClient = GObject.registerClass({
this.downloadingVideoId = null; this.downloadingVideoId = null;
this.lastInfo = null; this.lastInfo = null;
this.cachedSig = {
id: null,
actions: null,
};
} }
getVideoInfoPromise(videoId) getVideoInfoPromise(videoId)
@@ -110,9 +115,16 @@ var YouTubeClient = GObject.registerClass({
} }
debug(`found player URI: ${ytUri}`); debug(`found player URI: ${ytUri}`);
/* TODO: cache */ const ytId = ytPath.split('/').find(el => Misc.isHex(el));
let actions; let actions;
if(this.cachedSig.id === ytId) {
debug('reusing cached cipher actions');
actions = this.cachedSig.actions;
}
/* TODO: load cache from file */
if(!actions) { if(!actions) {
const [pBody, isAbortedPlayer] = const [pBody, isAbortedPlayer] =
await this._downloadDataPromise(ytUri).catch(debug); await this._downloadDataPromise(ytUri).catch(debug);
@@ -121,6 +133,7 @@ var YouTubeClient = GObject.registerClass({
break; break;
} }
actions = YTDL.sig.extractActions(pBody); actions = YTDL.sig.extractActions(pBody);
this._createCacheFileAsync(ytId, actions);
} }
if(!actions || !actions.length) { if(!actions || !actions.length) {
@@ -128,6 +141,13 @@ var YouTubeClient = GObject.registerClass({
break; break;
} }
debug('successfully obtained decipher actions'); debug('successfully obtained decipher actions');
if(this.cachedSig.id !== ytId) {
this.cachedSig.id = ytId;
this.cachedSig.actions = actions;
debug('set current decipher actions for reuse');
}
const isDeciphered = this._decipherStreamingData( const isDeciphered = this._decipherStreamingData(
info.streamingData, actions info.streamingData, actions
); );
@@ -394,6 +414,24 @@ var YouTubeClient = GObject.registerClass({
return `${url}&${sig}=${key}`; return `${url}&${sig}=${key}`;
} }
_createCacheFileAsync(ytId, actions)
{
const cachePath = GLib.get_user_cache_dir() + '/' + ytId;
const cacheFile = Gio.File.new_for_path(cachePath);
debug('saving cipher actions to cache file');
cacheFile.replace_contents_bytes_async(
GLib.Bytes.new_take(actions),
null,
false,
Gio.FileCreateFlags.NONE,
null
)
.then(() => debug('saved cache file'))
.catch(debug);
}
}); });
function checkYouTubeUri(uri) function checkYouTubeUri(uri)