From bd2d88c96e29b046df65578d608ecee26c957552 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 11 Jul 2022 00:52:59 +0200 Subject: [PATCH] add another up2k-hook example --- contrib/plugins/up2k-hook-ytid.js | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 contrib/plugins/up2k-hook-ytid.js diff --git a/contrib/plugins/up2k-hook-ytid.js b/contrib/plugins/up2k-hook-ytid.js new file mode 100644 index 00000000..00961237 --- /dev/null +++ b/contrib/plugins/up2k-hook-ytid.js @@ -0,0 +1,69 @@ +// way more specific example -- +// assumes all files dropped into the uploader have a youtube-id somewhere in the filename, +// locates the youtube-ids and passes them to an API which returns a list of IDS which should be uploaded + +function up2k_namefilter(good_files, nil_files, bad_files, hooks) { + var filenames = [], + file_lists = [good_files, nil_files, bad_files]; + + for (var lst of file_lists) + for (var ent of lst) + filenames.push(ent[1]); + + var yt_ids = new Set(); + for (var lst of file_lists) + for (var ent of lst) { + var m, name = ent[1]; + while (true) { + // some ytdl fork did %(title)-%(id).%(ext) ... + m = /(?:^|[^\w])([\w-]{11})(?:$|[^\w-])/.exec(name); + if (!m) + break; + + yt_ids.add(m[1]); + name = name.replace(m[1], ''); + } + } + + toast.inf(5, `running query for ${yt_ids.size} videos...`); + + var xhr = new XHR(); + xhr.open('POST', '/ytq', true); + xhr.setRequestHeader('Content-Type', 'text/plain'); + xhr.onload = xhr.onerror = function () { + if (this.status != 200) + return toast.err(0, `sorry, database query failed ;_;\n\nplease let us know so we can look at it, thx!!\n\nerror ${this.status}: ${(this.response && this.response.err) || this.responseText}`); + + var new_lists = [], + ptn = new RegExp(this.responseText.trim().split('\n').join('|') || '\n'), + nothing_to_do = true, + n_skip = 0; + + for (var lst of file_lists) { + var keep = []; + new_lists.push(keep); + + for (var ent of lst) + if (ptn.exec(ent[1])) + keep.push(ent); + else + n_skip++; + + if (keep.length) + nothing_to_do = false; + } + + if (nothing_to_do) + return modal.alert('Good news -- turns out we already have all those videos.\n\nBut thank you for checking in!'); + else if (n_skip) + toast.inf(0, `skipped ${n_skip} files which already exist on the server`); + + [good_files, nil_files, bad_files] = new_lists; + hooks[0](good_files, nil_files, bad_files, hooks.slice(1)); + }; + xhr.send(Array.from(yt_ids).join('\n')); +} + +up2k_hooks.push(function () { + up2k.gotallfiles.unshift(up2k_namefilter); +});