metacafe.py 4.8 KB

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