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