Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created January 18, 2020 02:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemcdonald/ec9d398bd4506d859469db55acb57bfb to your computer and use it in GitHub Desktop.
Save kylemcdonald/ec9d398bd4506d859469db55acb57bfb to your computer and use it in GitHub Desktop.
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
Copy link

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