How to get notification when youtube channel uploads video in python
Asked Answered
M

3

5

I am trying to create a script that'll notify me whenever a youtube channel uploads a video. Just like the Subscribe button

What I have tried:
I have tried looking for a documentation regarding this, and also learnt how to setup a YouTube V3 API in Google Cloud in the process. The closest libraries i have found were python-youtube and youtube-channel-subscribe.

My Progress so far:
I have tried youtube-channel-subscribe but the problem with this is that it opens up browser and then subscribes, and this won't work since I am going to be running this on servers. I had gone through the python-youtube documentation and i don't think it has functionality for this(I might be wrong)

Any help would be appreciated.

Misrepresent answered 23/6, 2021 at 13:53 Comment(0)
V
10

This can be done by simply using 2 built-in python libraries: requests and re. Basically, we want to monitor the newest video of the YouTube channel.

We can get the title, upload date and view count of the newest video of any YouTube channel with these few lines of code:

import requests
import re

channel = "https://www.youtube.com/user/PewDiePie"

html = requests.get(channel + "/videos").text
info = re.search('(?<={"label":").*?(?="})', html).group()
date = re.search('\d+ \w+ ago.*seconds ', info).group()

print(info)
print(date)

Output:

Reacting To Strangers Secrets by PewDiePie 4 hours ago 11 minutes, 54 seconds 584,062 views
4 hours ago 11 minutes, 54 seconds 

You can store the video's info (with the relative date converted to the absolute date) in a database, and whenever info bears a title not present in the database, or the date bears a time that when subtract form the current date, is later than the last date in the database, then a new video has been uploaded.


UPDATE

The date format for YouTube's videos changed, so the regex for the date above wouldn't work anymore. However, the info would still contain it.

Here is the version that also gets the url of the latest video:

import requests
import re

channel = "https://www.youtube.com/user/PewDiePie"

html = requests.get(channel + "/videos").text
info = re.search('(?<={"label":").*?(?="})', html).group()
url = "https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', html).group()

print(info)
print(url)

Output:

Lot of big changes lately.. by PewDiePie 6 hours ago 22 minutes 723,960 views
https://www.youtube.com/watch?v=psHriqExm6U
Vallery answered 29/6, 2021 at 20:44 Comment(3)
hey bro, just wanted to know how to fetch url of the video. Anyways thanks for the helpMisrepresent
@Misrepresent Glad to help. You can use url = "https://www.youtube.com/watch?v=" + re.search('(?<="videoId":").*?(?=")', html).group() to get the url of the newest video.Vallery
Thanks alot! i think ur answer deserves an upvote for such a quick response. Regardless, Thanks it'll help alotMisrepresent
I
3

So simply,

  1. Find the page you want to monitor. enter image description here
  1. Get it's URL and scrape it with the Python Library Beautiful Soup. https://pypi.org/project/beautifulsoup4/.

  2. List item

  3. Create a Json file for that first version of the file (you could make this really simple, just take the name of the most recent video).

  4. At whatever time interval you like, have the python scraper be called again to do a comparison

  5. When the JSON file is different, (if name of most recent is not same as original, then do x) make a API call to Zapier https://zapier.com/

  6. Have Zapier SMS you on change via the api call.

The code above could be found directly in documentation. If you would like a more complete method of scraping, that would get around any sort of blocking from the client website, i'd use proxy chaining just fyi.

Illbehaved answered 29/6, 2021 at 14:24 Comment(0)
W
2

An efficient way to implement the feature is to use the PubSubHubbub that YouTube Data API v3 provides.

Instead of using their REST API to periodically fetch data to check if there is a new video from a channel, YouTube sends a push notification when certain channels upload or update a video.

One advantage of using this protocol is that you don't need an API key from Google Cloud (which has quota limits for using YouTube Data API)

It's a shame that there isn't a package that utilizes this protocol. Since I also needed the same feature for my own project, I created a new python package for my own and other people who might also need it.

pip install ytnoti

Here is a basic example of how to use the package:

from pyngrok import ngrok
from ytnoti import YouTubeNotifier, Video

# Create your ngrok token from https://dashboard.ngrok.com/get-started/setup
# This is necessary because the webhook URL must be accessible from the internet.
ngrok.set_auth_token("Your ngrok token here")

notifier = YouTubeNotifier()


@notifier.upload()
async def listener(video: Video):
    print(f"New video from {video.channel.name}: {video.title}")


notifier.subscribe("UCupvZG-5ko_eiXAupbDfxWw")  # YouTube Channel ID(s)
notifier.run()

If you have a dedicated IP address or domain and you don't want to use ngrok:

from ytnoti import YouTubeNotifier, Video

notifier = YouTubeNotifier(callback_url="https://yourdomain.com")


@notifier.upload()
async def listener(video: Video):
    print(f"New video from {video.channel.name}: {video.title}")


notifier.subscribe("UCupvZG-5ko_eiXAupbDfxWw")  # YouTube Channel ID(s)
notifier.run()
Whitleywhitlock answered 27/6 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.