Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search YouTube for results using a newline separated list of queries.
import json
import googleapiclient.discovery
query_fn = 'queries.txt'
api_key = '' # insert API key here
# https://developers.google.com/youtube/v3/docs/search/list
params = {
'videoDuration': 'short', # any long medium short
'videoDefinition': 'any', # high standard
'order': 'viewCount', # date rating relevance title viewCount
'publishedAfter': '2019-03-29T00:00:00Z'
}
youtube = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key)
def batch_list(batches=20, **kwargs):
items = None
next_page_token = None
for batch in range(batches):
request = youtube.search().list(
part='snippet',
pageToken=next_page_token,
maxResults=50,
type='video',
**kwargs)
response = request.execute()
if items is None:
items = response['items']
else:
items.extend(response['items'])
next_page_token = response['nextPageToken']
if len(response['items']) < 50:
break
return items
with open(query_fn) as f:
queries = f.read().splitlines()
collected = []
for query in queries:
collected.extend(batch_list(q=query,**params))
with open('output.json', 'w') as f:
json.dump(collected, f)
@elkhan

This comment has been minimized.

Copy link

@elkhan elkhan commented Dec 21, 2020

I assume that the line 31 should be indented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment