From 918157be0402554060352c3f816909a94cb0146e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Tue, 16 Mar 2021 13:12:44 +0100 Subject: [PATCH] Cooooookies!!! --- src/fileOps.js | 29 +++++++++++++++++++++++++++++ src/main.js | 1 - src/youtube.js | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/fileOps.js diff --git a/src/fileOps.js b/src/fileOps.js new file mode 100644 index 00000000..f6ff5768 --- /dev/null +++ b/src/fileOps.js @@ -0,0 +1,29 @@ +const { Gio, GLib } = imports.gi; +const Debug = imports.src.debug; +const Misc = imports.src.misc; + +const { debug } = Debug; + +Gio._promisify(Gio._LocalFilePrototype, 'make_directory_async', 'make_directory_finish'); + +function createCacheDirPromise() +{ + return new Promise(async (resolve, reject) => { + const cacheDir = Gio.File.new_for_path( + GLib.get_user_cache_dir() + '/' + Misc.appId + ); + + if(cacheDir.query_exists(null)) + return resolve(cacheDir); + + const dirCreated = await cacheDir.make_directory_async( + GLib.PRIORITY_DEFAULT, + null, + ).catch(debug); + + if(!dirCreated) + return reject(new Error(`could not create dir: ${cacheDir.get_path()}`)); + + resolve(cacheDir); + }); +} diff --git a/src/main.js b/src/main.js index 31c16c66..a6f6b6d4 100644 --- a/src/main.js +++ b/src/main.js @@ -5,7 +5,6 @@ const { Gio, Gst } = imports.gi; Gst.init(null); Gio._promisify(Gio._LocalFilePrototype, 'load_bytes_async', 'load_bytes_finish'); -Gio._promisify(Gio._LocalFilePrototype, 'make_directory_async', 'make_directory_finish'); Gio._promisify(Gio._LocalFilePrototype, 'replace_contents_bytes_async', 'replace_contents_finish'); const { App } = imports.src.app; diff --git a/src/youtube.js b/src/youtube.js index 4138e407..5e2616af 100644 --- a/src/youtube.js +++ b/src/youtube.js @@ -1,11 +1,18 @@ const { Gio, GLib, GObject, Gst, Soup } = imports.gi; const ByteArray = imports.byteArray; const Debug = imports.src.debug; +const FileOps = imports.src.fileOps; const Misc = imports.src.misc; const YTDL = imports.src.assets['node-ytdl-core']; const { debug } = Debug; +const InitAsyncState = { + NONE: 0, + IN_PROGRESS: 1, + DONE: 2, +}; + var YouTubeClient = GObject.registerClass({ Signals: { 'info-resolved': { @@ -19,6 +26,7 @@ var YouTubeClient = GObject.registerClass({ super._init({ timeout: 5, }); + this.initAsyncState = InitAsyncState.NONE; /* videoID of current active download */ this.downloadingVideoId = null; @@ -47,6 +55,27 @@ var YouTubeClient = GObject.registerClass({ this.abort(); + /* Prevent doing this code more than once at a time */ + if(this.initAsyncState === InitAsyncState.NONE) { + this.initAsyncState = InitAsyncState.IN_PROGRESS; + + debug('loading cookies DB'); + const cacheDir = await FileOps.createCacheDirPromise().catch(debug); + if(!cacheDir) { + this.initAsyncState = InitAsyncState.NONE; + return reject(new Error('could not create cookies DB')); + } + + const cookiesDB = new Soup.CookieJarDB({ + filename: cacheDir.get_child('cookies.sqlite').get_path(), + read_only: false, + }); + this.add_feature(cookiesDB); + debug('successfully loaded cookies DB'); + + this.initAsyncState = InitAsyncState.DONE; + } + let tries = 2; while(tries--) { debug(`obtaining YouTube video info: ${videoId}`); @@ -55,8 +84,10 @@ var YouTubeClient = GObject.registerClass({ let result = await this._getInfoPromise(videoId).catch(debug); if(!result || !result.data) { - if(result && result.isAborted) - return reject(new Error('download aborted')); + if(result && result.isAborted) { + debug(new Error('download aborted')); + break; + } debug(`failed, remaining tries: ${tries}`); continue;