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.

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