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.

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