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.

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