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.

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