I want to do a fun project and calculate when a YouTuber is most likely to upload their next YouTube video based on previous releases and their release dates.
So I calculate how long the YouTuber needs on average between each upload, and then calculated when he will upload their next YouTube video.
But first I need all Date / Times from for example the last 50 videos of that specific YouTuber. Currently I'm trying to do that with Python and the Google Cloud "YouTube Data API v3".
I found a few lines of code online:
import os
import googleapiclient.discovery
from googleapiclient.errors import HttpError
from google.oauth2.credentials import Credentials
# Replace 'YOUR_API_KEY' with your API key or provide the path to your credentials file
api_key = 'YOUR_API_KEY'
# Create the API client
youtube = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key)
def get_channel_subscriber_count(channel_id):
try:
response = youtube.channels().list(part='statistics', id=channel_id).execute()
channel = response['items'][0]
subscriber_count = channel['statistics']['subscriberCount']
print(f"Subscriber Count: {subscriber_count}")
except HttpError as e:
print(f"An error occurred: {e}")
# Replace 'CHANNEL_ID' with the ID of the specific YouTube channel
channel_id = 'CHANNEL_ID'
get_channel_subscriber_count(channel_id)
I replaced the API Key with mine, and changed the channel_id to the name of the YouTuber (youtube.com/@RemusNeo) so I replace it with "RemusNeo". However I always get the following error message:
Traceback (most recent call last): File "C:\Users\Baseult\PycharmProjects\pythonProject\main.py", line 23, in get_channel_subscriber_count(channel_id) File "C:\Users\Baseult\PycharmProjects\pythonProject\main.py", line 15, in get_channel_subscriber_count channel = response['items'][0] KeyError: 'items'
If I use the same code and do it manually with each video using the following code, then it works fine:
def get_video_details(video_id):
try:
response = youtube.videos().list(part='snippet', id=video_id).execute()
video = response['items'][0]
title = video['snippet']['title']
release_date = video['snippet']['publishedAt']
print(f"Title: {title}")
print(f"Release Date: {release_date}")
except HttpError as e:
print(f"An error occurred: {e}")
# Replace 'VIDEO_ID' with the ID of the specific YouTube video
video_id = 'VIDEO_ID'
get_video_details(video_id)
I tried a few other methods, for example get the Subscriber Count of that YouTuber but that also didn't work. So I guess the code is outdated to access a YouTubers channel?
Would be awesome if someone could help me, thanks a lot :)