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