Skip to content

Instantly share code, notes, and snippets.

@why-not
Created March 10, 2023 02:35
Show Gist options
  • Save why-not/a2d4e13633f24cb4e46d45054e12a796 to your computer and use it in GitHub Desktop.
Save why-not/a2d4e13633f24cb4e46d45054e12a796 to your computer and use it in GitHub Desktop.
Code to get basic stats from videos in a given list of playlists (youtube)
import time
import pandas as pd
from googleapiclient.discovery import build
# Enter your API key here
api_key = "API KEY HERE"
# Create a service object for the YouTube API
youtube = build('youtube', 'v3', developerKey=api_key)
# Function to get video details
def get_video_details(video_id):
request = youtube.videos().list(
part="snippet,statistics",
id=video_id
)
response = request.execute()
title = response['items'][0]['snippet']['title']
timestamp = response['items'][0]['snippet']['publishedAt']
views = response['items'][0]['statistics']['viewCount']
likes = response['items'][0]['statistics']['likeCount']
return title, timestamp, views, likes
# Function to get playlist items
def get_playlist_items(playlist_id):
request = youtube.playlistItems().list(
part="snippet",
playlistId=playlist_id,
maxResults=50
)
response = request.execute()
video_ids = []
for item in response['items']:
video_ids.append(item['snippet']['resourceId']['videoId'])
return video_ids
# Function to get playlist details
def get_playlist_details(playlist_url):
playlist_id = playlist_url.split("=")[1]
request = youtube.playlists().list(
part="snippet",
id=playlist_id
)
response = request.execute()
playlist_name = response['items'][0]['snippet']['title']
video_ids = get_playlist_items(playlist_id)
video_details = []
for video_id in video_ids:
details = list(get_video_details(video_id))
details.append(playlist_name)
video_details.append(details)
print(f"Title: {details[0]}, Timestamp: {details[1]}, Views: {details[2]}, Likes: {details[3]}, Playlist Name: {details[4]}")
time.sleep(0.5) # Add a sleep of 500ms
return video_details
# Example list of YouTube playlist URLs
playlist_urls = ["https://www.youtube.com/playlist?list=PLeskMkEaHJYd8OV2ISHa8qb0MvPzoXkI8",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYdgg5L_oihGGnh9q_9Gpvyo",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYdkdh1ABRzMT2jCo658agFi",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYcHbEhykFIyapEPzjBAw9Sv",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYf7ug1XGeJvL03v-clIQCLw",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYe2pe0wUMFnLFPJtdb7h4Wl",
"https://www.youtube.com/playlist?list=PLeskMkEaHJYeW-iO8GKT3HqvLSij3XLR7"]
# Create an empty DataFrame
df = pd.DataFrame(columns=["Title", "Timestamp", "Views", "Likes", "Playlist Name" ])
# Loop through each playlist URL
for playlist_url in playlist_urls:
# Get playlist details
video_details = get_playlist_details(playlist_url)
# Append video details to DataFrame
df = df.append(pd.DataFrame(video_details, columns=["Title", "Timestamp", "Views", "Likes", "Playlist Name", ]), ignore_index=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment