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