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.

688 lines
26 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. import copy
  9. from test.helper import FakeYDL, assertRegexpMatches
  10. from youtube_dl import YoutubeDL
  11. from youtube_dl.compat import compat_str, compat_urllib_error
  12. from youtube_dl.extractor import YoutubeIE
  13. from youtube_dl.extractor.common import InfoExtractor
  14. from youtube_dl.postprocessor.common import PostProcessor
  15. from youtube_dl.utils import ExtractorError, match_filter_func
  16. TEST_URL = 'http://localhost/sample.mp4'
  17. class YDL(FakeYDL):
  18. def __init__(self, *args, **kwargs):
  19. super(YDL, self).__init__(*args, **kwargs)
  20. self.downloaded_info_dicts = []
  21. self.msgs = []
  22. def process_info(self, info_dict):
  23. self.downloaded_info_dicts.append(info_dict)
  24. def to_screen(self, msg):
  25. self.msgs.append(msg)
  26. def _make_result(formats, **kwargs):
  27. res = {
  28. 'formats': formats,
  29. 'id': 'testid',
  30. 'title': 'testttitle',
  31. 'extractor': 'testex',
  32. }
  33. res.update(**kwargs)
  34. return res
  35. class TestFormatSelection(unittest.TestCase):
  36. def test_prefer_free_formats(self):
  37. # Same resolution => download webm
  38. ydl = YDL()
  39. ydl.params['prefer_free_formats'] = True
  40. formats = [
  41. {'ext': 'webm', 'height': 460, 'url': TEST_URL},
  42. {'ext': 'mp4', 'height': 460, 'url': TEST_URL},
  43. ]
  44. info_dict = _make_result(formats)
  45. yie = YoutubeIE(ydl)
  46. yie._sort_formats(info_dict['formats'])
  47. ydl.process_ie_result(info_dict)
  48. downloaded = ydl.downloaded_info_dicts[0]
  49. self.assertEqual(downloaded['ext'], 'webm')
  50. # Different resolution => download best quality (mp4)
  51. ydl = YDL()
  52. ydl.params['prefer_free_formats'] = True
  53. formats = [
  54. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  55. {'ext': 'mp4', 'height': 1080, 'url': TEST_URL},
  56. ]
  57. info_dict['formats'] = formats
  58. yie = YoutubeIE(ydl)
  59. yie._sort_formats(info_dict['formats'])
  60. ydl.process_ie_result(info_dict)
  61. downloaded = ydl.downloaded_info_dicts[0]
  62. self.assertEqual(downloaded['ext'], 'mp4')
  63. # No prefer_free_formats => prefer mp4 and flv for greater compatibility
  64. ydl = YDL()
  65. ydl.params['prefer_free_formats'] = False
  66. formats = [
  67. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  68. {'ext': 'mp4', 'height': 720, 'url': TEST_URL},
  69. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  70. ]
  71. info_dict['formats'] = formats
  72. yie = YoutubeIE(ydl)
  73. yie._sort_formats(info_dict['formats'])
  74. ydl.process_ie_result(info_dict)
  75. downloaded = ydl.downloaded_info_dicts[0]
  76. self.assertEqual(downloaded['ext'], 'mp4')
  77. ydl = YDL()
  78. ydl.params['prefer_free_formats'] = False
  79. formats = [
  80. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  81. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  82. ]
  83. info_dict['formats'] = formats
  84. yie = YoutubeIE(ydl)
  85. yie._sort_formats(info_dict['formats'])
  86. ydl.process_ie_result(info_dict)
  87. downloaded = ydl.downloaded_info_dicts[0]
  88. self.assertEqual(downloaded['ext'], 'flv')
  89. def test_format_selection(self):
  90. formats = [
  91. {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  92. {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL},
  93. {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL},
  94. {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL},
  95. {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL},
  96. ]
  97. info_dict = _make_result(formats)
  98. ydl = YDL({'format': '20/47'})
  99. ydl.process_ie_result(info_dict.copy())
  100. downloaded = ydl.downloaded_info_dicts[0]
  101. self.assertEqual(downloaded['format_id'], '47')
  102. ydl = YDL({'format': '20/71/worst'})
  103. ydl.process_ie_result(info_dict.copy())
  104. downloaded = ydl.downloaded_info_dicts[0]
  105. self.assertEqual(downloaded['format_id'], '35')
  106. ydl = YDL()
  107. ydl.process_ie_result(info_dict.copy())
  108. downloaded = ydl.downloaded_info_dicts[0]
  109. self.assertEqual(downloaded['format_id'], '2')
  110. ydl = YDL({'format': 'webm/mp4'})
  111. ydl.process_ie_result(info_dict.copy())
  112. downloaded = ydl.downloaded_info_dicts[0]
  113. self.assertEqual(downloaded['format_id'], '47')
  114. ydl = YDL({'format': '3gp/40/mp4'})
  115. ydl.process_ie_result(info_dict.copy())
  116. downloaded = ydl.downloaded_info_dicts[0]
  117. self.assertEqual(downloaded['format_id'], '35')
  118. ydl = YDL({'format': 'example-with-dashes'})
  119. ydl.process_ie_result(info_dict.copy())
  120. downloaded = ydl.downloaded_info_dicts[0]
  121. self.assertEqual(downloaded['format_id'], 'example-with-dashes')
  122. def test_format_selection_audio(self):
  123. formats = [
  124. {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
  125. {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
  126. {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL},
  127. {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL},
  128. ]
  129. info_dict = _make_result(formats)
  130. ydl = YDL({'format': 'bestaudio'})
  131. ydl.process_ie_result(info_dict.copy())
  132. downloaded = ydl.downloaded_info_dicts[0]
  133. self.assertEqual(downloaded['format_id'], 'audio-high')
  134. ydl = YDL({'format': 'worstaudio'})
  135. ydl.process_ie_result(info_dict.copy())
  136. downloaded = ydl.downloaded_info_dicts[0]
  137. self.assertEqual(downloaded['format_id'], 'audio-low')
  138. formats = [
  139. {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  140. {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL},
  141. ]
  142. info_dict = _make_result(formats)
  143. ydl = YDL({'format': 'bestaudio/worstaudio/best'})
  144. ydl.process_ie_result(info_dict.copy())
  145. downloaded = ydl.downloaded_info_dicts[0]
  146. self.assertEqual(downloaded['format_id'], 'vid-high')
  147. def test_format_selection_audio_exts(self):
  148. formats = [
  149. {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  150. {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  151. {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  152. {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  153. {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  154. ]
  155. info_dict = _make_result(formats)
  156. ydl = YDL({'format': 'best'})
  157. ie = YoutubeIE(ydl)
  158. ie._sort_formats(info_dict['formats'])
  159. ydl.process_ie_result(copy.deepcopy(info_dict))
  160. downloaded = ydl.downloaded_info_dicts[0]
  161. self.assertEqual(downloaded['format_id'], 'aac-64')
  162. ydl = YDL({'format': 'mp3'})
  163. ie = YoutubeIE(ydl)
  164. ie._sort_formats(info_dict['formats'])
  165. ydl.process_ie_result(copy.deepcopy(info_dict))
  166. downloaded = ydl.downloaded_info_dicts[0]
  167. self.assertEqual(downloaded['format_id'], 'mp3-64')
  168. ydl = YDL({'prefer_free_formats': True})
  169. ie = YoutubeIE(ydl)
  170. ie._sort_formats(info_dict['formats'])
  171. ydl.process_ie_result(copy.deepcopy(info_dict))
  172. downloaded = ydl.downloaded_info_dicts[0]
  173. self.assertEqual(downloaded['format_id'], 'ogg-64')
  174. def test_format_selection_video(self):
  175. formats = [
  176. {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL},
  177. {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL},
  178. {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL},
  179. ]
  180. info_dict = _make_result(formats)
  181. ydl = YDL({'format': 'bestvideo'})
  182. ydl.process_ie_result(info_dict.copy())
  183. downloaded = ydl.downloaded_info_dicts[0]
  184. self.assertEqual(downloaded['format_id'], 'dash-video-high')
  185. ydl = YDL({'format': 'worstvideo'})
  186. ydl.process_ie_result(info_dict.copy())
  187. downloaded = ydl.downloaded_info_dicts[0]
  188. self.assertEqual(downloaded['format_id'], 'dash-video-low')
  189. formats = [
  190. {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL},
  191. ]
  192. info_dict = _make_result(formats)
  193. ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'})
  194. ydl.process_ie_result(info_dict.copy())
  195. downloaded = ydl.downloaded_info_dicts[0]
  196. self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot')
  197. def test_youtube_format_selection(self):
  198. order = [
  199. '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
  200. # Apple HTTP Live Streaming
  201. '96', '95', '94', '93', '92', '132', '151',
  202. # 3D
  203. '85', '84', '102', '83', '101', '82', '100',
  204. # Dash video
  205. '137', '248', '136', '247', '135', '246',
  206. '245', '244', '134', '243', '133', '242', '160',
  207. # Dash audio
  208. '141', '172', '140', '171', '139',
  209. ]
  210. def format_info(f_id):
  211. info = YoutubeIE._formats[f_id].copy()
  212. info['format_id'] = f_id
  213. info['url'] = 'url:' + f_id
  214. return info
  215. formats_order = [format_info(f_id) for f_id in order]
  216. info_dict = _make_result(list(formats_order), extractor='youtube')
  217. ydl = YDL({'format': 'bestvideo+bestaudio'})
  218. yie = YoutubeIE(ydl)
  219. yie._sort_formats(info_dict['formats'])
  220. ydl.process_ie_result(info_dict)
  221. downloaded = ydl.downloaded_info_dicts[0]
  222. self.assertEqual(downloaded['format_id'], '137+141')
  223. self.assertEqual(downloaded['ext'], 'mp4')
  224. info_dict = _make_result(list(formats_order), extractor='youtube')
  225. ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'})
  226. yie = YoutubeIE(ydl)
  227. yie._sort_formats(info_dict['formats'])
  228. ydl.process_ie_result(info_dict)
  229. downloaded = ydl.downloaded_info_dicts[0]
  230. self.assertEqual(downloaded['format_id'], '38')
  231. info_dict = _make_result(list(formats_order), extractor='youtube')
  232. ydl = YDL({'format': 'bestvideo/best,bestaudio'})
  233. yie = YoutubeIE(ydl)
  234. yie._sort_formats(info_dict['formats'])
  235. ydl.process_ie_result(info_dict)
  236. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  237. self.assertEqual(downloaded_ids, ['137', '141'])
  238. info_dict = _make_result(list(formats_order), extractor='youtube')
  239. ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'})
  240. yie = YoutubeIE(ydl)
  241. yie._sort_formats(info_dict['formats'])
  242. ydl.process_ie_result(info_dict)
  243. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  244. self.assertEqual(downloaded_ids, ['137+141', '248+141'])
  245. info_dict = _make_result(list(formats_order), extractor='youtube')
  246. ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'})
  247. yie = YoutubeIE(ydl)
  248. yie._sort_formats(info_dict['formats'])
  249. ydl.process_ie_result(info_dict)
  250. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  251. self.assertEqual(downloaded_ids, ['136+141', '247+141'])
  252. info_dict = _make_result(list(formats_order), extractor='youtube')
  253. ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'})
  254. yie = YoutubeIE(ydl)
  255. yie._sort_formats(info_dict['formats'])
  256. ydl.process_ie_result(info_dict)
  257. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  258. self.assertEqual(downloaded_ids, ['248+141'])
  259. for f1, f2 in zip(formats_order, formats_order[1:]):
  260. info_dict = _make_result([f1, f2], extractor='youtube')
  261. ydl = YDL({'format': 'best/bestvideo'})
  262. yie = YoutubeIE(ydl)
  263. yie._sort_formats(info_dict['formats'])
  264. ydl.process_ie_result(info_dict)
  265. downloaded = ydl.downloaded_info_dicts[0]
  266. self.assertEqual(downloaded['format_id'], f1['format_id'])
  267. info_dict = _make_result([f2, f1], extractor='youtube')
  268. ydl = YDL({'format': 'best/bestvideo'})
  269. yie = YoutubeIE(ydl)
  270. yie._sort_formats(info_dict['formats'])
  271. ydl.process_ie_result(info_dict)
  272. downloaded = ydl.downloaded_info_dicts[0]
  273. self.assertEqual(downloaded['format_id'], f1['format_id'])
  274. def test_invalid_format_specs(self):
  275. def assert_syntax_error(format_spec):
  276. ydl = YDL({'format': format_spec})
  277. info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}])
  278. self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict)
  279. assert_syntax_error('bestvideo,,best')
  280. assert_syntax_error('+bestaudio')
  281. assert_syntax_error('bestvideo+')
  282. assert_syntax_error('/')
  283. def test_format_filtering(self):
  284. formats = [
  285. {'format_id': 'A', 'filesize': 500, 'width': 1000},
  286. {'format_id': 'B', 'filesize': 1000, 'width': 500},
  287. {'format_id': 'C', 'filesize': 1000, 'width': 400},
  288. {'format_id': 'D', 'filesize': 2000, 'width': 600},
  289. {'format_id': 'E', 'filesize': 3000},
  290. {'format_id': 'F'},
  291. {'format_id': 'G', 'filesize': 1000000},
  292. ]
  293. for f in formats:
  294. f['url'] = 'http://_/'
  295. f['ext'] = 'unknown'
  296. info_dict = _make_result(formats)
  297. ydl = YDL({'format': 'best[filesize<3000]'})
  298. ydl.process_ie_result(info_dict)
  299. downloaded = ydl.downloaded_info_dicts[0]
  300. self.assertEqual(downloaded['format_id'], 'D')
  301. ydl = YDL({'format': 'best[filesize<=3000]'})
  302. ydl.process_ie_result(info_dict)
  303. downloaded = ydl.downloaded_info_dicts[0]
  304. self.assertEqual(downloaded['format_id'], 'E')
  305. ydl = YDL({'format': 'best[filesize <= ? 3000]'})
  306. ydl.process_ie_result(info_dict)
  307. downloaded = ydl.downloaded_info_dicts[0]
  308. self.assertEqual(downloaded['format_id'], 'F')
  309. ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
  310. ydl.process_ie_result(info_dict)
  311. downloaded = ydl.downloaded_info_dicts[0]
  312. self.assertEqual(downloaded['format_id'], 'B')
  313. ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
  314. ydl.process_ie_result(info_dict)
  315. downloaded = ydl.downloaded_info_dicts[0]
  316. self.assertEqual(downloaded['format_id'], 'C')
  317. ydl = YDL({'format': '[filesize>?1]'})
  318. ydl.process_ie_result(info_dict)
  319. downloaded = ydl.downloaded_info_dicts[0]
  320. self.assertEqual(downloaded['format_id'], 'G')
  321. ydl = YDL({'format': '[filesize<1M]'})
  322. ydl.process_ie_result(info_dict)
  323. downloaded = ydl.downloaded_info_dicts[0]
  324. self.assertEqual(downloaded['format_id'], 'E')
  325. ydl = YDL({'format': '[filesize<1MiB]'})
  326. ydl.process_ie_result(info_dict)
  327. downloaded = ydl.downloaded_info_dicts[0]
  328. self.assertEqual(downloaded['format_id'], 'G')
  329. ydl = YDL({'format': 'all[width>=400][width<=600]'})
  330. ydl.process_ie_result(info_dict)
  331. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  332. self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
  333. ydl = YDL({'format': 'best[height<40]'})
  334. try:
  335. ydl.process_ie_result(info_dict)
  336. except ExtractorError:
  337. pass
  338. self.assertEqual(ydl.downloaded_info_dicts, [])
  339. class TestYoutubeDL(unittest.TestCase):
  340. def test_subtitles(self):
  341. def s_formats(lang, autocaption=False):
  342. return [{
  343. 'ext': ext,
  344. 'url': 'http://localhost/video.%s.%s' % (lang, ext),
  345. '_auto': autocaption,
  346. } for ext in ['vtt', 'srt', 'ass']]
  347. subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
  348. auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
  349. info_dict = {
  350. 'id': 'test',
  351. 'title': 'Test',
  352. 'url': 'http://localhost/video.mp4',
  353. 'subtitles': subtitles,
  354. 'automatic_captions': auto_captions,
  355. 'extractor': 'TEST',
  356. }
  357. def get_info(params={}):
  358. params.setdefault('simulate', True)
  359. ydl = YDL(params)
  360. ydl.report_warning = lambda *args, **kargs: None
  361. return ydl.process_video_result(info_dict, download=False)
  362. result = get_info()
  363. self.assertFalse(result.get('requested_subtitles'))
  364. self.assertEqual(result['subtitles'], subtitles)
  365. self.assertEqual(result['automatic_captions'], auto_captions)
  366. result = get_info({'writesubtitles': True})
  367. subs = result['requested_subtitles']
  368. self.assertTrue(subs)
  369. self.assertEqual(set(subs.keys()), set(['en']))
  370. self.assertTrue(subs['en'].get('data') is None)
  371. self.assertEqual(subs['en']['ext'], 'ass')
  372. result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
  373. subs = result['requested_subtitles']
  374. self.assertEqual(subs['en']['ext'], 'srt')
  375. result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
  376. subs = result['requested_subtitles']
  377. self.assertTrue(subs)
  378. self.assertEqual(set(subs.keys()), set(['es', 'fr']))
  379. result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  380. subs = result['requested_subtitles']
  381. self.assertTrue(subs)
  382. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  383. self.assertFalse(subs['es']['_auto'])
  384. self.assertTrue(subs['pt']['_auto'])
  385. result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  386. subs = result['requested_subtitles']
  387. self.assertTrue(subs)
  388. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  389. self.assertTrue(subs['es']['_auto'])
  390. self.assertTrue(subs['pt']['_auto'])
  391. def test_add_extra_info(self):
  392. test_dict = {
  393. 'extractor': 'Foo',
  394. }
  395. extra_info = {
  396. 'extractor': 'Bar',
  397. 'playlist': 'funny videos',
  398. }
  399. YDL.add_extra_info(test_dict, extra_info)
  400. self.assertEqual(test_dict['extractor'], 'Foo')
  401. self.assertEqual(test_dict['playlist'], 'funny videos')
  402. def test_prepare_filename(self):
  403. info = {
  404. 'id': '1234',
  405. 'ext': 'mp4',
  406. 'width': None,
  407. }
  408. def fname(templ):
  409. ydl = YoutubeDL({'outtmpl': templ})
  410. return ydl.prepare_filename(info)
  411. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  412. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  413. # Replace missing fields with 'NA'
  414. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  415. def test_format_note(self):
  416. ydl = YoutubeDL()
  417. self.assertEqual(ydl._format_note({}), '')
  418. assertRegexpMatches(self, ydl._format_note({
  419. 'vbr': 10,
  420. }), '^\s*10k$')
  421. def test_postprocessors(self):
  422. filename = 'post-processor-testfile.mp4'
  423. audiofile = filename + '.mp3'
  424. class SimplePP(PostProcessor):
  425. def run(self, info):
  426. with open(audiofile, 'wt') as f:
  427. f.write('EXAMPLE')
  428. return [info['filepath']], info
  429. def run_pp(params, PP):
  430. with open(filename, 'wt') as f:
  431. f.write('EXAMPLE')
  432. ydl = YoutubeDL(params)
  433. ydl.add_post_processor(PP())
  434. ydl.post_process(filename, {'filepath': filename})
  435. run_pp({'keepvideo': True}, SimplePP)
  436. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  437. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  438. os.unlink(filename)
  439. os.unlink(audiofile)
  440. run_pp({'keepvideo': False}, SimplePP)
  441. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  442. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  443. os.unlink(audiofile)
  444. class ModifierPP(PostProcessor):
  445. def run(self, info):
  446. with open(info['filepath'], 'wt') as f:
  447. f.write('MODIFIED')
  448. return [], info
  449. run_pp({'keepvideo': False}, ModifierPP)
  450. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  451. os.unlink(filename)
  452. def test_match_filter(self):
  453. class FilterYDL(YDL):
  454. def __init__(self, *args, **kwargs):
  455. super(FilterYDL, self).__init__(*args, **kwargs)
  456. self.params['simulate'] = True
  457. def process_info(self, info_dict):
  458. super(YDL, self).process_info(info_dict)
  459. def _match_entry(self, info_dict, incomplete):
  460. res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
  461. if res is None:
  462. self.downloaded_info_dicts.append(info_dict)
  463. return res
  464. first = {
  465. 'id': '1',
  466. 'url': TEST_URL,
  467. 'title': 'one',
  468. 'extractor': 'TEST',
  469. 'duration': 30,
  470. 'filesize': 10 * 1024,
  471. }
  472. second = {
  473. 'id': '2',
  474. 'url': TEST_URL,
  475. 'title': 'two',
  476. 'extractor': 'TEST',
  477. 'duration': 10,
  478. 'description': 'foo',
  479. 'filesize': 5 * 1024,
  480. }
  481. videos = [first, second]
  482. def get_videos(filter_=None):
  483. ydl = FilterYDL({'match_filter': filter_})
  484. for v in videos:
  485. ydl.process_ie_result(v, download=True)
  486. return [v['id'] for v in ydl.downloaded_info_dicts]
  487. res = get_videos()
  488. self.assertEqual(res, ['1', '2'])
  489. def f(v):
  490. if v['id'] == '1':
  491. return None
  492. else:
  493. return 'Video id is not 1'
  494. res = get_videos(f)
  495. self.assertEqual(res, ['1'])
  496. f = match_filter_func('duration < 30')
  497. res = get_videos(f)
  498. self.assertEqual(res, ['2'])
  499. f = match_filter_func('description = foo')
  500. res = get_videos(f)
  501. self.assertEqual(res, ['2'])
  502. f = match_filter_func('description =? foo')
  503. res = get_videos(f)
  504. self.assertEqual(res, ['1', '2'])
  505. f = match_filter_func('filesize > 5KiB')
  506. res = get_videos(f)
  507. self.assertEqual(res, ['1'])
  508. def test_playlist_items_selection(self):
  509. entries = [{
  510. 'id': compat_str(i),
  511. 'title': compat_str(i),
  512. 'url': TEST_URL,
  513. } for i in range(1, 5)]
  514. playlist = {
  515. '_type': 'playlist',
  516. 'id': 'test',
  517. 'entries': entries,
  518. 'extractor': 'test:playlist',
  519. 'extractor_key': 'test:playlist',
  520. 'webpage_url': 'http://example.com',
  521. }
  522. def get_ids(params):
  523. ydl = YDL(params)
  524. # make a copy because the dictionary can be modified
  525. ydl.process_ie_result(playlist.copy())
  526. return [int(v['id']) for v in ydl.downloaded_info_dicts]
  527. result = get_ids({})
  528. self.assertEqual(result, [1, 2, 3, 4])
  529. result = get_ids({'playlistend': 10})
  530. self.assertEqual(result, [1, 2, 3, 4])
  531. result = get_ids({'playlistend': 2})
  532. self.assertEqual(result, [1, 2])
  533. result = get_ids({'playliststart': 10})
  534. self.assertEqual(result, [])
  535. result = get_ids({'playliststart': 2})
  536. self.assertEqual(result, [2, 3, 4])
  537. result = get_ids({'playlist_items': '2-4'})
  538. self.assertEqual(result, [2, 3, 4])
  539. result = get_ids({'playlist_items': '2,4'})
  540. self.assertEqual(result, [2, 4])
  541. result = get_ids({'playlist_items': '10'})
  542. self.assertEqual(result, [])
  543. def test_urlopen_no_file_protocol(self):
  544. # see https://github.com/rg3/youtube-dl/issues/8227
  545. ydl = YDL()
  546. self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')
  547. def test_do_not_override_ie_key_in_url_transparent(self):
  548. ydl = YDL()
  549. class Foo1IE(InfoExtractor):
  550. _VALID_URL = r'foo1:'
  551. def _real_extract(self, url):
  552. return {
  553. '_type': 'url_transparent',
  554. 'url': 'foo2:',
  555. 'ie_key': 'Foo2',
  556. }
  557. class Foo2IE(InfoExtractor):
  558. _VALID_URL = r'foo2:'
  559. def _real_extract(self, url):
  560. return {
  561. '_type': 'url',
  562. 'url': 'foo3:',
  563. 'ie_key': 'Foo3',
  564. }
  565. class Foo3IE(InfoExtractor):
  566. _VALID_URL = r'foo3:'
  567. def _real_extract(self, url):
  568. return _make_result([{'url': TEST_URL}])
  569. ydl.add_info_extractor(Foo1IE(ydl))
  570. ydl.add_info_extractor(Foo2IE(ydl))
  571. ydl.add_info_extractor(Foo3IE(ydl))
  572. ydl.extract_info('foo1:')
  573. downloaded = ydl.downloaded_info_dicts[0]
  574. self.assertEqual(downloaded['url'], TEST_URL)
  575. if __name__ == '__main__':
  576. unittest.main()