11#!/usr/bin/python
22
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
614
715
816# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
917# tab of
1018# https://cloud.google.com/console
1119# 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'
1523
1624def youtube_search (options ):
1725 youtube = build (YOUTUBE_API_SERVICE_NAME , YOUTUBE_API_VERSION ,
@@ -21,19 +29,19 @@ def youtube_search(options):
2129 # query term.
2230 search_response = youtube .search ().list (
2331 q = options .q ,
24- type = " video" ,
32+ type = ' video' ,
2533 location = options .location ,
2634 locationRadius = options .location_radius ,
27- part = " id,snippet" ,
35+ part = ' id,snippet' ,
2836 maxResults = options .max_results
2937 ).execute ()
3038
3139 search_videos = []
3240
3341 # 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 )
3745
3846 # Call the videos.list method to retrieve location details for each video.
3947 video_response = youtube .videos ().list (
@@ -44,23 +52,23 @@ def youtube_search(options):
4452 videos = []
4553
4654 # 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' ]))
5159
52- print " Videos:\n " , " \n " .join (videos ), " \n "
60+ print ' Videos:\n ' , ' \n ' .join (videos ), ' \n '
5361
5462
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 ()
6170
6271 try :
6372 youtube_search (args )
6473 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