Cooooookies!!!

This commit is contained in:
Rafał Dzięgiel
2021-03-16 13:12:44 +01:00
parent 72b55939b4
commit 918157be04
3 changed files with 62 additions and 3 deletions

29
src/fileOps.js Normal file
View File

@@ -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);
});
}

View File

@@ -5,7 +5,6 @@ const { Gio, Gst } = imports.gi;
Gst.init(null); Gst.init(null);
Gio._promisify(Gio._LocalFilePrototype, 'load_bytes_async', 'load_bytes_finish'); 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'); Gio._promisify(Gio._LocalFilePrototype, 'replace_contents_bytes_async', 'replace_contents_finish');
const { App } = imports.src.app; const { App } = imports.src.app;

View File

@@ -1,11 +1,18 @@
const { Gio, 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 FileOps = imports.src.fileOps;
const Misc = imports.src.misc; 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;
const InitAsyncState = {
NONE: 0,
IN_PROGRESS: 1,
DONE: 2,
};
var YouTubeClient = GObject.registerClass({ var YouTubeClient = GObject.registerClass({
Signals: { Signals: {
'info-resolved': { 'info-resolved': {
@@ -19,6 +26,7 @@ var YouTubeClient = GObject.registerClass({
super._init({ super._init({
timeout: 5, timeout: 5,
}); });
this.initAsyncState = InitAsyncState.NONE;
/* videoID of current active download */ /* videoID of current active download */
this.downloadingVideoId = null; this.downloadingVideoId = null;
@@ -47,6 +55,27 @@ var YouTubeClient = GObject.registerClass({
this.abort(); 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; let tries = 2;
while(tries--) { while(tries--) {
debug(`obtaining YouTube video info: ${videoId}`); debug(`obtaining YouTube video info: ${videoId}`);
@@ -55,8 +84,10 @@ var YouTubeClient = GObject.registerClass({
let result = await this._getInfoPromise(videoId).catch(debug); let result = await this._getInfoPromise(videoId).catch(debug);
if(!result || !result.data) { if(!result || !result.data) {
if(result && result.isAborted) if(result && result.isAborted) {
return reject(new Error('download aborted')); debug(new Error('download aborted'));
break;
}
debug(`failed, remaining tries: ${tries}`); debug(`failed, remaining tries: ${tries}`);
continue; continue;