diff --git a/endpoint.py b/endpoint.py index 5069b25..ebae3d5 100644 --- a/endpoint.py +++ b/endpoint.py @@ -12,6 +12,7 @@ import pathlib import flask import persistqueue import tweepy +import requests import xmltodict from twitivity import Twitivity, TwitivityError @@ -39,8 +40,11 @@ callback_url = config.get('Twitter', 'callback_url') webhook_url = config.get('Twitter', 'webhook_url') environment_name = config.get('Twitter', 'environment_name') +yt_webhook_url = config.get('YouTube', 'webhook_url') 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): path = pathlib.Path(storage_path, 'tokens.json') @@ -79,6 +83,16 @@ def setup_twitter_webhook(api): 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): queue = persistqueue.SQLiteQueue(storage_path) queue.put(data) @@ -145,8 +159,7 @@ def youtube_webhook_get(): mode = flask.request.args.get('hub.mode') topic = flask.request.args.get('hub.topic') 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 - mode not in ('subscribe', 'unsubscribe') or not challenge): + if topic != yt_websub_topic or mode not in ('subscribe', 'unsubscribe') or not challenge: return 'Invalid request', 403 return challenge, 200