Implement YT websub subscription

master
Nikola Forró 4 years ago
parent 65b2b6d8e5
commit 6cbbd3898f

@ -12,6 +12,7 @@ import pathlib
import flask import flask
import persistqueue import persistqueue
import tweepy import tweepy
import requests
import xmltodict import xmltodict
from twitivity import Twitivity, TwitivityError from twitivity import Twitivity, TwitivityError
@ -39,8 +40,11 @@ callback_url = config.get('Twitter', 'callback_url')
webhook_url = config.get('Twitter', 'webhook_url') webhook_url = config.get('Twitter', 'webhook_url')
environment_name = config.get('Twitter', 'environment_name') environment_name = config.get('Twitter', 'environment_name')
yt_webhook_url = config.get('YouTube', 'webhook_url')
yt_channel_id = config.get('YouTube', 'channel_id') yt_channel_id = config.get('YouTube', 'channel_id')
yt_websub_topic = 'https://www.youtube.com/xml/feeds/videos.xml?channel_id={0}'.format(yt_channel_id)
def update_twitter_tokens(user_id, access_token): def update_twitter_tokens(user_id, access_token):
path = pathlib.Path(storage_path, 'tokens.json') path = pathlib.Path(storage_path, 'tokens.json')
@ -79,6 +83,16 @@ def setup_twitter_webhook(api):
return True return True
def setup_youtube_webhook():
r = requests.post('https://pubsubhubbub.appspot.com/subscribe', data={
'hub.mode': 'subscribe',
'hub.topic': yt_websub_topic,
'hub.callback': yt_webhook_url,
'hub.verify': 'async',
})
return r.status_code in (202, 204)
def push_event(data): def push_event(data):
queue = persistqueue.SQLiteQueue(storage_path) queue = persistqueue.SQLiteQueue(storage_path)
queue.put(data) queue.put(data)
@ -145,8 +159,7 @@ def youtube_webhook_get():
mode = flask.request.args.get('hub.mode') mode = flask.request.args.get('hub.mode')
topic = flask.request.args.get('hub.topic') topic = flask.request.args.get('hub.topic')
challenge = flask.request.args.get('hub.challenge') challenge = flask.request.args.get('hub.challenge')
if (topic != 'https://www.youtube.com/xml/feeds/videos.xml?channel_id={0}'.format(yt_channel_id) or if topic != yt_websub_topic or mode not in ('subscribe', 'unsubscribe') or not challenge:
mode not in ('subscribe', 'unsubscribe') or not challenge):
return 'Invalid request', 403 return 'Invalid request', 403
return challenge, 200 return challenge, 200

Loading…
Cancel
Save