categories.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. try:
  2. #import urllib2
  3. from urllib2 import urlopen
  4. import urllib
  5. except ImportError:
  6. from urllib.request import urlopen
  7. import json
  8. URL = "https://www.googleapis.com/youtube/v3/videoCategories"
  9. IDS = {
  10. "Film & Animation": 1,
  11. "Autos & Vehicles": 2,
  12. "Music": 10,
  13. "Pets & Animals": 15,
  14. "Sports": 17,
  15. "Short Movies": 18,
  16. "Travel & Events": 19,
  17. "Gaming": 20,
  18. "Videoblogging": 21,
  19. "People & Blogs": 22,
  20. "Comedy": 23,
  21. "Entertainment": 24,
  22. "News & Politics": 25,
  23. "Howto & Style": 26,
  24. "Education": 27,
  25. "Science & Technology": 28,
  26. "Nonprofits & Activism": 29,
  27. "Movies": 30,
  28. "Anime/Animation": 31,
  29. "Action/Adventure": 32,
  30. "Classics": 33,
  31. "Documentary": 35,
  32. "Drama": 36,
  33. "Family": 37,
  34. "Foreign": 38,
  35. "Horror": 39,
  36. "Sci-Fi/Fantasy": 40,
  37. "Thriller": 41,
  38. "Shorts": 42,
  39. "Shows": 43,
  40. "Trailers": 44,
  41. }
  42. def get(region_code="us", api_key=None):
  43. params = dict(part="snippet", regionCode=region_code, key=api_key)
  44. full_url = URL + "?" + urllib.urlencode(params)
  45. response = urlopen(full_url)
  46. categories_info = json.loads(response.read())
  47. items = categories_info["items"]
  48. return dict((item["snippet"]["title"], item["id"]) for item in items)