logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

metacafe.py (11719B)


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