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.

137 lines
5.5 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.mp4",
  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. u"description": u"md5:38c711dd98f5bb87acf973d573442e67"
  39. }
  40. }]
  41. def report_disclaimer(self):
  42. """Report disclaimer retrieval."""
  43. self.to_screen(u'Retrieving disclaimer')
  44. def _real_initialize(self):
  45. # Retrieve disclaimer
  46. request = compat_urllib_request.Request(self._DISCLAIMER)
  47. try:
  48. self.report_disclaimer()
  49. compat_urllib_request.urlopen(request).read()
  50. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  51. raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
  52. # Confirm age
  53. disclaimer_form = {
  54. 'filters': '0',
  55. 'submit': "Continue - I'm over 18",
  56. }
  57. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  58. try:
  59. self.report_age_confirmation()
  60. compat_urllib_request.urlopen(request).read()
  61. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  62. raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
  63. def _real_extract(self, url):
  64. # Extract id and simplified title from URL
  65. mobj = re.match(self._VALID_URL, url)
  66. if mobj is None:
  67. raise ExtractorError(u'Invalid URL: %s' % url)
  68. video_id = mobj.group(1)
  69. # Check if video comes from YouTube
  70. mobj2 = re.match(r'^yt-(.*)$', video_id)
  71. if mobj2 is not None:
  72. return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
  73. # Retrieve video webpage to extract further information
  74. req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
  75. req.headers['Cookie'] = 'flashVersion=0;'
  76. webpage = self._download_webpage(req, video_id)
  77. # Extract URL, uploader and title from webpage
  78. self.report_extraction(video_id)
  79. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  80. if mobj is not None:
  81. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  82. video_ext = mediaURL[-3:]
  83. # Extract gdaKey if available
  84. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  85. if mobj is None:
  86. video_url = mediaURL
  87. else:
  88. gdaKey = mobj.group(1)
  89. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  90. else:
  91. mobj = re.search(r'<video src="([^"]+)"', webpage)
  92. if mobj:
  93. video_url = mobj.group(1)
  94. video_ext = 'mp4'
  95. else:
  96. mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
  97. if mobj is None:
  98. raise ExtractorError(u'Unable to extract media URL')
  99. vardict = compat_parse_qs(mobj.group(1))
  100. if 'mediaData' not in vardict:
  101. raise ExtractorError(u'Unable to extract media URL')
  102. mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  103. if mobj is None:
  104. raise ExtractorError(u'Unable to extract media URL')
  105. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  106. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  107. video_ext = determine_ext(video_url)
  108. video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
  109. description = self._og_search_description(webpage)
  110. video_uploader = self._html_search_regex(
  111. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  112. webpage, u'uploader nickname', fatal=False)
  113. return {
  114. '_type': 'video',
  115. 'id': video_id,
  116. 'url': video_url,
  117. 'description': description,
  118. 'uploader': video_uploader,
  119. 'upload_date': None,
  120. 'title': video_title,
  121. 'ext': video_ext,
  122. }