]>
Commit | Line | Data |
---|---|---|
fc287219 | 1 | import re |
fc287219 PH |
2 | import xml.etree.ElementTree |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
f7e02595 | 6 | compat_urllib_parse, |
fc287219 PH |
7 | ExtractorError, |
8 | ) | |
9 | ||
300fcad8 JMF |
10 | def _media_xml_tag(tag): |
11 | return '{http://search.yahoo.com/mrss/}%s' % tag | |
fc287219 | 12 | |
f7e02595 | 13 | |
84db8181 | 14 | class MTVServicesInfoExtractor(InfoExtractor): |
f7e02595 JMF |
15 | @staticmethod |
16 | def _id_from_uri(uri): | |
17 | return uri.split(':')[-1] | |
18 | ||
19 | # This was originally implemented for ComedyCentral, but it also works here | |
20 | @staticmethod | |
21 | def _transform_rtmp_url(rtmp_video_url): | |
22 | m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url) | |
23 | if not m: | |
63b7b722 | 24 | return rtmp_video_url |
f7e02595 | 25 | base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/' |
ab2f744b JMF |
26 | return base + m.group('finalid') |
27 | ||
28 | def _get_thumbnail_url(self, uri, itemdoc): | |
84db8181 JMF |
29 | search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail')) |
30 | thumb_node = itemdoc.find(search_path) | |
31 | if thumb_node is None: | |
32 | return None | |
33 | else: | |
34 | return thumb_node.attrib['url'] | |
f7e02595 | 35 | |
f13d0933 | 36 | def _extract_video_formats(self, metadataXml): |
f7e02595 JMF |
37 | if '/error_country_block.swf' in metadataXml: |
38 | raise ExtractorError(u'This video is not available from your country.', expected=True) | |
39 | mdoc = xml.etree.ElementTree.fromstring(metadataXml.encode('utf-8')) | |
f7e02595 | 40 | |
f13d0933 PH |
41 | formats = [] |
42 | for rendition in mdoc.findall('.//rendition'): | |
43 | try: | |
44 | _, _, ext = rendition.attrib['type'].partition('/') | |
45 | rtmp_video_url = rendition.find('./src').text | |
46 | formats.append({'ext': ext, | |
47 | 'url': self._transform_rtmp_url(rtmp_video_url), | |
48 | 'format_id': rendition.get('bitrate'), | |
49 | 'width': int(rendition.get('width')), | |
50 | 'height': int(rendition.get('height')), | |
51 | }) | |
52 | except (KeyError, TypeError): | |
53 | raise ExtractorError('Invalid rendition field.') | |
54 | return formats | |
f7e02595 JMF |
55 | |
56 | def _get_video_info(self, itemdoc): | |
57 | uri = itemdoc.find('guid').text | |
58 | video_id = self._id_from_uri(uri) | |
59 | self.report_extraction(video_id) | |
300fcad8 | 60 | mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url'] |
321a01f9 JMF |
61 | # Remove the templates, like &device={device} |
62 | mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', u'', mediagen_url) | |
f7e02595 JMF |
63 | if 'acceptMethods' not in mediagen_url: |
64 | mediagen_url += '&acceptMethods=fms' | |
65 | mediagen_page = self._download_webpage(mediagen_url, video_id, | |
66 | u'Downloading video urls') | |
f7e02595 JMF |
67 | |
68 | description_node = itemdoc.find('description') | |
69 | if description_node is not None: | |
0ab4ff63 | 70 | description = description_node.text.strip() |
f7e02595 JMF |
71 | else: |
72 | description = None | |
f13d0933 | 73 | |
fb7abb31 | 74 | return { |
f13d0933 PH |
75 | 'title': itemdoc.find('title').text, |
76 | 'formats': self._extract_video_formats(mediagen_page), | |
77 | 'id': video_id, | |
78 | 'thumbnail': self._get_thumbnail_url(uri, itemdoc), | |
79 | 'description': description, | |
80 | } | |
81 | ||
f7e02595 JMF |
82 | def _get_videos_info(self, uri): |
83 | video_id = self._id_from_uri(uri) | |
84 | data = compat_urllib_parse.urlencode({'uri': uri}) | |
e2b38da9 PH |
85 | |
86 | def fix_ampersand(s): | |
87 | """ Fix unencoded ampersand in XML """ | |
88 | return s.replace(u'& ', '& ') | |
89 | idoc = self._download_xml( | |
90 | self._FEED_URL + '?' + data, video_id, | |
91 | u'Downloading info', transform_source=fix_ampersand) | |
f7e02595 | 92 | return [self._get_video_info(item) for item in idoc.findall('.//item')] |
fc287219 | 93 | |
84db8181 JMF |
94 | |
95 | class MTVIE(MTVServicesInfoExtractor): | |
5c541b2c JMF |
96 | _VALID_URL = r'''(?x)^https?:// |
97 | (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| | |
98 | m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' | |
84db8181 JMF |
99 | |
100 | _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' | |
101 | ||
102 | _TESTS = [ | |
103 | { | |
104 | u'url': u'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', | |
105 | u'file': u'853555.mp4', | |
106 | u'md5': u'850f3f143316b1e71fa56a4edfd6e0f8', | |
107 | u'info_dict': { | |
108 | u'title': u'Taylor Swift - "Ours (VH1 Storytellers)"', | |
109 | u'description': u'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.', | |
110 | }, | |
111 | }, | |
112 | { | |
113 | u'add_ie': ['Vevo'], | |
114 | u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml', | |
115 | u'file': u'USCJY1331283.mp4', | |
116 | u'md5': u'73b4e7fcadd88929292fe52c3ced8caf', | |
117 | u'info_dict': { | |
118 | u'title': u'Everything Has Changed', | |
119 | u'upload_date': u'20130606', | |
120 | u'uploader': u'Taylor Swift', | |
121 | }, | |
122 | u'skip': u'VEVO is only available in some countries', | |
123 | }, | |
124 | ] | |
125 | ||
126 | def _get_thumbnail_url(self, uri, itemdoc): | |
127 | return 'http://mtv.mtvnimages.com/uri/' + uri | |
128 | ||
fc287219 PH |
129 | def _real_extract(self, url): |
130 | mobj = re.match(self._VALID_URL, url) | |
fc287219 | 131 | video_id = mobj.group('videoid') |
5c541b2c JMF |
132 | uri = mobj.group('mgid') |
133 | if uri is None: | |
134 | webpage = self._download_webpage(url, video_id) | |
135 | ||
136 | # Some videos come from Vevo.com | |
137 | m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";', | |
138 | webpage, re.DOTALL) | |
139 | if m_vevo: | |
140 | vevo_id = m_vevo.group(1); | |
141 | self.to_screen(u'Vevo video detected: %s' % vevo_id) | |
142 | return self.url_result('vevo:%s' % vevo_id, ie='Vevo') | |
143 | ||
144 | uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri') | |
f7e02595 | 145 | return self._get_videos_info(uri) |