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.

190 lines
7.5 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. )
  11. class MetacafeIE(InfoExtractor):
  12. _VALID_URL = r'http://(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
  13. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  14. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  15. IE_NAME = 'metacafe'
  16. _TESTS = [
  17. # Youtube video
  18. {
  19. 'add_ie': ['Youtube'],
  20. 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/',
  21. 'info_dict': {
  22. 'id': '_aUehQsCQtM',
  23. 'ext': 'mp4',
  24. 'upload_date': '20090102',
  25. 'title': 'The Electric Company | "Short I" | PBS KIDS GO!',
  26. 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8',
  27. 'uploader': 'PBS',
  28. 'uploader_id': 'PBS'
  29. }
  30. },
  31. # Normal metacafe video
  32. {
  33. 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  34. 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad',
  35. 'info_dict': {
  36. 'id': '11121940',
  37. 'ext': 'mp4',
  38. 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4',
  39. 'uploader': 'ign',
  40. 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  41. },
  42. },
  43. # AnyClip video
  44. {
  45. 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/',
  46. 'info_dict': {
  47. 'id': 'an-dVVXnuY7Jh77J',
  48. 'ext': 'mp4',
  49. 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3',
  50. 'uploader': 'anyclip',
  51. 'description': 'md5:38c711dd98f5bb87acf973d573442e67',
  52. },
  53. },
  54. # age-restricted video
  55. {
  56. 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  57. 'md5': '98dde7c1a35d02178e8ab7560fe8bd09',
  58. 'info_dict': {
  59. 'id': '5186653',
  60. 'ext': 'mp4',
  61. 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  62. 'uploader': 'Dwayne Pipe',
  63. 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b',
  64. 'age_limit': 18,
  65. },
  66. },
  67. # cbs video
  68. {
  69. 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/',
  70. 'info_dict': {
  71. 'id': '8VD4r_Zws8VP',
  72. 'ext': 'flv',
  73. 'title': 'Open: This is Face the Nation, February 9',
  74. 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476',
  75. 'duration': 96,
  76. },
  77. 'params': {
  78. # rtmp download
  79. 'skip_download': True,
  80. },
  81. },
  82. ]
  83. def report_disclaimer(self):
  84. self.to_screen('Retrieving disclaimer')
  85. def _real_initialize(self):
  86. # Retrieve disclaimer
  87. self.report_disclaimer()
  88. self._download_webpage(self._DISCLAIMER, None, False, 'Unable to retrieve disclaimer')
  89. # Confirm age
  90. disclaimer_form = {
  91. 'filters': '0',
  92. 'submit': "Continue - I'm over 18",
  93. }
  94. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  95. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  96. self.report_age_confirmation()
  97. self._download_webpage(request, None, False, 'Unable to confirm age')
  98. def _real_extract(self, url):
  99. # Extract id and simplified title from URL
  100. mobj = re.match(self._VALID_URL, url)
  101. if mobj is None:
  102. raise ExtractorError('Invalid URL: %s' % url)
  103. video_id = mobj.group(1)
  104. # the video may come from an external site
  105. m_external = re.match('^(\w{2})-(.*)$', video_id)
  106. if m_external is not None:
  107. prefix, ext_id = m_external.groups()
  108. # Check if video comes from YouTube
  109. if prefix == 'yt':
  110. return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube')
  111. # CBS videos use theplatform.com
  112. if prefix == 'cb':
  113. return self.url_result('theplatform:%s' % ext_id, 'ThePlatform')
  114. # Retrieve video webpage to extract further information
  115. req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
  116. # AnyClip videos require the flashversion cookie so that we get the link
  117. # to the mp4 file
  118. mobj_an = re.match(r'^an-(.*?)$', video_id)
  119. if mobj_an:
  120. req.headers['Cookie'] = 'flashVersion=0;'
  121. webpage = self._download_webpage(req, video_id)
  122. # Extract URL, uploader and title from webpage
  123. self.report_extraction(video_id)
  124. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  125. if mobj is not None:
  126. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  127. video_ext = mediaURL[-3:]
  128. # Extract gdaKey if available
  129. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  130. if mobj is None:
  131. video_url = mediaURL
  132. else:
  133. gdaKey = mobj.group(1)
  134. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  135. else:
  136. mobj = re.search(r'<video src="([^"]+)"', webpage)
  137. if mobj:
  138. video_url = mobj.group(1)
  139. video_ext = 'mp4'
  140. else:
  141. mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
  142. if mobj is None:
  143. raise ExtractorError('Unable to extract media URL')
  144. vardict = compat_parse_qs(mobj.group(1))
  145. if 'mediaData' not in vardict:
  146. raise ExtractorError('Unable to extract media URL')
  147. mobj = re.search(
  148. r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  149. if mobj is None:
  150. raise ExtractorError('Unable to extract media URL')
  151. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  152. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  153. video_ext = determine_ext(video_url)
  154. video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, 'title')
  155. description = self._og_search_description(webpage)
  156. thumbnail = self._og_search_thumbnail(webpage)
  157. video_uploader = self._html_search_regex(
  158. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  159. webpage, 'uploader nickname', fatal=False)
  160. if re.search(r'"contentRating":"restricted"', webpage) is not None:
  161. age_limit = 18
  162. else:
  163. age_limit = 0
  164. return {
  165. 'id': video_id,
  166. 'url': video_url,
  167. 'description': description,
  168. 'uploader': video_uploader,
  169. 'title': video_title,
  170. 'thumbnail':thumbnail,
  171. 'ext': video_ext,
  172. 'age_limit': age_limit,
  173. }