1
1
#!/usr/bin/python
2
2
3
- from apiclient .discovery import build
4
- from apiclient .errors import HttpError
5
- from oauth2client .tools import argparser
3
+ # This sample executes a search request for the specified search term.
4
+ # Sample usage:
5
+ # python geolocation_search.py --q=surfing --location-"37.42307,-122.08427" --location-radius=50km --max-results=10
6
+ # NOTE: To use the sample, you must provide a developer key obtained
7
+ # in the Google APIs Console. Search for "REPLACE_ME" in this code
8
+ # to find the correct place to provide that key..
9
+
10
+ import argparse
11
+
12
+ from googleapiclient .discovery import build
13
+ from googleapiclient .errors import HttpError
6
14
7
15
8
16
# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
9
17
# tab of
10
18
# https://cloud.google.com/console
11
19
# Please ensure that you have enabled the YouTube Data API for your project.
12
- DEVELOPER_KEY = " REPLACE_ME"
13
- YOUTUBE_API_SERVICE_NAME = " youtube"
14
- YOUTUBE_API_VERSION = "v3"
20
+ DEVELOPER_KEY = ' REPLACE_ME'
21
+ YOUTUBE_API_SERVICE_NAME = ' youtube'
22
+ YOUTUBE_API_VERSION = 'v3'
15
23
16
24
def youtube_search (options ):
17
25
youtube = build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
@@ -21,19 +29,19 @@ def youtube_search(options):
21
29
# query term.
22
30
search_response = youtube .search ().list (
23
31
q = options .q ,
24
- type = " video" ,
32
+ type = ' video' ,
25
33
location = options .location ,
26
34
locationRadius = options .location_radius ,
27
- part = " id,snippet" ,
35
+ part = ' id,snippet' ,
28
36
maxResults = options .max_results
29
37
).execute ()
30
38
31
39
search_videos = []
32
40
33
41
# Merge video ids
34
- for search_result in search_response .get (" items" , []):
35
- search_videos .append (search_result ["id" ][ " videoId" ])
36
- video_ids = "," .join (search_videos )
42
+ for search_result in search_response .get (' items' , []):
43
+ search_videos .append (search_result ['id' ][ ' videoId' ])
44
+ video_ids = ',' .join (search_videos )
37
45
38
46
# Call the videos.list method to retrieve location details for each video.
39
47
video_response = youtube .videos ().list (
@@ -44,23 +52,23 @@ def youtube_search(options):
44
52
videos = []
45
53
46
54
# Add each result to the list, and then display the list of matching videos.
47
- for video_result in video_response .get (" items" , []):
48
- videos .append (" %s, (%s,%s)" % (video_result [" snippet" ][ " title" ],
49
- video_result [" recordingDetails" ][ " location" ][ " latitude" ],
50
- video_result [" recordingDetails" ][ " location" ][ " longitude" ]))
55
+ for video_result in video_response .get (' items' , []):
56
+ videos .append (' %s, (%s,%s)' % (video_result [' snippet' ][ ' title' ],
57
+ video_result [' recordingDetails' ][ ' location' ][ ' latitude' ],
58
+ video_result [' recordingDetails' ][ ' location' ][ ' longitude' ]))
51
59
52
- print " Videos:\n " , " \n " .join (videos ), " \n "
60
+ print ' Videos:\n ' , ' \n ' .join (videos ), ' \n '
53
61
54
62
55
- if __name__ == "__main__" :
56
- argparser .add_argument ("--q" , help = "Search term" , default = "Google" )
57
- argparser .add_argument ("--location" , help = "Location" , default = "37.42307,-122.08427" )
58
- argparser .add_argument ("--location-radius" , help = "Location radius" , default = "5km" )
59
- argparser .add_argument ("--max-results" , help = "Max results" , default = 25 )
60
- args = argparser .parse_args ()
63
+ if __name__ == '__main__' :
64
+ parser = argparse .ArgumentParser ()
65
+ parser .add_argument ('--q' , help = 'Search term' , default = 'Google' )
66
+ parser .add_argument ('--location' , help = 'Location' , default = '37.42307,-122.08427' )
67
+ parser .add_argument ('--location-radius' , help = 'Location radius' , default = '5km' )
68
+ parser .add_argument ('--max-results' , help = 'Max results' , default = 25 )
69
+ args = parser .parse_args ()
61
70
62
71
try :
63
72
youtube_search (args )
64
73
except HttpError , e :
65
- print "An HTTP error %d occurred:\n %s" % (e .resp .status , e .content )
66
-
74
+ print 'An HTTP error %d occurred:\n %s' % (e .resp .status , e .content )
0 commit comments