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