Skip to content

Instantly share code, notes, and snippets.

@baojie
Created May 4, 2014 19:35
Show Gist options
  • Select an option

  • Save baojie/3d676de58d50cda9f987 to your computer and use it in GitHub Desktop.

Select an option

Save baojie/3d676de58d50cda9f987 to your computer and use it in GitHub Desktop.

Revisions

  1. baojie created this gist May 4, 2014.
    30 changes: 30 additions & 0 deletions youtube_search_simple.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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)