Search code examples
pythonpython-3.xyoutubeyoutube-api

How to get notification when youtube channel uploads video in python


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.


Solution

  • 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