-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_json_video.py
executable file
·76 lines (66 loc) · 3.34 KB
/
create_json_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
import settings
import json
import click
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
# https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = settings.GOOGLE_APIKEY
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def _extract_video_info(item):
"""Extract youtube video information from snippet dict"""
video_id = item['id']['videoId']
video_url = 'https://www.youtube.com/watch?v=' + video_id
oembed = '<iframe width="512" height="512" ' \
'src="https://www.youtube.com/embed/{}" ' \
'frameborder="0" allowfullscreen></iframe>'.format(video_id)
return {'video_url': video_url, 'oembed': oembed}
@click.command()
@click.option('--query', default='roman empire britain', help="Your YOUTUBE query")
@click.option('--duration', default='short', help="It could be short or long")
@click.option('--published', default='2016-01-01T00:00:00Z', help="It should be a date YYYY-MM-DD")
@click.option('--region', default='GB', help="Use a Region Code")
@click.option('--max', default='50', help="It should be less than 50")
@click.option('--output', default='videos.json', help="Name of the output video")
def youtube_search(query, duration, published, region, max, output):
"""Search in youtube and save the results in videos.json file."""
youtube = build(YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
search_response = youtube.search().list(q=query,
part="id,snippet",
maxResults=max,
type="video",
videoDuration=duration,
publishedAfter=published,
regionCode=region,
).execute()
videos = []
nextPageToken = search_response.get('nextPageToken', None)
for search_result in search_response.get("items", []):
tmp = search_result.copy()
tmp.update(_extract_video_info(search_result))
videos.append(tmp)
while len(search_response.get('items', [])) > 0 and nextPageToken != None:
search_response = youtube.search().list(q=query,
part="id,snippet",
maxResults=max,
type="video",
videoDuration=duration,
publishedAfter=published,
regionCode=region,
pageToken=nextPageToken
).execute()
nextPageToken = search_response.get('nextPageToken', None)
for search_result in search_response.get("items", []):
videos.append(search_result)
with open(output, 'w') as file:
file.write(json.dumps(videos))
if __name__ == "__main__":
youtube_search()