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.

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