Merge remote-tracking branch 'rzhxeo/crunchyroll'
[youtube-dl] / youtube_dl / extractor / collegehumor.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     compat_urllib_parse_urlparse,
6     determine_ext,
7
8     ExtractorError,
9 )
10
11
12 class CollegeHumorIE(InfoExtractor):
13     _VALID_URL = r'^(?:https?://)?(?:www\.)?collegehumor\.com/(video|embed|e)/(?P<videoid>[0-9]+)/?(?P<shorttitle>.*)$'
14
15     _TESTS = [{
16         u'url': u'http://www.collegehumor.com/video/6902724/comic-con-cosplay-catastrophe',
17         u'file': u'6902724.mp4',
18         u'md5': u'1264c12ad95dca142a9f0bf7968105a0',
19         u'info_dict': {
20             u'title': u'Comic-Con Cosplay Catastrophe',
21             u'description': u'Fans get creative this year at San Diego.  Too creative.  And yes, that\'s really Joss Whedon.',
22         },
23     },
24     {
25         u'url': u'http://www.collegehumor.com/video/3505939/font-conference',
26         u'file': u'3505939.mp4',
27         u'md5': u'c51ca16b82bb456a4397987791a835f5',
28         u'info_dict': {
29             u'title': u'Font Conference',
30             u'description': u'This video wasn\'t long enough, so we made it double-spaced.',
31         },
32     }]
33
34     def _real_extract(self, url):
35         mobj = re.match(self._VALID_URL, url)
36         if mobj is None:
37             raise ExtractorError(u'Invalid URL: %s' % url)
38         video_id = mobj.group('videoid')
39
40         info = {
41             'id': video_id,
42             'uploader': None,
43             'upload_date': None,
44         }
45
46         self.report_extraction(video_id)
47         xmlUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id
48         mdoc = self._download_xml(xmlUrl, video_id,
49                                          u'Downloading info XML',
50                                          u'Unable to download video info XML')
51
52         try:
53             videoNode = mdoc.findall('./video')[0]
54             youtubeIdNode = videoNode.find('./youtubeID')
55             if youtubeIdNode is not None:
56                 return self.url_result(youtubeIdNode.text, 'Youtube')
57             info['description'] = videoNode.findall('./description')[0].text
58             info['title'] = videoNode.findall('./caption')[0].text
59             info['thumbnail'] = videoNode.findall('./thumbnail')[0].text
60             next_url = videoNode.findall('./file')[0].text
61         except IndexError:
62             raise ExtractorError(u'Invalid metadata XML file')
63
64         if next_url.endswith(u'manifest.f4m'):
65             manifest_url = next_url + '?hdcore=2.10.3'
66             adoc = self._download_xml(manifest_url, video_id,
67                                          u'Downloading XML manifest',
68                                          u'Unable to download video info XML')
69
70             try:
71                 video_id = adoc.findall('./{http://ns.adobe.com/f4m/1.0}id')[0].text
72             except IndexError:
73                 raise ExtractorError(u'Invalid manifest file')
74             url_pr = compat_urllib_parse_urlparse(info['thumbnail'])
75             info['url'] = url_pr.scheme + '://' + url_pr.netloc + video_id[:-2].replace('.csmil','').replace(',','')
76             info['ext'] = 'mp4'
77         else:
78             # Old-style direct links
79             info['url'] = next_url
80             info['ext'] = determine_ext(info['url'])
81
82         return info