metacafe.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import json
  2. import re
  3. import urllib.parse
  4. from .common import InfoExtractor
  5. from ..compat import compat_parse_qs, compat_urllib_parse_unquote
  6. from ..utils import (
  7. ExtractorError,
  8. determine_ext,
  9. get_element_by_attribute,
  10. int_or_none,
  11. mimetype2ext,
  12. )
  13. class MetacafeIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?metacafe\.com/watch/(?P<id>[^/]+)/(?P<display_id>[^/?#]+)'
  15. _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
  16. _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
  17. IE_NAME = 'metacafe'
  18. _TESTS = [
  19. # Youtube video
  20. {
  21. 'add_ie': ['Youtube'],
  22. 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/',
  23. 'info_dict': {
  24. 'id': '_aUehQsCQtM',
  25. 'ext': 'mp4',
  26. 'upload_date': '20090102',
  27. 'title': 'The Electric Company | "Short I" | PBS KIDS GO!',
  28. 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8',
  29. 'uploader': 'PBS',
  30. 'uploader_id': 'PBS'
  31. }
  32. },
  33. # Normal metacafe video
  34. {
  35. 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/',
  36. 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad',
  37. 'info_dict': {
  38. 'id': '11121940',
  39. 'ext': 'mp4',
  40. 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4',
  41. 'uploader': 'ign',
  42. 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.',
  43. },
  44. 'skip': 'Page is temporarily unavailable.',
  45. },
  46. # metacafe video with family filter
  47. {
  48. 'url': 'http://www.metacafe.com/watch/2155630/adult_art_by_david_hart_156/',
  49. 'md5': 'b06082c5079bbdcde677a6291fbdf376',
  50. 'info_dict': {
  51. 'id': '2155630',
  52. 'ext': 'mp4',
  53. 'title': 'Adult Art By David Hart 156',
  54. 'uploader': '63346',
  55. 'description': 'md5:9afac8fc885252201ad14563694040fc',
  56. },
  57. 'params': {
  58. 'skip_download': True,
  59. },
  60. },
  61. # AnyClip video
  62. {
  63. 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/',
  64. 'info_dict': {
  65. 'id': 'an-dVVXnuY7Jh77J',
  66. 'ext': 'mp4',
  67. 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3',
  68. 'uploader': 'AnyClip',
  69. 'description': 'md5:cbef0460d31e3807f6feb4e7a5952e5b',
  70. },
  71. },
  72. # age-restricted video
  73. {
  74. 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/',
  75. 'md5': '98dde7c1a35d02178e8ab7560fe8bd09',
  76. 'info_dict': {
  77. 'id': '5186653',
  78. 'ext': 'mp4',
  79. 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.',
  80. 'uploader': 'Dwayne Pipe',
  81. 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b',
  82. 'age_limit': 18,
  83. },
  84. },
  85. # cbs video
  86. {
  87. 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/',
  88. 'info_dict': {
  89. 'id': '8VD4r_Zws8VP',
  90. 'ext': 'flv',
  91. 'title': 'Open: This is Face the Nation, February 9',
  92. 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476',
  93. 'duration': 96,
  94. 'uploader': 'CBSI-NEW',
  95. 'upload_date': '20140209',
  96. 'timestamp': 1391959800,
  97. },
  98. 'params': {
  99. # rtmp download
  100. 'skip_download': True,
  101. },
  102. },
  103. # Movieclips.com video
  104. {
  105. 'url': 'http://www.metacafe.com/watch/mv-Wy7ZU/my_week_with_marilyn_do_you_love_me/',
  106. 'info_dict': {
  107. 'id': 'mv-Wy7ZU',
  108. 'ext': 'mp4',
  109. 'title': 'My Week with Marilyn - Do You Love Me?',
  110. 'description': 'From the movie My Week with Marilyn - Colin (Eddie Redmayne) professes his love to Marilyn (Michelle Williams) and gets her to promise to return to set and finish the movie.',
  111. 'uploader': 'movie_trailers',
  112. 'duration': 176,
  113. },
  114. 'params': {
  115. 'skip_download': 'requires rtmpdump',
  116. }
  117. }
  118. ]
  119. def report_disclaimer(self):
  120. self.to_screen('Retrieving disclaimer')
  121. def _real_extract(self, url):
  122. # Extract id and simplified title from URL
  123. video_id, display_id = self._match_valid_url(url).groups()
  124. # the video may come from an external site
  125. m_external = re.match(r'^(\w{2})-(.*)$', video_id)
  126. if m_external is not None:
  127. prefix, ext_id = m_external.groups()
  128. # Check if video comes from YouTube
  129. if prefix == 'yt':
  130. return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube')
  131. # CBS videos use theplatform.com
  132. if prefix == 'cb':
  133. return self.url_result('theplatform:%s' % ext_id, 'ThePlatform')
  134. headers = {
  135. # Disable family filter
  136. 'Cookie': 'user=%s; ' % urllib.parse.quote(json.dumps({'ffilter': False}))
  137. }
  138. # AnyClip videos require the flashversion cookie so that we get the link
  139. # to the mp4 file
  140. if video_id.startswith('an-'):
  141. headers['Cookie'] += 'flashVersion=0; '
  142. # Retrieve video webpage to extract further information
  143. webpage = self._download_webpage(url, video_id, headers=headers)
  144. error = get_element_by_attribute(
  145. 'class', 'notfound-page-title', webpage)
  146. if error:
  147. raise ExtractorError(error, expected=True)
  148. video_title = self._html_search_meta(
  149. ['og:title', 'twitter:title'], webpage, 'title', default=None) or self._search_regex(r'<h1>(.*?)</h1>', webpage, 'title')
  150. # Extract URL, uploader and title from webpage
  151. self.report_extraction(video_id)
  152. video_url = None
  153. mobj = re.search(r'(?m)&(?:media|video)URL=([^&]+)', webpage)
  154. if mobj is not None:
  155. mediaURL = compat_urllib_parse_unquote(mobj.group(1))
  156. video_ext = determine_ext(mediaURL)
  157. # Extract gdaKey if available
  158. mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
  159. if mobj is None:
  160. video_url = mediaURL
  161. else:
  162. gdaKey = mobj.group(1)
  163. video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
  164. if video_url is None:
  165. mobj = re.search(r'<video src="([^"]+)"', webpage)
  166. if mobj:
  167. video_url = mobj.group(1)
  168. video_ext = 'mp4'
  169. if video_url is None:
  170. flashvars = self._search_regex(
  171. r' name="flashvars" value="(.*?)"', webpage, 'flashvars',
  172. default=None)
  173. if flashvars:
  174. vardict = compat_parse_qs(flashvars)
  175. if 'mediaData' not in vardict:
  176. raise ExtractorError('Unable to extract media URL')
  177. mobj = re.search(
  178. r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
  179. if mobj is None:
  180. raise ExtractorError('Unable to extract media URL')
  181. mediaURL = mobj.group('mediaURL').replace('\\/', '/')
  182. video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
  183. video_ext = determine_ext(video_url)
  184. if video_url is None:
  185. player_url = self._search_regex(
  186. r"swfobject\.embedSWF\('([^']+)'",
  187. webpage, 'config URL', default=None)
  188. if player_url:
  189. config_url = self._search_regex(
  190. r'config=(.+)$', player_url, 'config URL')
  191. config_doc = self._download_xml(
  192. config_url, video_id,
  193. note='Downloading video config')
  194. smil_url = config_doc.find('.//properties').attrib['smil_file']
  195. smil_doc = self._download_xml(
  196. smil_url, video_id,
  197. note='Downloading SMIL document')
  198. base_url = smil_doc.find('./head/meta').attrib['base']
  199. video_url = []
  200. for vn in smil_doc.findall('.//video'):
  201. br = int(vn.attrib['system-bitrate'])
  202. play_path = vn.attrib['src']
  203. video_url.append({
  204. 'format_id': 'smil-%d' % br,
  205. 'url': base_url,
  206. 'play_path': play_path,
  207. 'page_url': url,
  208. 'player_url': player_url,
  209. 'ext': play_path.partition(':')[0],
  210. })
  211. if video_url is None:
  212. flashvars = self._parse_json(self._search_regex(
  213. r'flashvars\s*=\s*({.*});', webpage, 'flashvars',
  214. default=None), video_id, fatal=False)
  215. if flashvars:
  216. video_url = []
  217. for source in flashvars.get('sources'):
  218. source_url = source.get('src')
  219. if not source_url:
  220. continue
  221. ext = mimetype2ext(source.get('type')) or determine_ext(source_url)
  222. if ext == 'm3u8':
  223. video_url.extend(self._extract_m3u8_formats(
  224. source_url, video_id, 'mp4',
  225. 'm3u8_native', m3u8_id='hls', fatal=False))
  226. else:
  227. video_url.append({
  228. 'url': source_url,
  229. 'ext': ext,
  230. })
  231. if video_url is None:
  232. raise ExtractorError('Unsupported video type')
  233. description = self._html_search_meta(
  234. ['og:description', 'twitter:description', 'description'],
  235. webpage, 'title', fatal=False)
  236. thumbnail = self._html_search_meta(
  237. ['og:image', 'twitter:image'], webpage, 'title', fatal=False)
  238. video_uploader = self._html_search_regex(
  239. r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);',
  240. webpage, 'uploader nickname', fatal=False)
  241. duration = int_or_none(
  242. self._html_search_meta('video:duration', webpage, default=None))
  243. age_limit = (
  244. 18
  245. if re.search(r'(?:"contentRating":|"rating",)"restricted"', webpage)
  246. else 0)
  247. if isinstance(video_url, list):
  248. formats = video_url
  249. else:
  250. formats = [{
  251. 'url': video_url,
  252. 'ext': video_ext,
  253. }]
  254. return {
  255. 'id': video_id,
  256. 'display_id': display_id,
  257. 'description': description,
  258. 'uploader': video_uploader,
  259. 'title': video_title,
  260. 'thumbnail': thumbnail,
  261. 'age_limit': age_limit,
  262. 'formats': formats,
  263. 'duration': duration,
  264. }