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.

135 lines
5.3 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. u"add_ie": ["Youtube"],
  22. u"url": u"http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/",
  23. u"file": u"_aUehQsCQtM.flv",
  24. u"info_dict": {
  25. u"upload_date": u"20090102",
  26. u"title": u"The Electric Company | \"Short I\" | PBS KIDS GO!",
  27. u"description": u"md5:2439a8ef6d5a70e380c22f5ad323e5a8",
  28. u"uploader": u"PBS",
  29. u"uploader_id": u"PBS"
  30. }
  31. },
  32. {
  33. u"url": u"http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/",
  34. u"file": u"an-dVVXnuY7Jh77J.mp4",
  35. u"info_dict": {
  36. u"title": u"The Andromeda Strain (1971): Stop the Bomb Part 3",
  37. u"uploader": u"AnyClip",
  38. }
  39. }]
  40. def report_disclaimer(self):
  41. """Report disclaimer retrieval."""
  42. self.to_screen(u'Retrieving disclaimer')
  43. def _real_initialize(self):
  44. # Retrieve disclaimer
  45. request = compat_urllib_request.Request(self._DISCLAIMER)
  46. try:
  47. self.report_disclaimer()
  48. compat_urllib_request.urlopen(request).read()
  49. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  50. raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
  51. # Confirm age
  52. disclaimer_form = {
  53. 'filters': '0',
  54. 'submit': "Continue - I'm over 18",
  55. }
  56. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  57. try:
  58. self.report_age_confirmation()
  59. compat_urllib_request.urlopen(request).read()
  60. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  61. raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
  62. def _real_extract(self, url):
  63. # Extract id and simplified title from URL
  64. mobj = re.match(self._VALID_URL, url)
  65. if mobj is None:
  66. raise ExtractorError(u'Invalid URL: %s' % url)
  67. video_id = mobj.group(1)
  68. # Check if video comes from YouTube
  69. mobj2 = re.match(r'^yt-(.*)$', video_id)
  70. if mobj2 is not None:
  71. return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
  72. # Retrieve video webpage to extract further information
  73. req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
  74. req.headers['Cookie'] = 'flashVersion=0;'
  75. webpage = self._download_webpage(req, video_id)
  76. # Extract URL, uploader and title from webpage
  77. self.report_extraction(video_id)
  78. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  79. if mobj is not None:
  80. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  81. video_ext = mediaURL[-3:]
  82. # Extract gdaKey if available
  83. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  84. if mobj is None:
  85. video_url = mediaURL
  86. else:
  87. gdaKey = mobj.group(1)
  88. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  89. else:
  90. mobj = re.search(r'<video src="([^"]+)"', webpage)
  91. if mobj:
  92. video_url = mobj.group(1)
  93. video_ext = 'mp4'
  94. else:
  95. mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
  96. if mobj is None:
  97. raise ExtractorError(u'Unable to extract media URL')
  98. vardict = compat_parse_qs(mobj.group(1))
  99. if 'mediaData' not in vardict:
  100. raise ExtractorError(u'Unable to extract media URL')
  101. mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  102. if mobj is None:
  103. raise ExtractorError(u'Unable to extract media URL')
  104. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  105. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  106. video_ext = determine_ext(video_url)
  107. mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
  108. if mobj is None:
  109. raise ExtractorError(u'Unable to extract title')
  110. video_title = mobj.group(1).decode('utf-8')
  111. video_uploader = self._html_search_regex(r'submitter=(.*?);|<p class="By">\s*By\s*<a[^>]*>(.*?)</a>', webpage, u'uploader nickname', fatal=False)
  112. return [{
  113. 'id': video_id,
  114. 'url': video_url,
  115. 'uploader': video_uploader,
  116. 'upload_date': None,
  117. 'title': video_title,
  118. 'ext': video_ext,
  119. }]