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