Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. from apiclient.discovery import build
  5. from apiclient.errors import HttpError
  6. from oauth2client.tools import argparser
  7.  
  8. DEVELOPER_KEY = "mykey"
  9. YOUTUBE_API_SERVICE_NAME = "youtube"
  10. YOUTUBE_API_VERSION = "v3"
  11.  
  12.  
  13. def youtube_search(options):
  14. youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
  15. developerKey=DEVELOPER_KEY)
  16.  
  17. search_response = youtube.search().list(
  18. q=options.q,
  19. part="id,snippet",
  20. maxResults=options.max_results
  21. ).execute()
  22.  
  23. videos = []
  24. channels = []
  25. playlists = []
  26.  
  27. for search_result in search_response.get("items", []):
  28. if search_result["id"]["kind"] == "youtube#video":
  29. videos.append("%s (%s)" % (search_result["snippet"]["title"],
  30. search_result["id"]["videoId"]))
  31. elif search_result["id"]["kind"] == "youtube#channel":
  32. channels.append("%s (%s)" % (search_result["snippet"]["title"],
  33. search_result["id"]["channelId"]))
  34. elif search_result["id"]["kind"] == "youtube#playlist":
  35. playlists.append("%s (%s)" % (search_result["snippet"]["title"],
  36. search_result["id"]["playlistId"]))
  37.  
  38. print "Videos:n", "n".join(videos), "n"
  39. print "Channels:n", "n".join(channels), "n"
  40. print "Playlists:n", "n".join(playlists), "n"
  41.  
  42.  
  43. if __name__ == "__main__":
  44. argparser.add_argument("--q", help="Search term", default="Google")
  45. argparser.add_argument("--max-results", help="Max results", default=25)
  46. args = argparser.parse_args()
  47.  
  48. try:
  49. youtube_search(args)
  50. except HttpError, e:
  51. print "An HTTP error %d occurred:n%s" % (e.resp.status, e.content)
  52. search.py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement