]> git.gir.st - subscriptionfeed.git/blob - app/update-subscriptions.py
move flask secret_key to config.ini
[subscriptionfeed.git] / app / update-subscriptions.py
1 #!/bin/sh
2 ''':'
3 . /opt/yt/venv/bin/activate
4 exec python $0 "$@"
5 ':'''
6
7 import sys
8 import time
9 import sqlite3
10 import requests
11 from common import *
12
13 webhook = cf['websub']['public_uri']
14 lease = cf['websub']['lease']
15 hmackey = cf['websub']['hmac_key']
16
17 with sqlite3.connect(cf['global']['database']) as conn:
18 c = conn.cursor()
19 c.execute("""
20 SELECT DISTINCT s.channel_id
21 FROM subscriptions AS s LEFT JOIN websub AS w
22 ON s.channel_id = w.channel_id
23 WHERE subscribed_until < datetime('now', '+12 hours')
24 OR subscribed_until IS NULL
25 ORDER BY subscribed_until
26 -- LIMIT 1
27 """)
28 for (channel_id,) in c.fetchall():
29 if '-v' in sys.argv: sys.stderr.write(f'updating {channel_id}\n')
30
31 version, timestamp, nonce, sig = "v1", int(time.time()), 0, "x" # TODO:sig,nonce
32 r = requests.post("https://pubsubhubbub.appspot.com/subscribe", data={
33 "hub.callback": f"{webhook}/websub/{version}/{timestamp}/{nonce}/{channel_id}/{sig}",
34 "hub.topic": f"https://www.youtube.com/xml/feeds/videos.xml?channel_id={channel_id}",
35 # XXX: https://www.youtube.com/xml/feeds/videos.xml?playlist_id=PL...
36 "hub.verify": "async", # nonstandard?
37 "hub.mode": "subscribe",
38 "hub.lease_seconds": lease,
39 "hub.secret": hmackey,
40 })
41 if '-v' in sys.argv and not r.ok: sys.stderr.write(f'FAILED {channel_id}: {r.text}\n')
42
43 # mark the channel as subscription pending (auto-updates timestamp):
44 # 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.
45 #$dbh->prepare("INSERT OR REPLACE INTO websub (channel_id, pending)
46 # VALUES (?, 1)")->execute($channel_id);
47 time.sleep(60)
Imprint / Impressum