Created
July 6, 2021 01:50
-
-
Save sbassi/a18ab8d0caaeaa09bc4f3975a9bd569a to your computer and use it in GitHub Desktop.
This file contains 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 | |
import time | |
APIKEY = 'INSERT YOUR KEY HERE' | |
# NExt channelID is for https://www.youtube.com/c/A24com | |
CHANNELID = 'UCR9120YBAqMfntqgRTKmkjQ' | |
URL = f'https://www.googleapis.com/youtube/v3/search?key={APIKEY}&channelId={CHANNELID}&part=snippet,id&order=date&maxResults=50' | |
def get_titles_id(response): | |
"""" | |
""" | |
data = [] | |
rspjson = json.loads(response) | |
for item in rspjson['items']: | |
vid = item['id']['videoId'] | |
title = item['snippet']['title'] | |
data.append((vid, title)) | |
return data | |
def get_next_page_token(response): | |
rspjson = json.loads(response) | |
npt = rspjson['nextPageToken'] | |
return npt | |
# First request | |
response = requests.get(URL) | |
npt = get_next_page_token(response.content) | |
data = get_titles_id(response.content) | |
while True: | |
URL = f'https://www.googleapis.com/youtube/v3/search?key={APIKEY}&channelId={CHANNELID}&part=snippet,id&order=date&maxResults=50&pageToken={npt}' | |
response = requests.get(URL) | |
data += get_titles_id(response.content) | |
try: | |
npt = get_next_page_token(response.content) | |
except KeyError: | |
break | |
time.sleep(1) | |
for d in data: | |
print(d[0] + '\t' + d[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment