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.

286 lines
11 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_parse_qs,
  6. compat_urllib_parse_unquote,
  7. compat_urllib_parse_urlencode,
  8. )
  9. from ..utils import (
  10. determine_ext,
  11. ExtractorError,
  12. int_or_none,
  13. get_element_by_attribute,
  14. mimetype2ext,
  15. )
  16. class MetacafeIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:www\.)?metacafe\.com/watch/(?P<video_id>[^/]+)/(?P<display_id>[^/?#]+)'
  18. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  19. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  20. IE_NAME = 'metacafe'
  21. _TESTS = [
  22. # Youtube video
  23. {
  24. 'add_ie': ['Youtube'],
  25. 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/',
  26. 'info_dict': {
  27. 'id': '_aUehQsCQtM',
  28. 'ext': 'mp4',
  29. 'upload_date': '20090102',
  30. 'title': 'The Electric Company | "Short I" | PBS KIDS GO!',
  31. 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8',
  32. 'uploader': 'PBS',
  33. 'uploader_id': 'PBS'
  34. }
  35. },
  36. # Normal metacafe video
  37. {
  38. 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  39. 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad',
  40. 'info_dict': {
  41. 'id': '11121940',
  42. 'ext': 'mp4',
  43. 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4',
  44. 'uploader': 'ign',
  45. 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  46. },
  47. 'skip': 'Page is temporarily unavailable.',
  48. },
  49. # metacafe video with family filter
  50. {
  51. 'url': 'http://www.metacafe.com/watch/2155630/adult_art_by_david_hart_156/',
  52. 'md5': 'b06082c5079bbdcde677a6291fbdf376',
  53. 'info_dict': {
  54. 'id': '2155630',
  55. 'ext': 'mp4',
  56. 'title': 'Adult Art By David Hart 156',
  57. 'uploader': '63346',
  58. 'description': 'md5:9afac8fc885252201ad14563694040fc',
  59. },
  60. 'params': {
  61. 'skip_download': True,
  62. },
  63. },
  64. # AnyClip video
  65. {
  66. 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/',
  67. 'info_dict': {
  68. 'id': 'an-dVVXnuY7Jh77J',
  69. 'ext': 'mp4',
  70. 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3',
  71. 'uploader': 'AnyClip',
  72. 'description': 'md5:cbef0460d31e3807f6feb4e7a5952e5b',
  73. },
  74. },
  75. # age-restricted video
  76. {
  77. 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  78. 'md5': '98dde7c1a35d02178e8ab7560fe8bd09',
  79. 'info_dict': {
  80. 'id': '5186653',
  81. 'ext': 'mp4',
  82. 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  83. 'uploader': 'Dwayne Pipe',
  84. 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b',
  85. 'age_limit': 18,
  86. },
  87. },
  88. # cbs video
  89. {
  90. 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/',
  91. 'info_dict': {
  92. 'id': '8VD4r_Zws8VP',
  93. 'ext': 'flv',
  94. 'title': 'Open: This is Face the Nation, February 9',
  95. 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476',
  96. 'duration': 96,
  97. 'uploader': 'CBSI-NEW',
  98. 'upload_date': '20140209',
  99. 'timestamp': 1391959800,
  100. },
  101. 'params': {
  102. # rtmp download
  103. 'skip_download': True,
  104. },
  105. },
  106. # Movieclips.com video
  107. {
  108. 'url': 'http://www.metacafe.com/watch/mv-Wy7ZU/my_week_with_marilyn_do_you_love_me/',
  109. 'info_dict': {
  110. 'id': 'mv-Wy7ZU',
  111. 'ext': 'mp4',
  112. 'title': 'My Week with Marilyn - Do You Love Me?',
  113. 'description': 'From the movie My Week with Marilyn - Colin (Eddie Redmayne) professes his love to Marilyn (Michelle Williams) and gets her to promise to return to set and finish the movie.',
  114. 'uploader': 'movie_trailers',
  115. 'duration': 176,
  116. },
  117. 'params': {
  118. 'skip_download': 'requires rtmpdump',
  119. }
  120. }
  121. ]
  122. def report_disclaimer(self):
  123. self.to_screen('Retrieving disclaimer')
  124. def _real_extract(self, url):
  125. # Extract id and simplified title from URL
  126. video_id, display_id = re.match(self._VALID_URL, url).groups()
  127. # the video may come from an external site
  128. m_external = re.match(r'^(\w{2})-(.*)$', video_id)
  129. if m_external is not None:
  130. prefix, ext_id = m_external.groups()
  131. # Check if video comes from YouTube
  132. if prefix == 'yt':
  133. return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube')
  134. # CBS videos use theplatform.com
  135. if prefix == 'cb':
  136. return self.url_result('theplatform:%s' % ext_id, 'ThePlatform')
  137. headers = {
  138. # Disable family filter
  139. 'Cookie': 'user=%s; ' % compat_urllib_parse_urlencode({'ffilter': False})
  140. }
  141. # AnyClip videos require the flashversion cookie so that we get the link
  142. # to the mp4 file
  143. if video_id.startswith('an-'):
  144. headers['Cookie'] += 'flashVersion=0; '
  145. # Retrieve video webpage to extract further information
  146. webpage = self._download_webpage(url, video_id, headers=headers)
  147. error = get_element_by_attribute(
  148. 'class', 'notfound-page-title', webpage)
  149. if error:
  150. raise ExtractorError(error, expected=True)
  151. video_title = self._html_search_meta(
  152. ['og:title', 'twitter:title'], webpage, 'title', default=None) or self._search_regex(r'<h1>(.*?)</h1>', webpage, 'title')
  153. # Extract URL, uploader and title from webpage
  154. self.report_extraction(video_id)
  155. video_url = None
  156. mobj = re.search(r'(?m)&(?:media|video)URL=([^&]+)', webpage)
  157. if mobj is not None:
  158. mediaURL = compat_urllib_parse_unquote(mobj.group(1))
  159. video_ext = determine_ext(mediaURL)
  160. # Extract gdaKey if available
  161. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  162. if mobj is None:
  163. video_url = mediaURL
  164. else:
  165. gdaKey = mobj.group(1)
  166. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  167. if video_url is None:
  168. mobj = re.search(r'<video src="([^"]+)"', webpage)
  169. if mobj:
  170. video_url = mobj.group(1)
  171. video_ext = 'mp4'
  172. if video_url is None:
  173. flashvars = self._search_regex(
  174. r' name="flashvars" value="(.*?)"', webpage, 'flashvars',
  175. default=None)
  176. if flashvars:
  177. vardict = compat_parse_qs(flashvars)
  178. if 'mediaData' not in vardict:
  179. raise ExtractorError('Unable to extract media URL')
  180. mobj = re.search(
  181. r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  182. if mobj is None:
  183. raise ExtractorError('Unable to extract media URL')
  184. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  185. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  186. video_ext = determine_ext(video_url)
  187. if video_url is None:
  188. player_url = self._search_regex(
  189. r"swfobject\.embedSWF\('([^']+)'",
  190. webpage, 'config URL', default=None)
  191. if player_url:
  192. config_url = self._search_regex(
  193. r'config=(.+)$', player_url, 'config URL')
  194. config_doc = self._download_xml(
  195. config_url, video_id,
  196. note='Downloading video config')
  197. smil_url = config_doc.find('.//properties').attrib['smil_file']
  198. smil_doc = self._download_xml(
  199. smil_url, video_id,
  200. note='Downloading SMIL document')
  201. base_url = smil_doc.find('./head/meta').attrib['base']
  202. video_url = []
  203. for vn in smil_doc.findall('.//video'):
  204. br = int(vn.attrib['system-bitrate'])
  205. play_path = vn.attrib['src']
  206. video_url.append({
  207. 'format_id': 'smil-%d' % br,
  208. 'url': base_url,
  209. 'play_path': play_path,
  210. 'page_url': url,
  211. 'player_url': player_url,
  212. 'ext': play_path.partition(':')[0],
  213. })
  214. if video_url is None:
  215. flashvars = self._parse_json(self._search_regex(
  216. r'flashvars\s*=\s*({.*});', webpage, 'flashvars',
  217. default=None), video_id, fatal=False)
  218. if flashvars:
  219. video_url = []
  220. for source in flashvars.get('sources'):
  221. source_url = source.get('src')
  222. if not source_url:
  223. continue
  224. ext = mimetype2ext(source.get('type')) or determine_ext(source_url)
  225. if ext == 'm3u8':
  226. video_url.extend(self._extract_m3u8_formats(
  227. source_url, video_id, 'mp4',
  228. 'm3u8_native', m3u8_id='hls', fatal=False))
  229. else:
  230. video_url.append({
  231. 'url': source_url,
  232. 'ext': ext,
  233. })
  234. if video_url is None:
  235. raise ExtractorError('Unsupported video type')
  236. description = self._html_search_meta(
  237. ['og:description', 'twitter:description', 'description'],
  238. webpage, 'title', fatal=False)
  239. thumbnail = self._html_search_meta(
  240. ['og:image', 'twitter:image'], webpage, 'title', fatal=False)
  241. video_uploader = self._html_search_regex(
  242. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  243. webpage, 'uploader nickname', fatal=False)
  244. duration = int_or_none(
  245. self._html_search_meta('video:duration', webpage, default=None))
  246. age_limit = (
  247. 18
  248. if re.search(r'(?:"contentRating":|"rating",)"restricted"', webpage)
  249. else 0)
  250. if isinstance(video_url, list):
  251. formats = video_url
  252. else:
  253. formats = [{
  254. 'url': video_url,
  255. 'ext': video_ext,
  256. }]
  257. self._sort_formats(formats)
  258. return {
  259. 'id': video_id,
  260. 'display_id': display_id,
  261. 'description': description,
  262. 'uploader': video_uploader,
  263. 'title': video_title,
  264. 'thumbnail': thumbnail,
  265. 'age_limit': age_limit,
  266. 'formats': formats,
  267. 'duration': duration,
  268. }