summaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/collegehumor.py
blob: 026d023a0801190b05b721edabfd426ab2f6cafa (plain)
    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     # embedded youtube video
   33     {
   34         'url': 'http://www.collegehumor.com/embed/6950457',
   35         'info_dict': {
   36             'id': 'W5gMp3ZjYg4',
   37             'ext': 'mp4',
   38             'title': 'Funny Dogs Protecting Babies Compilation 2014 [NEW HD]',
   39             'uploader': 'Funnyplox TV',
   40             'uploader_id': 'funnyploxtv',
   41             'description': 'md5:b20fc87608e2837596bbc8df85a3c34d',
   42             'upload_date': '20140128',
   43         },
   44         'params': {
   45             'skip_download': True,
   46         },
   47         'add_ie': ['Youtube'],
   48     },
   49     ]
   50 
   51     def _real_extract(self, url):
   52         mobj = re.match(self._VALID_URL, url)
   53         video_id = mobj.group('videoid')
   54 
   55         jsonUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id + '.json'
   56         data = json.loads(self._download_webpage(
   57             jsonUrl, video_id, 'Downloading info JSON'))
   58         vdata = data['video']
   59         if vdata.get('youtubeId') is not None:
   60             return {
   61                 '_type': 'url',
   62                 'url': vdata['youtubeId'],
   63                 'ie_key': 'Youtube',
   64             }
   65 
   66         AGE_LIMITS = {'nc17': 18, 'r': 18, 'pg13': 13, 'pg': 10, 'g': 0}
   67         rating = vdata.get('rating')
   68         if rating:
   69             age_limit = AGE_LIMITS.get(rating.lower())
   70         else:
   71             age_limit = None  # None = No idea
   72 
   73         PREFS = {'high_quality': 2, 'low_quality': 0}
   74         formats = []
   75         for format_key in ('mp4', 'webm'):
   76             for qname, qurl in vdata.get(format_key, {}).items():
   77                 formats.append({
   78                     'format_id': format_key + '_' + qname,
   79                     'url': qurl,
   80                     'format': format_key,
   81                     'preference': PREFS.get(qname),
   82                 })
   83         self._sort_formats(formats)
   84 
   85         return {
   86             'id': video_id,
   87             'title': vdata['title'],
   88             'description': vdata.get('description'),
   89             'thumbnail': vdata.get('thumbnail'),
   90             'formats': formats,
   91             'age_limit': age_limit,
   92         }

Generated by cgit