1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
22 def _media_xml_tag(tag
):
23 return '{http://search.yahoo.com/mrss/}%s' % tag
26 class MTVServicesInfoExtractor(InfoExtractor
):
27 _MOBILE_TEMPLATE
= None
30 def _id_from_uri(uri
):
31 return uri
.split(':')[-1]
33 # This was originally implemented for ComedyCentral, but it also works here
35 def _transform_rtmp_url(rtmp_video_url
):
36 m
= re
.match(r
'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url
)
39 base
= 'http://viacommtvstrmfs.fplive.net/'
40 return base
+ m
.group('finalid')
42 def _get_feed_url(self
, uri
):
45 def _get_thumbnail_url(self
, uri
, itemdoc
):
46 search_path
= '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
47 thumb_node
= itemdoc
.find(search_path
)
48 if thumb_node
is None:
51 return thumb_node
.attrib
['url']
53 def _extract_mobile_video_formats(self
, mtvn_id
):
54 webpage_url
= self
._MOBILE
_TEMPLATE
% mtvn_id
55 req
= compat_urllib_request
.Request(webpage_url
)
56 # Otherwise we get a webpage that would execute some javascript
57 req
.add_header('User-Agent', 'curl/7')
58 webpage
= self
._download
_webpage
(req
, mtvn_id
,
59 'Downloading mobile page')
60 metrics_url
= unescapeHTML(self
._search
_regex
(r
'<a href="(http://metrics.+?)"', webpage
, 'url'))
61 req
= HEADRequest(metrics_url
)
62 response
= self
._request
_webpage
(req
, mtvn_id
, 'Resolving url')
63 url
= response
.geturl()
64 # Transform the url to get the best quality:
65 url
= re
.sub(r
'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url
, 1)
66 return [{'url': url, 'ext': 'mp4'}
]
68 def _extract_video_formats(self
, mdoc
, mtvn_id
):
69 if re
.match(r
'.*/(error_country_block\.swf|geoblock\.mp4)$', mdoc
.find('.//src').text
) is not None:
70 if mtvn_id
is not None and self
._MOBILE
_TEMPLATE
is not None:
71 self
.to_screen('The normal version is not available from your '
72 'country, trying with the mobile version')
73 return self
._extract
_mobile
_video
_formats
(mtvn_id
)
74 raise ExtractorError('This video is not available from your country.',
78 for rendition
in mdoc
.findall('.//rendition'):
80 _
, _
, ext
= rendition
.attrib
['type'].partition('/')
81 rtmp_video_url
= rendition
.find('./src').text
82 if rtmp_video_url
.endswith('siteunavail.png'):
86 'url': self
._transform
_rtmp
_url
(rtmp_video_url
),
87 'format_id': rendition
.get('bitrate'),
88 'width': int(rendition
.get('width')),
89 'height': int(rendition
.get('height')),
91 except (KeyError, TypeError):
92 raise ExtractorError('Invalid rendition field.')
93 self
._sort
_formats
(formats
)
96 def _extract_subtitles(self
, mdoc
, mtvn_id
):
98 for transcript
in mdoc
.findall('.//transcript'):
99 if transcript
.get('kind') != 'captions':
101 lang
= transcript
.get('srclang')
103 'url': compat_str(typographic
.get('src')),
104 'ext': typographic
.get('format')
105 } for typographic
in transcript
.findall('./typographic')]
108 def _get_video_info(self
, itemdoc
):
109 uri
= itemdoc
.find('guid').text
110 video_id
= self
._id
_from
_uri
(uri
)
111 self
.report_extraction(video_id
)
112 mediagen_url
= itemdoc
.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib
['url']
113 # Remove the templates, like &device={device}
114 mediagen_url
= re
.sub(r
'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url
)
115 if 'acceptMethods' not in mediagen_url
:
116 mediagen_url
+= '&acceptMethods=fms'
118 mediagen_doc
= self
._download
_xml
(mediagen_url
, video_id
,
119 'Downloading video urls')
121 item
= mediagen_doc
.find('./video/item')
122 if item
is not None and item
.get('type') == 'text':
123 message
= '%s returned error: ' % self
.IE_NAME
124 if item
.get('code') is not None:
125 message
+= '%s - ' % item
.get('code')
127 raise ExtractorError(message
, expected
=True)
129 description_node
= itemdoc
.find('description')
130 if description_node
is not None:
131 description
= description_node
.text
.strip()
137 title_el
= find_xpath_attr(
138 itemdoc
, './/{http://search.yahoo.com/mrss/}category',
139 'scheme', 'urn:mtvn:video_title')
141 title_el
= itemdoc
.find('.//{http://search.yahoo.com/mrss/}title')
143 title_el
= itemdoc
.find('.//title')
144 if title_el
.text
is None:
147 title
= title_el
.text
149 raise ExtractorError('Could not find video title')
150 title
= title
.strip()
152 # This a short id that's used in the webpage urls
154 mtvn_id_node
= find_xpath_attr(itemdoc
, './/{http://search.yahoo.com/mrss/}category',
155 'scheme', 'urn:mtvn:id')
156 if mtvn_id_node
is not None:
157 mtvn_id
= mtvn_id_node
.text
161 'formats': self
._extract
_video
_formats
(mediagen_doc
, mtvn_id
),
162 'subtitles': self
._extract
_subtitles
(mediagen_doc
, mtvn_id
),
164 'thumbnail': self
._get
_thumbnail
_url
(uri
, itemdoc
),
165 'description': description
,
168 def _get_videos_info(self
, uri
):
169 video_id
= self
._id
_from
_uri
(uri
)
170 feed_url
= self
._get
_feed
_url
(uri
)
171 data
= compat_urllib_parse
.urlencode({'uri': uri}
)
172 idoc
= self
._download
_xml
(
173 feed_url
+ '?' + data
, video_id
,
174 'Downloading info', transform_source
=fix_xml_ampersands
)
175 return self
.playlist_result(
176 [self
._get
_video
_info
(item
) for item
in idoc
.findall('.//item')])
178 def _real_extract(self
, url
):
179 title
= url_basename(url
)
180 webpage
= self
._download
_webpage
(url
, title
)
182 # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
183 # or http://media.mtvnservices.com/{mgid}
184 og_url
= self
._og
_search
_video
_url
(webpage
)
185 mgid
= url_basename(og_url
)
186 if mgid
.endswith('.swf'):
188 except RegexNotFoundError
:
191 if mgid
is None or ':' not in mgid
:
192 mgid
= self
._search
_regex
(
193 [r
'data-mgid="(.*?)"', r
'swfobject.embedSWF\(".*?(mgid:.*?)"'],
196 videos_info
= self
._get
_videos
_info
(mgid
)
200 class MTVServicesEmbeddedIE(MTVServicesInfoExtractor
):
201 IE_NAME
= 'mtvservices:embedded'
202 _VALID_URL
= r
'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
205 # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
206 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906',
207 'md5': 'cb349b21a7897164cede95bd7bf3fbb9',
211 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds',
212 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.',
216 def _get_feed_url(self
, uri
):
217 video_id
= self
._id
_from
_uri
(uri
)
218 site_id
= uri
.replace(video_id
, '')
219 config_url
= ('http://media.mtvnservices.com/pmt/e1/players/{0}/'
220 'context4/context5/config.xml'.format(site_id
))
221 config_doc
= self
._download
_xml
(config_url
, video_id
)
222 feed_node
= config_doc
.find('.//feed')
223 feed_url
= feed_node
.text
.strip().split('?')[0]
226 def _real_extract(self
, url
):
227 mobj
= re
.match(self
._VALID
_URL
, url
)
228 mgid
= mobj
.group('mgid')
229 return self
._get
_videos
_info
(mgid
)
232 class MTVIE(MTVServicesInfoExtractor
):
233 _VALID_URL
= r
'''(?x)^https?://
234 (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
235 m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
237 _FEED_URL
= 'http://www.mtv.com/player/embed/AS3/rss/'
241 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
242 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
246 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
247 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
252 def _get_thumbnail_url(self
, uri
, itemdoc
):
253 return 'http://mtv.mtvnimages.com/uri/' + uri
255 def _real_extract(self
, url
):
256 mobj
= re
.match(self
._VALID
_URL
, url
)
257 video_id
= mobj
.group('videoid')
258 uri
= mobj
.groupdict().get('mgid')
260 webpage
= self
._download
_webpage
(url
, video_id
)
262 # Some videos come from Vevo.com
264 r
'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";', webpage
)
266 vevo_id
= m_vevo
.group(1)
267 self
.to_screen('Vevo video detected: %s' % vevo_id
)
268 return self
.url_result('vevo:%s' % vevo_id
, ie
='Vevo')
270 uri
= self
._html
_search
_regex
(r
'/uri/(.*?)\?', webpage
, 'uri')
271 return self
._get
_videos
_info
(uri
)
274 class MTVIggyIE(MTVServicesInfoExtractor
):
275 IE_NAME
= 'mtviggy.com'
276 _VALID_URL
= r
'https?://www\.mtviggy\.com/videos/.+'
278 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
282 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
285 _FEED_URL
= 'http://all.mtvworldverticals.com/feed-xml/'