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.

250 lines
9.9 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_parse_qs,
  6. compat_urllib_parse,
  7. compat_urllib_request,
  8. determine_ext,
  9. ExtractorError,
  10. int_or_none,
  11. )
  12. class MetacafeIE(InfoExtractor):
  13. _VALID_URL = r'http://(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
  14. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  15. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  16. IE_NAME = 'metacafe'
  17. _TESTS = [
  18. # Youtube video
  19. {
  20. 'add_ie': ['Youtube'],
  21. 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/',
  22. 'info_dict': {
  23. 'id': '_aUehQsCQtM',
  24. 'ext': 'mp4',
  25. 'upload_date': '20090102',
  26. 'title': 'The Electric Company | "Short I" | PBS KIDS GO!',
  27. 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8',
  28. 'uploader': 'PBS',
  29. 'uploader_id': 'PBS'
  30. }
  31. },
  32. # Normal metacafe video
  33. {
  34. 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  35. 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad',
  36. 'info_dict': {
  37. 'id': '11121940',
  38. 'ext': 'mp4',
  39. 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4',
  40. 'uploader': 'ign',
  41. 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  42. },
  43. },
  44. # AnyClip video
  45. {
  46. 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/',
  47. 'info_dict': {
  48. 'id': 'an-dVVXnuY7Jh77J',
  49. 'ext': 'mp4',
  50. 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3',
  51. 'uploader': 'anyclip',
  52. 'description': 'md5:38c711dd98f5bb87acf973d573442e67',
  53. },
  54. },
  55. # age-restricted video
  56. {
  57. 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  58. 'md5': '98dde7c1a35d02178e8ab7560fe8bd09',
  59. 'info_dict': {
  60. 'id': '5186653',
  61. 'ext': 'mp4',
  62. 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  63. 'uploader': 'Dwayne Pipe',
  64. 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b',
  65. 'age_limit': 18,
  66. },
  67. },
  68. # cbs video
  69. {
  70. 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/',
  71. 'info_dict': {
  72. 'id': '8VD4r_Zws8VP',
  73. 'ext': 'flv',
  74. 'title': 'Open: This is Face the Nation, February 9',
  75. 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476',
  76. 'duration': 96,
  77. },
  78. 'params': {
  79. # rtmp download
  80. 'skip_download': True,
  81. },
  82. },
  83. # Movieclips.com video
  84. {
  85. 'url': 'http://www.metacafe.com/watch/mv-Wy7ZU/my_week_with_marilyn_do_you_love_me/',
  86. 'info_dict': {
  87. 'id': 'mv-Wy7ZU',
  88. 'ext': 'mp4',
  89. 'title': 'My Week with Marilyn - Do You Love Me?',
  90. '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.',
  91. 'uploader': 'movie_trailers',
  92. 'duration': 176,
  93. },
  94. 'params': {
  95. 'skip_download': 'requires rtmpdump',
  96. }
  97. }
  98. ]
  99. def report_disclaimer(self):
  100. self.to_screen('Retrieving disclaimer')
  101. def _real_initialize(self):
  102. # Retrieve disclaimer
  103. self.report_disclaimer()
  104. self._download_webpage(self._DISCLAIMER, None, False, 'Unable to retrieve disclaimer')
  105. # Confirm age
  106. disclaimer_form = {
  107. 'filters': '0',
  108. 'submit': "Continue - I'm over 18",
  109. }
  110. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  111. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  112. self.report_age_confirmation()
  113. self._download_webpage(request, None, False, 'Unable to confirm age')
  114. def _real_extract(self, url):
  115. # Extract id and simplified title from URL
  116. mobj = re.match(self._VALID_URL, url)
  117. if mobj is None:
  118. raise ExtractorError('Invalid URL: %s' % url)
  119. video_id = mobj.group(1)
  120. # the video may come from an external site
  121. m_external = re.match('^(\w{2})-(.*)$', video_id)
  122. if m_external is not None:
  123. prefix, ext_id = m_external.groups()
  124. # Check if video comes from YouTube
  125. if prefix == 'yt':
  126. return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube')
  127. # CBS videos use theplatform.com
  128. if prefix == 'cb':
  129. return self.url_result('theplatform:%s' % ext_id, 'ThePlatform')
  130. # Retrieve video webpage to extract further information
  131. req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
  132. # AnyClip videos require the flashversion cookie so that we get the link
  133. # to the mp4 file
  134. mobj_an = re.match(r'^an-(.*?)$', video_id)
  135. if mobj_an:
  136. req.headers['Cookie'] = 'flashVersion=0;'
  137. webpage = self._download_webpage(req, video_id)
  138. # Extract URL, uploader and title from webpage
  139. self.report_extraction(video_id)
  140. video_url = None
  141. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  142. if mobj is not None:
  143. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  144. video_ext = mediaURL[-3:]
  145. # Extract gdaKey if available
  146. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  147. if mobj is None:
  148. video_url = mediaURL
  149. else:
  150. gdaKey = mobj.group(1)
  151. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  152. if video_url is None:
  153. mobj = re.search(r'<video src="([^"]+)"', webpage)
  154. if mobj:
  155. video_url = mobj.group(1)
  156. video_ext = 'mp4'
  157. if video_url is None:
  158. flashvars = self._search_regex(
  159. r' name="flashvars" value="(.*?)"', webpage, 'flashvars',
  160. default=None)
  161. if flashvars:
  162. vardict = compat_parse_qs(flashvars)
  163. if 'mediaData' not in vardict:
  164. raise ExtractorError('Unable to extract media URL')
  165. mobj = re.search(
  166. r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  167. if mobj is None:
  168. raise ExtractorError('Unable to extract media URL')
  169. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  170. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  171. video_ext = determine_ext(video_url)
  172. if video_url is None:
  173. player_url = self._search_regex(
  174. r"swfobject\.embedSWF\('([^']+)'",
  175. webpage, 'config URL', default=None)
  176. if player_url:
  177. config_url = self._search_regex(
  178. r'config=(.+)$', player_url, 'config URL')
  179. config_doc = self._download_xml(
  180. config_url, video_id,
  181. note='Downloading video config')
  182. smil_url = config_doc.find('.//properties').attrib['smil_file']
  183. smil_doc = self._download_xml(
  184. smil_url, video_id,
  185. note='Downloading SMIL document')
  186. base_url = smil_doc.find('./head/meta').attrib['base']
  187. video_url = []
  188. for vn in smil_doc.findall('.//video'):
  189. br = int(vn.attrib['system-bitrate'])
  190. play_path = vn.attrib['src']
  191. video_url.append({
  192. 'format_id': 'smil-%d' % br,
  193. 'url': base_url,
  194. 'play_path': play_path,
  195. 'page_url': url,
  196. 'player_url': player_url,
  197. 'ext': play_path.partition(':')[0],
  198. })
  199. if video_url is None:
  200. raise ExtractorError('Unsupported video type')
  201. video_title = self._html_search_regex(
  202. r'(?im)<title>(.*) - Video</title>', webpage, 'title')
  203. description = self._og_search_description(webpage)
  204. thumbnail = self._og_search_thumbnail(webpage)
  205. video_uploader = self._html_search_regex(
  206. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  207. webpage, 'uploader nickname', fatal=False)
  208. duration = int_or_none(
  209. self._html_search_meta('video:duration', webpage))
  210. age_limit = (
  211. 18
  212. if re.search(r'"contentRating":"restricted"', webpage)
  213. else 0)
  214. if isinstance(video_url, list):
  215. formats = video_url
  216. else:
  217. formats = [{
  218. 'url': video_url,
  219. 'ext': video_ext,
  220. }]
  221. self._sort_formats(formats)
  222. return {
  223. 'id': video_id,
  224. 'description': description,
  225. 'uploader': video_uploader,
  226. 'title': video_title,
  227. 'thumbnail': thumbnail,
  228. 'age_limit': age_limit,
  229. 'formats': formats,
  230. 'duration': duration,
  231. }