Skip to content

Instantly share code, notes, and snippets.

@baojie
Created May 4, 2014 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baojie/3d676de58d50cda9f987 to your computer and use it in GitHub Desktop.
Save baojie/3d676de58d50cda9f987 to your computer and use it in GitHub Desktop.
Simplified youtube search example
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