You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
15 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_parse_urlencode,
  6. compat_str,
  7. compat_xpath,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. find_xpath_attr,
  12. fix_xml_ampersands,
  13. float_or_none,
  14. HEADRequest,
  15. sanitized_Request,
  16. strip_or_none,
  17. timeconvert,
  18. unescapeHTML,
  19. url_basename,
  20. RegexNotFoundError,
  21. xpath_text,
  22. )
  23. def _media_xml_tag(tag):
  24. return '{http://search.yahoo.com/mrss/}%s' % tag
  25. class MTVServicesInfoExtractor(InfoExtractor):
  26. _MOBILE_TEMPLATE = None
  27. _LANG = None
  28. @staticmethod
  29. def _id_from_uri(uri):
  30. return uri.split(':')[-1]
  31. # This was originally implemented for ComedyCentral, but it also works here
  32. @classmethod
  33. def _transform_rtmp_url(cls, rtmp_video_url):
  34. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
  35. if not m:
  36. return {'rtmp': rtmp_video_url}
  37. base = 'http://viacommtvstrmfs.fplive.net/'
  38. return {'http': base + m.group('finalid')}
  39. def _get_feed_url(self, uri):
  40. return self._FEED_URL
  41. def _get_thumbnail_url(self, uri, itemdoc):
  42. search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
  43. thumb_node = itemdoc.find(search_path)
  44. if thumb_node is None:
  45. return None
  46. else:
  47. return thumb_node.attrib['url']
  48. def _extract_mobile_video_formats(self, mtvn_id):
  49. webpage_url = self._MOBILE_TEMPLATE % mtvn_id
  50. req = sanitized_Request(webpage_url)
  51. # Otherwise we get a webpage that would execute some javascript
  52. req.add_header('User-Agent', 'curl/7')
  53. webpage = self._download_webpage(req, mtvn_id,
  54. 'Downloading mobile page')
  55. metrics_url = unescapeHTML(self._search_regex(r'<a href="(http://metrics.+?)"', webpage, 'url'))
  56. req = HEADRequest(metrics_url)
  57. response = self._request_webpage(req, mtvn_id, 'Resolving url')
  58. url = response.geturl()
  59. # Transform the url to get the best quality:
  60. url = re.sub(r'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url, 1)
  61. return [{'url': url, 'ext': 'mp4'}]
  62. def _extract_video_formats(self, mdoc, mtvn_id):
  63. if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4|copyright_error\.flv(?:\?geo\b.+?)?)$', mdoc.find('.//src').text) is not None:
  64. if mtvn_id is not None and self._MOBILE_TEMPLATE is not None:
  65. self.to_screen('The normal version is not available from your '
  66. 'country, trying with the mobile version')
  67. return self._extract_mobile_video_formats(mtvn_id)
  68. raise ExtractorError('This video is not available from your country.',
  69. expected=True)
  70. formats = []
  71. for rendition in mdoc.findall('.//rendition'):
  72. try:
  73. _, _, ext = rendition.attrib['type'].partition('/')
  74. rtmp_video_url = rendition.find('./src').text
  75. if rtmp_video_url.endswith('siteunavail.png'):
  76. continue
  77. new_urls = self._transform_rtmp_url(rtmp_video_url)
  78. formats.extend([{
  79. 'ext': 'flv' if new_url.startswith('rtmp') else ext,
  80. 'url': new_url,
  81. 'format_id': '-'.join(filter(None, [kind, rendition.get('bitrate')])),
  82. 'width': int(rendition.get('width')),
  83. 'height': int(rendition.get('height')),
  84. } for kind, new_url in new_urls.items()])
  85. except (KeyError, TypeError):
  86. raise ExtractorError('Invalid rendition field.')
  87. self._sort_formats(formats)
  88. return formats
  89. def _extract_subtitles(self, mdoc, mtvn_id):
  90. subtitles = {}
  91. for transcript in mdoc.findall('.//transcript'):
  92. if transcript.get('kind') != 'captions':
  93. continue
  94. lang = transcript.get('srclang')
  95. subtitles[lang] = [{
  96. 'url': compat_str(typographic.get('src')),
  97. 'ext': typographic.get('format')
  98. } for typographic in transcript.findall('./typographic')]
  99. return subtitles
  100. def _get_video_info(self, itemdoc):
  101. uri = itemdoc.find('guid').text
  102. video_id = self._id_from_uri(uri)
  103. self.report_extraction(video_id)
  104. content_el = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content')))
  105. mediagen_url = content_el.attrib['url']
  106. # Remove the templates, like &device={device}
  107. mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
  108. if 'acceptMethods' not in mediagen_url:
  109. mediagen_url += '&' if '?' in mediagen_url else '?'
  110. mediagen_url += 'acceptMethods=fms'
  111. mediagen_doc = self._download_xml(mediagen_url, video_id,
  112. 'Downloading video urls')
  113. item = mediagen_doc.find('./video/item')
  114. if item is not None and item.get('type') == 'text':
  115. message = '%s returned error: ' % self.IE_NAME
  116. if item.get('code') is not None:
  117. message += '%s - ' % item.get('code')
  118. message += item.text
  119. raise ExtractorError(message, expected=True)
  120. description = strip_or_none(xpath_text(itemdoc, 'description'))
  121. timestamp = timeconvert(xpath_text(itemdoc, 'pubDate'))
  122. title_el = None
  123. if title_el is None:
  124. title_el = find_xpath_attr(
  125. itemdoc, './/{http://search.yahoo.com/mrss/}category',
  126. 'scheme', 'urn:mtvn:video_title')
  127. if title_el is None:
  128. title_el = itemdoc.find(compat_xpath('.//{http://search.yahoo.com/mrss/}title'))
  129. if title_el is None:
  130. title_el = itemdoc.find(compat_xpath('.//title'))
  131. if title_el.text is None:
  132. title_el = None
  133. title = title_el.text
  134. if title is None:
  135. raise ExtractorError('Could not find video title')
  136. title = title.strip()
  137. # This a short id that's used in the webpage urls
  138. mtvn_id = None
  139. mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category',
  140. 'scheme', 'urn:mtvn:id')
  141. if mtvn_id_node is not None:
  142. mtvn_id = mtvn_id_node.text
  143. return {
  144. 'title': title,
  145. 'formats': self._extract_video_formats(mediagen_doc, mtvn_id),
  146. 'subtitles': self._extract_subtitles(mediagen_doc, mtvn_id),
  147. 'id': video_id,
  148. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  149. 'description': description,
  150. 'duration': float_or_none(content_el.attrib.get('duration')),
  151. 'timestamp': timestamp,
  152. }
  153. def _get_feed_query(self, uri):
  154. data = {'uri': uri}
  155. if self._LANG:
  156. data['lang'] = self._LANG
  157. return compat_urllib_parse_urlencode(data)
  158. def _get_videos_info(self, uri):
  159. video_id = self._id_from_uri(uri)
  160. feed_url = self._get_feed_url(uri)
  161. info_url = feed_url + '?' + self._get_feed_query(uri)
  162. return self._get_videos_info_from_url(info_url, video_id)
  163. def _get_videos_info_from_url(self, url, video_id):
  164. idoc = self._download_xml(
  165. url, video_id,
  166. 'Downloading info', transform_source=fix_xml_ampersands)
  167. title = xpath_text(idoc, './channel/title')
  168. description = xpath_text(idoc, './channel/description')
  169. return self.playlist_result(
  170. [self._get_video_info(item) for item in idoc.findall('.//item')],
  171. playlist_title=title, playlist_description=description)
  172. def _extract_mgid(self, webpage):
  173. try:
  174. # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
  175. # or http://media.mtvnservices.com/{mgid}
  176. og_url = self._og_search_video_url(webpage)
  177. mgid = url_basename(og_url)
  178. if mgid.endswith('.swf'):
  179. mgid = mgid[:-4]
  180. except RegexNotFoundError:
  181. mgid = None
  182. if mgid is None or ':' not in mgid:
  183. mgid = self._search_regex(
  184. [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'],
  185. webpage, 'mgid', default=None)
  186. if not mgid:
  187. sm4_embed = self._html_search_meta(
  188. 'sm4:video:embed', webpage, 'sm4 embed', default='')
  189. mgid = self._search_regex(
  190. r'embed/(mgid:.+?)["\'&?/]', sm4_embed, 'mgid')
  191. return mgid
  192. def _real_extract(self, url):
  193. title = url_basename(url)
  194. webpage = self._download_webpage(url, title)
  195. mgid = self._extract_mgid(webpage)
  196. videos_info = self._get_videos_info(mgid)
  197. return videos_info
  198. class MTVServicesEmbeddedIE(MTVServicesInfoExtractor):
  199. IE_NAME = 'mtvservices:embedded'
  200. _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
  201. _TEST = {
  202. # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
  203. 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906',
  204. 'md5': 'cb349b21a7897164cede95bd7bf3fbb9',
  205. 'info_dict': {
  206. 'id': '1043906',
  207. 'ext': 'mp4',
  208. 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds',
  209. 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.',
  210. 'timestamp': 1400126400,
  211. 'upload_date': '20140515',
  212. },
  213. }
  214. @staticmethod
  215. def _extract_url(webpage):
  216. mobj = re.search(
  217. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//media.mtvnservices.com/embed/.+?)\1', webpage)
  218. if mobj:
  219. return mobj.group('url')
  220. def _get_feed_url(self, uri):
  221. video_id = self._id_from_uri(uri)
  222. site_id = uri.replace(video_id, '')
  223. config_url = ('http://media.mtvnservices.com/pmt/e1/players/{0}/'
  224. 'context4/context5/config.xml'.format(site_id))
  225. config_doc = self._download_xml(config_url, video_id)
  226. feed_node = config_doc.find('.//feed')
  227. feed_url = feed_node.text.strip().split('?')[0]
  228. return feed_url
  229. def _real_extract(self, url):
  230. mobj = re.match(self._VALID_URL, url)
  231. mgid = mobj.group('mgid')
  232. return self._get_videos_info(mgid)
  233. class MTVIE(MTVServicesInfoExtractor):
  234. _VALID_URL = r'''(?x)^https?://
  235. (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
  236. m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
  237. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  238. _TESTS = [
  239. {
  240. 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  241. 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
  242. 'info_dict': {
  243. 'id': '853555',
  244. 'ext': 'mp4',
  245. 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
  246. 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  247. 'timestamp': 1352610000,
  248. 'upload_date': '20121111',
  249. },
  250. },
  251. ]
  252. def _get_thumbnail_url(self, uri, itemdoc):
  253. return 'http://mtv.mtvnimages.com/uri/' + uri
  254. def _real_extract(self, url):
  255. mobj = re.match(self._VALID_URL, url)
  256. video_id = mobj.group('videoid')
  257. uri = mobj.groupdict().get('mgid')
  258. if uri is None:
  259. webpage = self._download_webpage(url, video_id)
  260. # Some videos come from Vevo.com
  261. m_vevo = re.search(
  262. r'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";', webpage)
  263. if m_vevo:
  264. vevo_id = m_vevo.group(1)
  265. self.to_screen('Vevo video detected: %s' % vevo_id)
  266. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  267. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
  268. return self._get_videos_info(uri)
  269. class MTVDEIE(MTVServicesInfoExtractor):
  270. IE_NAME = 'mtv.de'
  271. _VALID_URL = r'https?://(?:www\.)?mtv\.de/(?:artists|shows|news)/(?:[^/]+/)*(?P<id>\d+)-[^/#?]+/*(?:[#?].*)?$'
  272. _TESTS = [{
  273. 'url': 'http://www.mtv.de/artists/10571-cro/videos/61131-traum',
  274. 'info_dict': {
  275. 'id': 'music_video-a50bc5f0b3aa4b3190aa',
  276. 'ext': 'flv',
  277. 'title': 'MusicVideo_cro-traum',
  278. 'description': 'Cro - Traum',
  279. },
  280. 'params': {
  281. # rtmp download
  282. 'skip_download': True,
  283. },
  284. 'skip': 'Blocked at Travis CI',
  285. }, {
  286. # mediagen URL without query (e.g. http://videos.mtvnn.com/mediagen/e865da714c166d18d6f80893195fcb97)
  287. 'url': 'http://www.mtv.de/shows/933-teen-mom-2/staffeln/5353/folgen/63565-enthullungen',
  288. 'info_dict': {
  289. 'id': 'local_playlist-f5ae778b9832cc837189',
  290. 'ext': 'flv',
  291. 'title': 'Episode_teen-mom-2_shows_season-5_episode-1_full-episode_part1',
  292. },
  293. 'params': {
  294. # rtmp download
  295. 'skip_download': True,
  296. },
  297. 'skip': 'Blocked at Travis CI',
  298. }, {
  299. 'url': 'http://www.mtv.de/news/77491-mtv-movies-spotlight-pixels-teil-3',
  300. 'info_dict': {
  301. 'id': 'local_playlist-4e760566473c4c8c5344',
  302. 'ext': 'mp4',
  303. 'title': 'Article_mtv-movies-spotlight-pixels-teil-3_short-clips_part1',
  304. 'description': 'MTV Movies Supercut',
  305. },
  306. 'params': {
  307. # rtmp download
  308. 'skip_download': True,
  309. },
  310. 'skip': 'Das Video kann zur Zeit nicht abgespielt werden.',
  311. }]
  312. def _real_extract(self, url):
  313. video_id = self._match_id(url)
  314. webpage = self._download_webpage(url, video_id)
  315. playlist = self._parse_json(
  316. self._search_regex(
  317. r'window\.pagePlaylist\s*=\s*(\[.+?\]);\n', webpage, 'page playlist'),
  318. video_id)
  319. def _mrss_url(item):
  320. return item['mrss'] + item.get('mrssvars', '')
  321. # news pages contain single video in playlist with different id
  322. if len(playlist) == 1:
  323. return self._get_videos_info_from_url(_mrss_url(playlist[0]), video_id)
  324. for item in playlist:
  325. item_id = item.get('id')
  326. if item_id and compat_str(item_id) == video_id:
  327. return self._get_videos_info_from_url(_mrss_url(item), video_id)