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.

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