#!/bin/sh ''':' . /opt/yt/venv/bin/activate exec python $0 "$@" ':''' import sys import time import sqlite3 import requests from common import * webhook = cf['websub']['public_uri'] lease = cf['websub']['lease'] hmackey = cf['websub']['hmac_key'] with sqlite3.connect(cf['global']['database']) as conn: c = conn.cursor() c.execute(""" SELECT DISTINCT s.channel_id FROM subscriptions AS s LEFT JOIN websub AS w ON s.channel_id = w.channel_id WHERE subscribed_until < datetime('now', '+12 hours') OR subscribed_until IS NULL ORDER BY subscribed_until -- LIMIT 1 """) for (channel_id,) in c.fetchall(): if '-v' in sys.argv: sys.stderr.write(f'updating {channel_id}\n') version, timestamp, nonce, sig = "v1", int(time.time()), 0, "x" # TODO:sig,nonce r = requests.post("https://pubsubhubbub.appspot.com/subscribe", data={ "hub.callback": f"{webhook}/websub/{version}/{timestamp}/{nonce}/{channel_id}/{sig}", "hub.topic": f"https://www.youtube.com/xml/feeds/videos.xml?channel_id={channel_id}", # XXX: https://www.youtube.com/xml/feeds/videos.xml?playlist_id=PL... "hub.verify": "async", # nonstandard? "hub.mode": "subscribe", "hub.lease_seconds": lease, "hub.secret": hmackey, }) if '-v' in sys.argv and not r.ok: sys.stderr.write(f'FAILED {channel_id}: {r.text}\n') # mark the channel as subscription pending (auto-updates timestamp): # NOTE: the callback happens even before the response of the postf call. we then overwrite the final state with the pending state again, so this is commented out. #$dbh->prepare("INSERT OR REPLACE INTO websub (channel_id, pending) # VALUES (?, 1)")->execute($channel_id); time.sleep(60)