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.

189 lines
7.3 KiB

  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. compat_parse_qs,
  5. compat_urllib_parse,
  6. compat_urllib_request,
  7. compat_str,
  8. determine_ext,
  9. ExtractorError,
  10. )
  11. class MetacafeIE(InfoExtractor):
  12. """Information Extractor for metacafe.com."""
  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 = u'metacafe'
  17. _TESTS = [
  18. # Youtube video
  19. {
  20. u"add_ie": ["Youtube"],
  21. u"url": u"http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/",
  22. u"file": u"_aUehQsCQtM.mp4",
  23. u"info_dict": {
  24. u"upload_date": u"20090102",
  25. u"title": u"The Electric Company | \"Short I\" | PBS KIDS GO!",
  26. u"description": u"md5:2439a8ef6d5a70e380c22f5ad323e5a8",
  27. u"uploader": u"PBS",
  28. u"uploader_id": u"PBS"
  29. }
  30. },
  31. # Normal metacafe video
  32. {
  33. u'url': u'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  34. u'md5': u'6e0bca200eaad2552e6915ed6fd4d9ad',
  35. u'info_dict': {
  36. u'id': u'11121940',
  37. u'ext': u'mp4',
  38. u'title': u'News: Stuff You Won\'t Do with Your PlayStation 4',
  39. u'uploader': u'ign',
  40. u'description': u'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  41. },
  42. },
  43. # AnyClip video
  44. {
  45. u"url": u"http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/",
  46. u"file": u"an-dVVXnuY7Jh77J.mp4",
  47. u"info_dict": {
  48. u"title": u"The Andromeda Strain (1971): Stop the Bomb Part 3",
  49. u"uploader": u"anyclip",
  50. u"description": u"md5:38c711dd98f5bb87acf973d573442e67",
  51. },
  52. },
  53. # age-restricted video
  54. {
  55. u'url': u'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  56. u'md5': u'98dde7c1a35d02178e8ab7560fe8bd09',
  57. u'info_dict': {
  58. u'id': u'5186653',
  59. u'ext': u'mp4',
  60. u'title': u'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  61. u'uploader': u'Dwayne Pipe',
  62. u'description': u'md5:950bf4c581e2c059911fa3ffbe377e4b',
  63. u'age_limit': 18,
  64. },
  65. },
  66. # cbs video
  67. {
  68. u'url': u'http://www.metacafe.com/watch/cb-0rOxMBabDXN6/samsung_galaxy_note_2_samsungs_next_generation_phablet/',
  69. u'info_dict': {
  70. u'id': u'0rOxMBabDXN6',
  71. u'ext': u'flv',
  72. u'title': u'Samsung Galaxy Note 2: Samsung\'s next-generation phablet',
  73. u'description': u'md5:54d49fac53d26d5a0aaeccd061ada09d',
  74. u'duration': 129,
  75. },
  76. u'params': {
  77. # rtmp download
  78. u'skip_download': True,
  79. },
  80. },
  81. ]
  82. def report_disclaimer(self):
  83. """Report disclaimer retrieval."""
  84. self.to_screen(u'Retrieving disclaimer')
  85. def _real_initialize(self):
  86. # Retrieve disclaimer
  87. self.report_disclaimer()
  88. self._download_webpage(self._DISCLAIMER, None, False, u'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, u'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(u'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(u'Unable to extract media URL')
  144. vardict = compat_parse_qs(mobj.group(1))
  145. if 'mediaData' not in vardict:
  146. raise ExtractorError(u'Unable to extract media URL')
  147. mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  148. if mobj is None:
  149. raise ExtractorError(u'Unable to extract media URL')
  150. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  151. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  152. video_ext = determine_ext(video_url)
  153. video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
  154. description = self._og_search_description(webpage)
  155. video_uploader = self._html_search_regex(
  156. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  157. webpage, u'uploader nickname', fatal=False)
  158. if re.search(r'"contentRating":"restricted"', webpage) is not None:
  159. age_limit = 18
  160. else:
  161. age_limit = 0
  162. return {
  163. '_type': 'video',
  164. 'id': video_id,
  165. 'url': video_url,
  166. 'description': description,
  167. 'uploader': video_uploader,
  168. 'upload_date': None,
  169. 'title': video_title,
  170. 'ext': video_ext,
  171. 'age_limit': age_limit,
  172. }