Skip to content

Commit 27f1205

Browse files
Remove oauth2client usages.
Update search.py to remove usage of deprecated oauth2client library.
1 parent 8235ff8 commit 27f1205

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

‎python/search.py

+37-28
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
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 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
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

1624
def youtube_search(options):
1725
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
@@ -21,7 +29,7 @@ def youtube_search(options):
2129
# query term.
2230
search_response = youtube.search().list(
2331
q=options.q,
24-
part="id,snippet",
32+
part='id,snippet',
2533
maxResults=options.max_results
2634
).execute()
2735

@@ -31,28 +39,29 @@ def youtube_search(options):
3139

3240
# Add each result to the appropriate list, and then display the lists of
3341
# 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()
5463

5564
try:
5665
youtube_search(args)
5766
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

Comments
 (0)