categories.py 1.2 KB

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