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 search.py --key=surfing --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,7 +29,7 @@ def youtube_search(options):
21
29
# query term.
22
30
search_response = youtube .search ().list (
23
31
q = options .q ,
24
- part = " id,snippet" ,
32
+ part = ' id,snippet' ,
25
33
maxResults = options .max_results
26
34
).execute ()
27
35
@@ -31,28 +39,29 @@ def youtube_search(options):
31
39
32
40
# Add each result to the appropriate list, and then display the lists of
33
41
# matching videos, channels, and playlists.
34
- for search_result in search_response .get ("items" , []):
35
- if search_result ["id" ]["kind" ] == "youtube#video" :
36
- videos .append ("%s (%s)" % (search_result ["snippet" ]["title" ],
37
- search_result ["id" ]["videoId" ]))
38
- elif search_result ["id" ]["kind" ] == "youtube#channel" :
39
- channels .append ("%s (%s)" % (search_result ["snippet" ]["title" ],
40
- search_result ["id" ]["channelId" ]))
41
- elif search_result ["id" ]["kind" ] == "youtube#playlist" :
42
- playlists .append ("%s (%s)" % (search_result ["snippet" ]["title" ],
43
- search_result ["id" ]["playlistId" ]))
44
-
45
- print "Videos:\n " , "\n " .join (videos ), "\n "
46
- print "Channels:\n " , "\n " .join (channels ), "\n "
47
- print "Playlists:\n " , "\n " .join (playlists ), "\n "
48
-
49
-
50
- if __name__ == "__main__" :
51
- argparser .add_argument ("--q" , help = "Search term" , default = "Google" )
52
- argparser .add_argument ("--max-results" , help = "Max results" , default = 25 )
53
- args = argparser .parse_args ()
42
+ for search_result in search_response .get ('items' , []):
43
+ if search_result ['id' ]['kind' ] == 'youtube#video' :
44
+ videos .append ('%s (%s)' % (search_result ['snippet' ]['title' ],
45
+ search_result ['id' ]['videoId' ]))
46
+ elif search_result ['id' ]['kind' ] == 'youtube#channel' :
47
+ channels .append ('%s (%s)' % (search_result ['snippet' ]['title' ],
48
+ search_result ['id' ]['channelId' ]))
49
+ elif search_result ['id' ]['kind' ] == 'youtube#playlist' :
50
+ playlists .append ('%s (%s)' % (search_result ['snippet' ]['title' ],
51
+ search_result ['id' ]['playlistId' ]))
52
+
53
+ print 'Videos:\n ' , '\n ' .join (videos ), '\n '
54
+ print 'Channels:\n ' , '\n ' .join (channels ), '\n '
55
+ print 'Playlists:\n ' , '\n ' .join (playlists ), '\n '
56
+
57
+
58
+ if __name__ == '__main__' :
59
+ parser = argparse .ArgumentParser ()
60
+ parser .add_argument ('--q' , help = 'Search term' , default = 'Google' )
61
+ parser .add_argument ('--max-results' , help = 'Max results' , default = 25 )
62
+ args = parser .parse_args ()
54
63
55
64
try :
56
65
youtube_search (args )
57
66
except HttpError , e :
58
- print " An HTTP error %d occurred:\n %s" % (e .resp .status , e .content )
67
+ print ' An HTTP error %d occurred:\n %s' % (e .resp .status , e .content )
0 commit comments