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.

123 lines
4.8 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. ExtractorError,
  12. )
  13. class MetacafeIE(InfoExtractor):
  14. """Information Extractor for metacafe.com."""
  15. _VALID_URL = r'(?:http://)?(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
  16. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  17. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  18. IE_NAME = u'metacafe'
  19. _TEST = {
  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.flv",
  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. def report_disclaimer(self):
  32. """Report disclaimer retrieval."""
  33. self.to_screen(u'Retrieving disclaimer')
  34. def _real_initialize(self):
  35. # Retrieve disclaimer
  36. request = compat_urllib_request.Request(self._DISCLAIMER)
  37. try:
  38. self.report_disclaimer()
  39. compat_urllib_request.urlopen(request).read()
  40. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  41. raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))
  42. # Confirm age
  43. disclaimer_form = {
  44. 'filters': '0',
  45. 'submit': "Continue - I'm over 18",
  46. }
  47. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  48. try:
  49. self.report_age_confirmation()
  50. compat_urllib_request.urlopen(request).read()
  51. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  52. raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
  53. def _real_extract(self, url):
  54. # Extract id and simplified title from URL
  55. mobj = re.match(self._VALID_URL, url)
  56. if mobj is None:
  57. raise ExtractorError(u'Invalid URL: %s' % url)
  58. video_id = mobj.group(1)
  59. # Check if video comes from YouTube
  60. mobj2 = re.match(r'^yt-(.*)$', video_id)
  61. if mobj2 is not None:
  62. return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]
  63. # Retrieve video webpage to extract further information
  64. webpage = self._download_webpage('http://www.metacafe.com/watch/%s/' % video_id, video_id)
  65. # Extract URL, uploader and title from webpage
  66. self.report_extraction(video_id)
  67. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  68. if mobj is not None:
  69. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  70. video_extension = mediaURL[-3:]
  71. # Extract gdaKey if available
  72. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  73. if mobj is None:
  74. video_url = mediaURL
  75. else:
  76. gdaKey = mobj.group(1)
  77. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  78. else:
  79. mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
  80. if mobj is None:
  81. raise ExtractorError(u'Unable to extract media URL')
  82. vardict = compat_parse_qs(mobj.group(1))
  83. if 'mediaData' not in vardict:
  84. raise ExtractorError(u'Unable to extract media URL')
  85. mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  86. if mobj is None:
  87. raise ExtractorError(u'Unable to extract media URL')
  88. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  89. video_extension = mediaURL[-3:]
  90. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  91. mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
  92. if mobj is None:
  93. raise ExtractorError(u'Unable to extract title')
  94. video_title = mobj.group(1).decode('utf-8')
  95. mobj = re.search(r'submitter=(.*?);', webpage)
  96. if mobj is None:
  97. raise ExtractorError(u'Unable to extract uploader nickname')
  98. video_uploader = mobj.group(1)
  99. return [{
  100. 'id': video_id.decode('utf-8'),
  101. 'url': video_url.decode('utf-8'),
  102. 'uploader': video_uploader.decode('utf-8'),
  103. 'upload_date': None,
  104. 'title': video_title,
  105. 'ext': video_extension.decode('utf-8'),
  106. }]