metacafe.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_parse_qs,
  6. compat_urllib_parse,
  7. compat_urllib_request,
  8. determine_ext,
  9. ExtractorError,
  10. )
  11. class MetacafeIE(InfoExtractor):
  12. _VALID_URL = r'http://(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
  13. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  14. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  15. IE_NAME = 'metacafe'
  16. _TESTS = [
  17. # Youtube video
  18. {
  19. 'add_ie': ['Youtube'],
  20. 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/',
  21. 'info_dict': {
  22. 'id': '_aUehQsCQtM',
  23. 'ext': 'mp4',
  24. 'upload_date': '20090102',
  25. 'title': 'The Electric Company | "Short I" | PBS KIDS GO!',
  26. 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8',
  27. 'uploader': 'PBS',
  28. 'uploader_id': 'PBS'
  29. }
  30. },
  31. # Normal metacafe video
  32. {
  33. 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  34. 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad',
  35. 'info_dict': {
  36. 'id': '11121940',
  37. 'ext': 'mp4',
  38. 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4',
  39. 'uploader': 'ign',
  40. 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  41. },
  42. },
  43. # AnyClip video
  44. {
  45. 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/',
  46. 'info_dict': {
  47. 'id': 'an-dVVXnuY7Jh77J',
  48. 'ext': 'mp4',
  49. 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3',
  50. 'uploader': 'anyclip',
  51. 'description': 'md5:38c711dd98f5bb87acf973d573442e67',
  52. },
  53. },
  54. # age-restricted video
  55. {
  56. 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  57. 'md5': '98dde7c1a35d02178e8ab7560fe8bd09',
  58. 'info_dict': {
  59. 'id': '5186653',
  60. 'ext': 'mp4',
  61. 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  62. 'uploader': 'Dwayne Pipe',
  63. 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b',
  64. 'age_limit': 18,
  65. },
  66. },
  67. # cbs video
  68. {
  69. 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/',
  70. 'info_dict': {
  71. 'id': '8VD4r_Zws8VP',
  72. 'ext': 'flv',
  73. 'title': 'Open: This is Face the Nation, February 9',
  74. 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476',
  75. 'duration': 96,
  76. },
  77. 'params': {
  78. # rtmp download
  79. 'skip_download': True,
  80. },
  81. },
  82. ]
  83. def report_disclaimer(self):
  84. self.to_screen('Retrieving disclaimer')
  85. def _real_initialize(self):
  86. # Retrieve disclaimer
  87. self.report_disclaimer()
  88. self._download_webpage(self._DISCLAIMER, None, False, 'Unable to retrieve disclaimer')
  89. # Confirm age
  90. disclaimer_form = {
  91. 'filters': '0',
  92. 'submit': "Continue - I'm over 18",
  93. }
  94. request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
  95. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  96. self.report_age_confirmation()
  97. self._download_webpage(request, None, False, 'Unable to confirm age')
  98. """Report disclaimer retrieval."""
  99. def _real_extract(self, url):
  100. # Extract id and simplified title from URL
  101. mobj = re.match(self._VALID_URL, url)
  102. if mobj is None:
  103. raise ExtractorError('Invalid URL: %s' % url)
  104. video_id = mobj.group(1)
  105. # the video may come from an external site
  106. m_external = re.match('^(\w{2})-(.*)$', video_id)
  107. if m_external is not None:
  108. prefix, ext_id = m_external.groups()
  109. # Check if video comes from YouTube
  110. if prefix == 'yt':
  111. return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube')
  112. # CBS videos use theplatform.com
  113. if prefix == 'cb':
  114. return self.url_result('theplatform:%s' % ext_id, 'ThePlatform')
  115. # Retrieve video webpage to extract further information
  116. req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
  117. # AnyClip videos require the flashversion cookie so that we get the link
  118. # to the mp4 file
  119. mobj_an = re.match(r'^an-(.*?)$', video_id)
  120. if mobj_an:
  121. req.headers['Cookie'] = 'flashVersion=0;'
  122. webpage = self._download_webpage(req, video_id)
  123. # Extract URL, uploader and title from webpage
  124. self.report_extraction(video_id)
  125. mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
  126. if mobj is not None:
  127. mediaURL = compat_urllib_parse.unquote(mobj.group(1))
  128. video_ext = mediaURL[-3:]
  129. # Extract gdaKey if available
  130. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  131. if mobj is None:
  132. video_url = mediaURL
  133. else:
  134. gdaKey = mobj.group(1)
  135. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  136. else:
  137. mobj = re.search(r'<video src="([^"]+)"', webpage)
  138. if mobj:
  139. video_url = mobj.group(1)
  140. video_ext = 'mp4'
  141. else:
  142. mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
  143. if mobj is None:
  144. raise ExtractorError('Unable to extract media URL')
  145. vardict = compat_parse_qs(mobj.group(1))
  146. if 'mediaData' not in vardict:
  147. raise ExtractorError('Unable to extract media URL')
  148. mobj = re.search(
  149. r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  150. if mobj is None:
  151. raise ExtractorError('Unable to extract media URL')
  152. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  153. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  154. video_ext = determine_ext(video_url)
  155. video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, 'title')
  156. description = self._og_search_description(webpage)
  157. thumbnail = self._og_search_thumbnail(webpage)
  158. video_uploader = self._html_search_regex(
  159. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  160. webpage, 'uploader nickname', fatal=False)
  161. if re.search(r'"contentRating":"restricted"', webpage) is not None:
  162. age_limit = 18
  163. else:
  164. age_limit = 0
  165. return {
  166. 'id': video_id,
  167. 'url': video_url,
  168. 'description': description,
  169. 'uploader': video_uploader,
  170. 'title': video_title,
  171. 'thumbnail':thumbnail,
  172. 'ext': video_ext,
  173. 'age_limit': age_limit,
  174. }