[collegehumor] Add support for age_limit
[youtube-dl] / youtube_dl / extractor / collegehumor.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5
6
7 class CollegeHumorIE(InfoExtractor):
8     _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/(video|embed|e)/(?P<videoid>[0-9]+)/?(?P<shorttitle>.*)$'
9
10     _TESTS = [{
11         u'url': u'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
12         u'file': u'6902724.mp4',
13         u'md5': u'dcc0f5c1c8be98dc33889a191f4c26bd',
14         u'info_dict': {
15             u'title': u'Comic-Con Cosplay Catastrophe',
16             u'description': u'Fans get creative this year at San Diego.  Too',
17             u'age_limit': 13,
18         },
19     },
20     {
21         u'url': u'http://www.collegehumor.com/video/3505939/font-conference',
22         u'file': u'3505939.mp4',
23         u'md5': u'72fa701d8ef38664a4dbb9e2ab721816',
24         u'info_dict': {
25             u'title': u'Font Conference',
26             u'description': u'This video wasn\'t long enough, so we made it double-spaced.',
27             u'age_limit': 10,
28         },
29     }]
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('videoid')
34
35         jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
36         data = json.loads(self._download_webpage(
37             jsonUrl, video_id, u'Downloading info JSON'))
38         vdata = data['video']
39
40         AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
41         rating = vdata.get('rating')
42         if rating:
43             age_limit = AGE_LIMITS.get(rating.lower())
44         else:
45             age_limit = None  # None = No idea
46
47         PREFS = {'high_quality': 2, 'low_quality': 0}
48         formats = []
49         for format_key in ('mp4', 'webm'):
50             for qname, qurl in vdata[format_key].items():
51                 formats.append({
52                     'format_id': format_key + '_' + qname,
53                     'url': qurl,
54                     'format': format_key,
55                     'preference': PREFS.get(qname),
56                 })
57         self._sort_formats(formats)
58
59         return {
60             'id': video_id,
61             'title': vdata['title'],
62             'description': vdata.get('description'),
63             'thumbnail': vdata.get('thumbnail'),
64             'formats': formats,
65             'age_limit': age_limit,
66         }