]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/mtv.py
MTVIE: fix xml tags in the media namespace (python2.6)
[yt-dlp.git] / youtube_dl / extractor / mtv.py
CommitLineData
fc287219 1import re
fc287219
PH
2import xml.etree.ElementTree
3
4from .common import InfoExtractor
5from ..utils import (
f7e02595 6 compat_urllib_parse,
fc287219
PH
7 ExtractorError,
8)
9
300fcad8
JMF
10def _media_xml_tag(tag):
11 return '{http://search.yahoo.com/mrss/}%s' % tag
fc287219
PH
12
13class MTVIE(InfoExtractor):
f7e02595
JMF
14 _VALID_URL = r'^https?://(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$'
15
16 _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
17
18 _TESTS = [
19 {
20 u'url': u'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
21 u'file': u'853555.mp4',
22 u'md5': u'850f3f143316b1e71fa56a4edfd6e0f8',
23 u'info_dict': {
24 u'title': u'Taylor Swift - "Ours (VH1 Storytellers)"',
25 u'description': u'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
26 },
27 },
28 {
29 u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
30 u'file': u'USCJY1331283.mp4',
31 u'md5': u'73b4e7fcadd88929292fe52c3ced8caf',
32 u'info_dict': {
33 u'title': u'Everything Has Changed',
34 u'upload_date': u'20130606',
35 u'uploader': u'Taylor Swift',
36 },
37 u'skip': u'VEVO is only available in some countries',
38 },
39 ]
40
41 @staticmethod
42 def _id_from_uri(uri):
43 return uri.split(':')[-1]
44
45 # This was originally implemented for ComedyCentral, but it also works here
46 @staticmethod
47 def _transform_rtmp_url(rtmp_video_url):
48 m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
49 if not m:
50 raise ExtractorError(u'Cannot transform RTMP url')
51 base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
52 return base + m.group('finalid')
53
54 def _extract_video_url(self, metadataXml):
55 if '/error_country_block.swf' in metadataXml:
56 raise ExtractorError(u'This video is not available from your country.', expected=True)
57 mdoc = xml.etree.ElementTree.fromstring(metadataXml.encode('utf-8'))
58 renditions = mdoc.findall('.//rendition')
59
60 # For now, always pick the highest quality.
61 rendition = renditions[-1]
62
63 try:
64 _,_,ext = rendition.attrib['type'].partition('/')
65 format = ext + '-' + rendition.attrib['width'] + 'x' + rendition.attrib['height'] + '_' + rendition.attrib['bitrate']
66 rtmp_video_url = rendition.find('./src').text
67 except KeyError:
68 raise ExtractorError('Invalid rendition field.')
69 video_url = self._transform_rtmp_url(rtmp_video_url)
70 return {'ext': ext, 'url': video_url, 'format': format}
71
72 def _get_video_info(self, itemdoc):
73 uri = itemdoc.find('guid').text
74 video_id = self._id_from_uri(uri)
75 self.report_extraction(video_id)
300fcad8 76 mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
f7e02595
JMF
77 if 'acceptMethods' not in mediagen_url:
78 mediagen_url += '&acceptMethods=fms'
79 mediagen_page = self._download_webpage(mediagen_url, video_id,
80 u'Downloading video urls')
81 video_info = self._extract_video_url(mediagen_page)
82
83 description_node = itemdoc.find('description')
84 if description_node is not None:
85 description = description_node.text
86 else:
87 description = None
88 video_info.update({'title': itemdoc.find('title').text,
89 'id': video_id,
90 'thumbnail': 'http://mtv.mtvnimages.com/uri/' + uri,
91 'description': description,
92 })
93 return video_info
94
95 def _get_videos_info(self, uri):
96 video_id = self._id_from_uri(uri)
97 data = compat_urllib_parse.urlencode({'uri': uri})
98 infoXml = self._download_webpage(self._FEED_URL +'?' + data, video_id,
99 u'Downloading info')
100 idoc = xml.etree.ElementTree.fromstring(infoXml.encode('utf-8'))
101 return [self._get_video_info(item) for item in idoc.findall('.//item')]
fc287219
PH
102
103 def _real_extract(self, url):
104 mobj = re.match(self._VALID_URL, url)
fc287219
PH
105 video_id = mobj.group('videoid')
106
107 webpage = self._download_webpage(url, video_id)
108
1c251cd9
JMF
109 # Some videos come from Vevo.com
110 m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
111 webpage, re.DOTALL)
112 if m_vevo:
113 vevo_id = m_vevo.group(1);
114 self.to_screen(u'Vevo video detected: %s' % vevo_id)
115 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
116
f7e02595
JMF
117 uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
118 return self._get_videos_info(uri)