]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/collegehumor.py
[youtube:search_url] Fix extraction (Closes #6578)
[yt-dlp.git] / youtube_dl / extractor / collegehumor.py
CommitLineData
7c0578dc
PH
1from __future__ import unicode_literals
2
b4a9bf70 3import json
7beb36a5 4import re
7beb36a5
PH
5
6from .common import InfoExtractor
83cebd73 7from ..utils import int_or_none
7beb36a5
PH
8
9
10class CollegeHumorIE(InfoExtractor):
39b782b3 11 _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/(video|embed|e)/(?P<videoid>[0-9]+)/?(?P<shorttitle>.*)$'
7beb36a5 12
9e1a5b84
JW
13 _TESTS = [
14 {
15 'url': 'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
16 'md5': 'dcc0f5c1c8be98dc33889a191f4c26bd',
17 'info_dict': {
18 'id': '6902724',
19 'ext': 'mp4',
20 'title': 'Comic-Con Cosplay Catastrophe',
21 'description': "Fans get creative this year at San Diego. Too creative. And yes, that's really Joss Whedon.",
22 'age_limit': 13,
23 'duration': 187,
24 },
25 }, {
26 'url': 'http://www.collegehumor.com/video/3505939/font-conference',
27 'md5': '72fa701d8ef38664a4dbb9e2ab721816',
28 'info_dict': {
29 'id': '3505939',
30 'ext': 'mp4',
31 'title': 'Font Conference',
32 'description': "This video wasn't long enough, so we made it double-spaced.",
33 'age_limit': 10,
34 'duration': 179,
35 },
36 }, {
37 # embedded youtube video
38 'url': 'http://www.collegehumor.com/embed/6950306',
39 'info_dict': {
40 'id': 'Z-bao9fg6Yc',
41 'ext': 'mp4',
42 'title': 'Young Americans Think President John F. Kennedy Died THIS MORNING IN A CAR ACCIDENT!!!',
43 'uploader': 'Mark Dice',
44 'uploader_id': 'MarkDice',
45 'description': 'md5:62c3dab9351fac7bb44b53b69511d87f',
46 'upload_date': '20140127',
47 },
48 'params': {
49 'skip_download': True,
50 },
51 'add_ie': ['Youtube'],
de48adda 52 },
fcdea266 53 ]
7beb36a5
PH
54
55 def _real_extract(self, url):
56 mobj = re.match(self._VALID_URL, url)
7beb36a5
PH
57 video_id = mobj.group('videoid')
58
b4a9bf70
PH
59 jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
60 data = json.loads(self._download_webpage(
7c0578dc 61 jsonUrl, video_id, 'Downloading info JSON'))
b4a9bf70 62 vdata = data['video']
fcdea266
JMF
63 if vdata.get('youtubeId') is not None:
64 return {
65 '_type': 'url',
66 'url': vdata['youtubeId'],
67 'ie_key': 'Youtube',
68 }
b4a9bf70 69
55033ffb
PH
70 AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
71 rating = vdata.get('rating')
72 if rating:
73 age_limit = AGE_LIMITS.get(rating.lower())
74 else:
75 age_limit = None # None = No idea
76
b4a9bf70
PH
77 PREFS = {'high_quality': 2, 'low_quality': 0}
78 formats = []
79 for format_key in ('mp4', 'webm'):
c4db377c 80 for qname, qurl in vdata.get(format_key, {}).items():
b4a9bf70
PH
81 formats.append({
82 'format_id': format_key + '_' + qname,
83 'url': qurl,
84 'format': format_key,
85 'preference': PREFS.get(qname),
86 })
b4a9bf70
PH
87 self._sort_formats(formats)
88
83cebd73 89 duration = int_or_none(vdata.get('duration'), 1000)
355271fb 90 like_count = int_or_none(vdata.get('likes'))
83cebd73 91
b4a9bf70 92 return {
7beb36a5 93 'id': video_id,
b4a9bf70
PH
94 'title': vdata['title'],
95 'description': vdata.get('description'),
96 'thumbnail': vdata.get('thumbnail'),
97 'formats': formats,
55033ffb 98 'age_limit': age_limit,
83cebd73 99 'duration': duration,
355271fb 100 'like_count': like_count,
7beb36a5 101 }