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.

178 lines
7.0 KiB

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