From 228f71708bae41878fd994788dd886956d695ff1 Mon Sep 17 00:00:00 2001 From: ed Date: Sun, 8 Aug 2021 02:47:04 +0200 Subject: [PATCH] improve youtube collector/parser --- bin/mtag/res/yt-ipr.user.js | 32 ++++++++++++++++++++++++++++++++ bin/mtag/yt-ipr.py | 18 ++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 bin/mtag/res/yt-ipr.user.js diff --git a/bin/mtag/res/yt-ipr.user.js b/bin/mtag/res/yt-ipr.user.js new file mode 100644 index 00000000..b81947e0 --- /dev/null +++ b/bin/mtag/res/yt-ipr.user.js @@ -0,0 +1,32 @@ +// ==UserScript== +// @name youtube-playerdata-hub +// @match https://youtube.com/* +// @match https://*.youtube.com/* +// @version 1.0 +// @grant GM_addStyle +// ==/UserScript== + +function main() { + var sent = {}; + function send(txt) { + if (sent[txt]) + return; + + fetch('https://127.0.0.1:3923/playerdata?_=' + Date.now(), { method: "PUT", body: txt }); + console.log('[yt-ipr] yeet %d bytes', txt.length); + sent[txt] = 1; + } + + function collect() { + setTimeout(collect, 60 * 1000); + var pd = document.querySelector('ytd-watch-flexy'); + if (pd) + send(JSON.stringify(pd.playerData)); + } + setTimeout(collect, 5000); +} + +var scr = document.createElement('script'); +scr.textContent = '(' + main.toString() + ')();'; +(document.head || document.getElementsByTagName('head')[0]).appendChild(scr); +console.log('[yt-ipr] a'); diff --git a/bin/mtag/yt-ipr.py b/bin/mtag/yt-ipr.py index 3f4c2f6d..a7eba6bf 100644 --- a/bin/mtag/yt-ipr.py +++ b/bin/mtag/yt-ipr.py @@ -2,6 +2,7 @@ import re import sys +import gzip import json from datetime import datetime @@ -14,20 +15,17 @@ example usage: :c,mtp=yt-id,yt-title,yt-author,yt-channel,yt-views,yt-private,yt-expires=bin/mtag/yt-ipr.py :c,mte=yt-id,yt-title,yt-author,yt-channel,yt-views,yt-private,yt-expires -quick userscript to push them across: - console.log('a'); - setTimeout(function() { - for (var scr of document.querySelectorAll('script[nonce]')) - if (scr.innerHTML.indexOf('manifest.googlevideo.com/api/manifest')>0) - fetch('https://127.0.0.1:3923/playerdata', {method:"PUT", body: scr.innerHTML}); - }, 10*1000); - +see res/yt-ipr.user.js for the example userscript to go with this """ def main(): - with open(sys.argv[1], "r", encoding="utf-8") as f: - txt = f.read() + try: + with gzip.open(sys.argv[1], "rt", encoding="utf-8", errors="replace") as f: + txt = f.read() + except: + with open(sys.argv[1], "r", encoding="utf-8", errors="replace") as f: + txt = f.read() txt = "{" + txt.split("{", 1)[1]