Created
January 18, 2020 02:13
-
-
Save kylemcdonald/ec9d398bd4506d859469db55acb57bfb to your computer and use it in GitHub Desktop.
Search YouTube for results using a newline separated list of queries.
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I assume that the line 31 should be indented