Created
October 30, 2023 18:58
-
-
Save hmelenok/7e7e5cebcdeb3d14c1409ed91eac89af to your computer and use it in GitHub Desktop.
Populate Youtube category by videos id's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
API_KEY = 'YOUR_YOUTUBE_API_KEY' | |
VIDEO_IDS = ['1Ds2G7lcrxA', 'AnotherVideoID', ...] # Replace with your list of video IDs | |
BASE_URL = "https://www.googleapis.com/youtube/v3/videos" | |
def get_video_details(video_id): | |
params = { | |
'part': 'snippet', | |
'id': video_id, | |
'key': API_KEY | |
} | |
response = requests.get(BASE_URL, params=params) | |
data = response.json() | |
if 'items' in data and len(data['items']) > 0: | |
snippet = data['items'][0]['snippet'] | |
category_id = snippet['categoryId'] | |
tags = snippet.get('tags', []) # Some videos might not have tags | |
return category_id, tags | |
else: | |
return None, [] | |
category_id_to_name = { | |
"1": "Film & Animation", | |
"2": "Autos & Vehicles", | |
"10": "Music", | |
# ... You can get the full list from YouTube Data API's videoCategories.list method | |
# Note: These category IDs might differ by region, so you may want to fetch it dynamically | |
} | |
for video_id in VIDEO_IDS: | |
category_id, tags = get_video_details(video_id) | |
category_name = category_id_to_name.get(category_id, "Unknown") | |
print(f"Video ID: {video_id}, Category: {category_name}, Tags: {tags}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment