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