How to get notification when youtube channel uploads video in python
Question:
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.
Answers:
So simply,
-
Get it’s URL and scrape it with the Python Library Beautiful Soup. https://pypi.org/project/beautifulsoup4/.
-
List item
-
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).
-
At whatever time interval you like, have the python scraper be called again to do a comparison
-
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/
-
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.
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
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.
So simply,
-
Get it’s URL and scrape it with the Python Library Beautiful Soup. https://pypi.org/project/beautifulsoup4/.
-
List item
-
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).
-
At whatever time interval you like, have the python scraper be called again to do a comparison
-
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/
-
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.
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