Skip to content

Instantly share code, notes, and snippets.

@Nachtalb
Last active November 1, 2021 19:51
Show Gist options
  • Save Nachtalb/270b83f04f06b377e0781e199756ad39 to your computer and use it in GitHub Desktop.
Save Nachtalb/270b83f04f06b377e0781e199756ad39 to your computer and use it in GitHub Desktop.
Get all videos of a youtube channel and print url, title and publish date as CSV
# Ya need to install "yarl" and "requests" cuz I was too lazy to use the standard libs
from yarl import URL
from requests import Session
key = "YOUTUBE_API_KEY"
channel_id = "CHANNEL_ID"
url = f"https://www.googleapis.com/youtube/v3/search"
query = {
'key': key,
'channelId': channel_id,
'part': 'snippet,id',
'order': 'date',
'maxResults': 50,
'type': 'video',
}
ses = Session()
resp_data = {}
print('URL, Title, DateTime')
video_url = 'https://youtu.be/'
while not resp_data or 'nextPageToken' in resp_data:
if 'nextPageToken' in resp_data:
query['pageToken'] = resp_data['nextPageToken']
good_url = URL(url).with_query(**query)
resp = ses.get(good_url)
resp_data = resp.json()
for item in resp_data['items']:
title = item['snippet']['title'].replace('"', '\\"')
print(', '.join([video_url + item['id']['videoId'], f'"{title}"', item['snippet']['publishTime']]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment