Merge pull request #1120 from pishposhmcgee/patch-1
[youtube-dl] / youtube_dl / extractor / collegehumor.py
1 import re
2 import xml.etree.ElementTree
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse_urlparse,
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     _TEST = {
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     def _real_extract(self, url):
26         mobj = re.match(self._VALID_URL, url)
27         if mobj is None:
28             raise ExtractorError(u'Invalid URL: %s' % url)
29         video_id = mobj.group('videoid')
30
31         info = {
32             'id': video_id,
33             'uploader': None,
34             'upload_date': None,
35         }
36
37         self.report_extraction(video_id)
38         xmlUrl = 'http://www.collegehumor.com/moogaloop/video/' + video_id
39         metaXml = self._download_webpage(xmlUrl, video_id,
40                                          u'Downloading info XML',
41                                          u'Unable to download video info XML')
42
43         mdoc = xml.etree.ElementTree.fromstring(metaXml)
44         try:
45             videoNode = mdoc.findall('./video')[0]
46             youtubeIdNode = videoNode.find('./youtubeID')
47             if youtubeIdNode is not None:
48                 return self.url_result(youtubeIdNode.text, 'Youtube')
49             info['description'] = videoNode.findall('./description')[0].text
50             info['title'] = videoNode.findall('./caption')[0].text
51             info['thumbnail'] = videoNode.findall('./thumbnail')[0].text
52             manifest_url = videoNode.findall('./file')[0].text
53         except IndexError:
54             raise ExtractorError(u'Invalid metadata XML file')
55
56         manifest_url += '?hdcore=2.10.3'
57         manifestXml = self._download_webpage(manifest_url, video_id,
58                                              u'Downloading XML manifest',
59                                              u'Unable to download video info XML')
60
61         adoc = xml.etree.ElementTree.fromstring(manifestXml)
62         try:
63             media_node = adoc.findall('./{http://ns.adobe.com/f4m/1.0}media')[0]
64             node_id = media_node.attrib['url']
65             video_id = adoc.findall('./{http://ns.adobe.com/f4m/1.0}id')[0].text
66         except IndexError as err:
67             raise ExtractorError(u'Invalid manifest file')
68
69         url_pr = compat_urllib_parse_urlparse(info['thumbnail'])
70
71         info['url'] = url_pr.scheme + '://' + url_pr.netloc + video_id[:-2].replace('.csmil','').replace(',','')
72         info['ext'] = 'mp4'
73         return [info]