Utilize Discord's webhooks to receive new video notifications via YouTube's PubSubHubbub push notifications.
YouTube offers push notifications in the form of a PubSubHubbub server-to-server interface, though the documentation is fairly lacking. This small Flask server implements the required callback URL on the /feed
route and parses out the URL for the video in the notification. An unfortunate limitation is that a push notification is sent out for both a new video upload and an edit to the description/title of an existing video. The XML content seems to be the same for each type of notification, so there's no easy way to differentiate.
The other half of the equation is Discord's webhooks. Once this server receives a push notification from YouTube and parses out the video URL, a message containing that URL is sent to a webhook to be posted in the associated Discord text channel.
Clone the repo, make a virtual environment, and install the dependencies. Ex:
git clone https://github.com/BryanCuneo/yt-to-discord.git
cd yt-to-discord
python3 -m venv venv
pip install -r .\requirements.txt
webhook_url
: Once you create a webhook (see the 'Making a Webhook' section here), you'll see a button labeled 'Copy Webhook URL.'
Click that and paste the resulting link here in config.yaml
.
channel_ids[]
: A comma sparated list of channel IDs from which your server should accept notifications. If left as an empty list, []
, it will accept notifications from any channel.
message_prefix
: A string to be included in the message with the video URL.
For easy testing purposes, you can run the Flask dev server on your local machine then expose it to the web with something like ngrok. Ex:
flask run --host=0.0.0.0
Then in another terminal:
ngrok http 5000
You'll get something like the following. Copy the first forwarding URL for step four.
Navigate to the Google PubSubHubbub subscription page:
In the 'Subscribe/Unsibscribe' mode, fill out the first four boxes:
- Callback URL - the URL you copied in step 3, followed by /feed. Ex:
http://1829c24236ed.ngrok.io/feed
- Topic URL -
https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
where CHANNEL_ID is the YouTube channel ID for the channel you'd like to subscribe to. - Verify type -
Asynchronous
- Mode -
Subscribe
Press the 'Do It!' button. Within a few minutes, you should see a GET request to /feed on your ngrok interface and a response code of 200.
From here, you'll have to wait until there is activity on the channel you subscribed to. For testing purposes you can simply subscribe to your own personal YouTube channel and upload a short video. You can then edit the description repeatedly every time you wish to generate a new notification. Each notification should show up as a POST to /feed with a response code of 204.
Once you're satisfied with the functionality you'll want a proper hosting solution rather than Flask's dev server. There are infinite possibilities here including some free options.