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.

376 lines
14 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. unescapeHTML,
  17. url_basename,
  18. RegexNotFoundError,
  19. xpath_text,
  20. )
  21. def _media_xml_tag(tag):
  22. return '{http://search.yahoo.com/mrss/}%s' % tag
  23. class MTVServicesInfoExtractor(InfoExtractor):
  24. _MOBILE_TEMPLATE = None
  25. _LANG = None
  26. @staticmethod
  27. def _id_from_uri(uri):
  28. return uri.split(':')[-1]
  29. # This was originally implemented for ComedyCentral, but it also works here
  30. @staticmethod
  31. def _transform_rtmp_url(rtmp_video_url):
  32. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
  33. if not m:
  34. return rtmp_video_url
  35. base = 'http://viacommtvstrmfs.fplive.net/'
  36. return base + m.group('finalid')
  37. def _get_feed_url(self, uri):
  38. return self._FEED_URL
  39. def _get_thumbnail_url(self, uri, itemdoc):
  40. search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
  41. thumb_node = itemdoc.find(search_path)
  42. if thumb_node is None:
  43. return None
  44. else:
  45. return thumb_node.attrib['url']
  46. def _extract_mobile_video_formats(self, mtvn_id):
  47. webpage_url = self._MOBILE_TEMPLATE % mtvn_id
  48. req = sanitized_Request(webpage_url)
  49. # Otherwise we get a webpage that would execute some javascript
  50. req.add_header('User-Agent', 'curl/7')
  51. webpage = self._download_webpage(req, mtvn_id,
  52. 'Downloading mobile page')
  53. metrics_url = unescapeHTML(self._search_regex(r'<a href="(http://metrics.+?)"', webpage, 'url'))
  54. req = HEADRequest(metrics_url)
  55. response = self._request_webpage(req, mtvn_id, 'Resolving url')
  56. url = response.geturl()
  57. # Transform the url to get the best quality:
  58. url = re.sub(r'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url, 1)
  59. return [{'url': url, 'ext': 'mp4'}]
  60. def _extract_video_formats(self, mdoc, mtvn_id):
  61. if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4|copyright_error\.flv(?:\?geo\b.+?)?)$', mdoc.find('.//src').text) is not None:
  62. if mtvn_id is not None and self._MOBILE_TEMPLATE is not None:
  63. self.to_screen('The normal version is not available from your '
  64. 'country, trying with the mobile version')
  65. return self._extract_mobile_video_formats(mtvn_id)
  66. raise ExtractorError('This video is not available from your country.',
  67. expected=True)
  68. formats = []
  69. for rendition in mdoc.findall('.//rendition'):
  70. try:
  71. _, _, ext = rendition.attrib['type'].partition('/')
  72. rtmp_video_url = rendition.find('./src').text
  73. if rtmp_video_url.endswith('siteunavail.png'):
  74. continue
  75. new_url = self._transform_rtmp_url(rtmp_video_url)
  76. formats.append({
  77. 'ext': 'flv' if new_url.startswith('rtmp') else ext,
  78. 'url': new_url,
  79. 'format_id': rendition.get('bitrate'),
  80. 'width': int(rendition.get('width')),
  81. 'height': int(rendition.get('height')),
  82. })
  83. except (KeyError, TypeError):
  84. raise ExtractorError('Invalid rendition field.')
  85. self._sort_formats(formats)
  86. return formats
  87. def _extract_subtitles(self, mdoc, mtvn_id):
  88. subtitles = {}
  89. for transcript in mdoc.findall('.//transcript'):
  90. if transcript.get('kind') != 'captions':
  91. continue
  92. lang = transcript.get('srclang')
  93. subtitles[lang] = [{
  94. 'url': compat_str(typographic.get('src')),
  95. 'ext': typographic.get('format')
  96. } for typographic in transcript.findall('./typographic')]
  97. return subtitles
  98. def _get_video_info(self, itemdoc):
  99. uri = itemdoc.find('guid').text
  100. video_id = self._id_from_uri(uri)
  101. self.report_extraction(video_id)
  102. content_el = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content')))
  103. mediagen_url = content_el.attrib['url']
  104. # Remove the templates, like &device={device}
  105. mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
  106. if 'acceptMethods' not in mediagen_url:
  107. mediagen_url += '&' if '?' in mediagen_url else '?'
  108. mediagen_url += 'acceptMethods=fms'
  109. mediagen_doc = self._download_xml(mediagen_url, video_id,
  110. 'Downloading video urls')
  111. item = mediagen_doc.find('./video/item')
  112. if item is not None and item.get('type') == 'text':
  113. message = '%s returned error: ' % self.IE_NAME
  114. if item.get('code') is not None:
  115. message += '%s - ' % item.get('code')
  116. message += item.text
  117. raise ExtractorError(message, expected=True)
  118. description = xpath_text(itemdoc, 'description')
  119. title_el = None
  120. if title_el is None:
  121. title_el = find_xpath_attr(
  122. itemdoc, './/{http://search.yahoo.com/mrss/}category',
  123. 'scheme', 'urn:mtvn:video_title')
  124. if title_el is None:
  125. title_el = itemdoc.find(compat_xpath('.//{http://search.yahoo.com/mrss/}title'))
  126. if title_el is None:
  127. title_el = itemdoc.find(compat_xpath('.//title'))
  128. if title_el.text is None:
  129. title_el = None
  130. title = title_el.text
  131. if title is None:
  132. raise ExtractorError('Could not find video title')
  133. title = title.strip()
  134. # This a short id that's used in the webpage urls
  135. mtvn_id = None
  136. mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category',
  137. 'scheme', 'urn:mtvn:id')
  138. if mtvn_id_node is not None:
  139. mtvn_id = mtvn_id_node.text
  140. return {
  141. 'title': title,
  142. 'formats': self._extract_video_formats(mediagen_doc, mtvn_id),
  143. 'subtitles': self._extract_subtitles(mediagen_doc, mtvn_id),
  144. 'id': video_id,
  145. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  146. 'description': description,
  147. 'duration': float_or_none(content_el.attrib.get('duration')),
  148. }
  149. def _get_feed_query(self, uri):
  150. data = {'uri': uri}
  151. if self._LANG:
  152. data['lang'] = self._LANG
  153. return compat_urllib_parse_urlencode(data)
  154. def _get_videos_info(self, uri):
  155. video_id = self._id_from_uri(uri)
  156. feed_url = self._get_feed_url(uri)
  157. info_url = feed_url + '?' + self._get_feed_query(uri)
  158. return self._get_videos_info_from_url(info_url, video_id)
  159. def _get_videos_info_from_url(self, url, video_id):
  160. idoc = self._download_xml(
  161. url, video_id,
  162. 'Downloading info', transform_source=fix_xml_ampersands)
  163. return self.playlist_result(
  164. [self._get_video_info(item) for item in idoc.findall('.//item')])
  165. def _extract_mgid(self, webpage):
  166. try:
  167. # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
  168. # or http://media.mtvnservices.com/{mgid}
  169. og_url = self._og_search_video_url(webpage)
  170. mgid = url_basename(og_url)
  171. if mgid.endswith('.swf'):
  172. mgid = mgid[:-4]
  173. except RegexNotFoundError:
  174. mgid = None
  175. if mgid is None or ':' not in mgid:
  176. mgid = self._search_regex(
  177. [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'],
  178. webpage, 'mgid', default=None)
  179. if not mgid:
  180. sm4_embed = self._html_search_meta(
  181. 'sm4:video:embed', webpage, 'sm4 embed', default='')
  182. mgid = self._search_regex(
  183. r'embed/(mgid:.+?)["\'&?/]', sm4_embed, 'mgid')
  184. return mgid
  185. def _real_extract(self, url):
  186. title = url_basename(url)
  187. webpage = self._download_webpage(url, title)
  188. mgid = self._extract_mgid(webpage)
  189. videos_info = self._get_videos_info(mgid)
  190. return videos_info
  191. class MTVServicesEmbeddedIE(MTVServicesInfoExtractor):
  192. IE_NAME = 'mtvservices:embedded'
  193. _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
  194. _TEST = {
  195. # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
  196. 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906',
  197. 'md5': 'cb349b21a7897164cede95bd7bf3fbb9',
  198. 'info_dict': {
  199. 'id': '1043906',
  200. 'ext': 'mp4',
  201. 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds',
  202. 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.',
  203. },
  204. }
  205. @staticmethod
  206. def _extract_url(webpage):
  207. mobj = re.search(
  208. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//media.mtvnservices.com/embed/.+?)\1', webpage)
  209. if mobj:
  210. return mobj.group('url')
  211. def _get_feed_url(self, uri):
  212. video_id = self._id_from_uri(uri)
  213. site_id = uri.replace(video_id, '')
  214. config_url = ('http://media.mtvnservices.com/pmt/e1/players/{0}/'
  215. 'context4/context5/config.xml'.format(site_id))
  216. config_doc = self._download_xml(config_url, video_id)
  217. feed_node = config_doc.find('.//feed')
  218. feed_url = feed_node.text.strip().split('?')[0]
  219. return feed_url
  220. def _real_extract(self, url):
  221. mobj = re.match(self._VALID_URL, url)
  222. mgid = mobj.group('mgid')
  223. return self._get_videos_info(mgid)
  224. class MTVIE(MTVServicesInfoExtractor):
  225. _VALID_URL = r'''(?x)^https?://
  226. (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
  227. m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
  228. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  229. _TESTS = [
  230. {
  231. 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  232. 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
  233. 'info_dict': {
  234. 'id': '853555',
  235. 'ext': 'mp4',
  236. 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
  237. 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  238. },
  239. },
  240. ]
  241. def _get_thumbnail_url(self, uri, itemdoc):
  242. return 'http://mtv.mtvnimages.com/uri/' + uri
  243. def _real_extract(self, url):
  244. mobj = re.match(self._VALID_URL, url)
  245. video_id = mobj.group('videoid')
  246. uri = mobj.groupdict().get('mgid')
  247. if uri is None:
  248. webpage = self._download_webpage(url, video_id)
  249. # Some videos come from Vevo.com
  250. m_vevo = re.search(
  251. r'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";', webpage)
  252. if m_vevo:
  253. vevo_id = m_vevo.group(1)
  254. self.to_screen('Vevo video detected: %s' % vevo_id)
  255. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  256. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
  257. return self._get_videos_info(uri)
  258. class MTVIggyIE(MTVServicesInfoExtractor):
  259. IE_NAME = 'mtviggy.com'
  260. _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+'
  261. _TEST = {
  262. 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
  263. 'info_dict': {
  264. 'id': '984696',
  265. 'ext': 'mp4',
  266. 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
  267. }
  268. }
  269. _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/'
  270. class MTVDEIE(MTVServicesInfoExtractor):
  271. IE_NAME = 'mtv.de'
  272. _VALID_URL = r'https?://(?:www\.)?mtv\.de/(?:artists|shows|news)/(?:[^/]+/)*(?P<id>\d+)-[^/#?]+/*(?:[#?].*)?$'
  273. _TESTS = [{
  274. 'url': 'http://www.mtv.de/artists/10571-cro/videos/61131-traum',
  275. 'info_dict': {
  276. 'id': 'music_video-a50bc5f0b3aa4b3190aa',
  277. 'ext': 'mp4',
  278. 'title': 'MusicVideo_cro-traum',
  279. 'description': 'Cro - Traum',
  280. },
  281. 'params': {
  282. # rtmp download
  283. 'skip_download': True,
  284. },
  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': 'mp4',
  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. }, {
  298. # single video in pagePlaylist with different id
  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. }]
  311. def _real_extract(self, url):
  312. video_id = self._match_id(url)
  313. webpage = self._download_webpage(url, video_id)
  314. playlist = self._parse_json(
  315. self._search_regex(
  316. r'window\.pagePlaylist\s*=\s*(\[.+?\]);\n', webpage, 'page playlist'),
  317. video_id)
  318. # news pages contain single video in playlist with different id
  319. if len(playlist) == 1:
  320. return self._get_videos_info_from_url(playlist[0]['mrss'], video_id)
  321. for item in playlist:
  322. item_id = item.get('id')
  323. if item_id and compat_str(item_id) == video_id:
  324. return self._get_videos_info_from_url(item['mrss'], video_id)