Created
May 4, 2014 19:35
-
-
Save baojie/3d676de58d50cda9f987 to your computer and use it in GitHub Desktop.
Simplified youtube search example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apiclient.discovery import build | |
from apiclient.errors import HttpError | |
from oauth2client.tools import argparser | |
# Get API key https://cloud.google.com/console | |
DEVELOPER_KEY = "REPLACE ME" | |
YOUTUBE_API_SERVICE_NAME = "youtube" | |
YOUTUBE_API_VERSION = "v3" | |
def youtube_search(search_term="Google", max_results=25): | |
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, | |
developerKey=DEVELOPER_KEY) | |
# Call the search.list method to retrieve results with query term. | |
search_response = youtube.search().list( | |
q=search_term, | |
part="id,snippet", | |
maxResults=max_results | |
).execute() | |
for item in search_response.get("items", []): | |
if item["id"]["kind"] == "youtube#video": | |
print u' '.join([item["id"]["videoId"], item["snippet"]["title"]]).encode('utf-8') | |
if __name__ == "__main__": | |
try: | |
youtube_search() | |
except HttpError, e: | |
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment