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.

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