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.

596 lines
22 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 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[ext=mp4],bestvideo[ext=webm])+bestaudio'})
  212. yie = YoutubeIE(ydl)
  213. yie._sort_formats(info_dict['formats'])
  214. ydl.process_ie_result(info_dict)
  215. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  216. self.assertEqual(downloaded_ids, ['137+141', '248+141'])
  217. info_dict = _make_result(list(formats_order), extractor='youtube')
  218. ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+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, ['136+141', '247+141'])
  224. info_dict = _make_result(list(formats_order), extractor='youtube')
  225. ydl = YDL({'format': '(bestvideo[ext=none]/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, ['248+141'])
  231. for f1, f2 in zip(formats_order, formats_order[1:]):
  232. info_dict = _make_result([f1, f2], extractor='youtube')
  233. ydl = YDL({'format': 'best/bestvideo'})
  234. yie = YoutubeIE(ydl)
  235. yie._sort_formats(info_dict['formats'])
  236. ydl.process_ie_result(info_dict)
  237. downloaded = ydl.downloaded_info_dicts[0]
  238. self.assertEqual(downloaded['format_id'], f1['format_id'])
  239. info_dict = _make_result([f2, f1], extractor='youtube')
  240. ydl = YDL({'format': 'best/bestvideo'})
  241. yie = YoutubeIE(ydl)
  242. yie._sort_formats(info_dict['formats'])
  243. ydl.process_ie_result(info_dict)
  244. downloaded = ydl.downloaded_info_dicts[0]
  245. self.assertEqual(downloaded['format_id'], f1['format_id'])
  246. def test_format_filtering(self):
  247. formats = [
  248. {'format_id': 'A', 'filesize': 500, 'width': 1000},
  249. {'format_id': 'B', 'filesize': 1000, 'width': 500},
  250. {'format_id': 'C', 'filesize': 1000, 'width': 400},
  251. {'format_id': 'D', 'filesize': 2000, 'width': 600},
  252. {'format_id': 'E', 'filesize': 3000},
  253. {'format_id': 'F'},
  254. {'format_id': 'G', 'filesize': 1000000},
  255. ]
  256. for f in formats:
  257. f['url'] = 'http://_/'
  258. f['ext'] = 'unknown'
  259. info_dict = _make_result(formats)
  260. ydl = YDL({'format': 'best[filesize<3000]'})
  261. ydl.process_ie_result(info_dict)
  262. downloaded = ydl.downloaded_info_dicts[0]
  263. self.assertEqual(downloaded['format_id'], 'D')
  264. ydl = YDL({'format': 'best[filesize<=3000]'})
  265. ydl.process_ie_result(info_dict)
  266. downloaded = ydl.downloaded_info_dicts[0]
  267. self.assertEqual(downloaded['format_id'], 'E')
  268. ydl = YDL({'format': 'best[filesize <= ? 3000]'})
  269. ydl.process_ie_result(info_dict)
  270. downloaded = ydl.downloaded_info_dicts[0]
  271. self.assertEqual(downloaded['format_id'], 'F')
  272. ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
  273. ydl.process_ie_result(info_dict)
  274. downloaded = ydl.downloaded_info_dicts[0]
  275. self.assertEqual(downloaded['format_id'], 'B')
  276. ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
  277. ydl.process_ie_result(info_dict)
  278. downloaded = ydl.downloaded_info_dicts[0]
  279. self.assertEqual(downloaded['format_id'], 'C')
  280. ydl = YDL({'format': '[filesize>?1]'})
  281. ydl.process_ie_result(info_dict)
  282. downloaded = ydl.downloaded_info_dicts[0]
  283. self.assertEqual(downloaded['format_id'], 'G')
  284. ydl = YDL({'format': '[filesize<1M]'})
  285. ydl.process_ie_result(info_dict)
  286. downloaded = ydl.downloaded_info_dicts[0]
  287. self.assertEqual(downloaded['format_id'], 'E')
  288. ydl = YDL({'format': '[filesize<1MiB]'})
  289. ydl.process_ie_result(info_dict)
  290. downloaded = ydl.downloaded_info_dicts[0]
  291. self.assertEqual(downloaded['format_id'], 'G')
  292. ydl = YDL({'format': 'all[width>=400][width<=600]'})
  293. ydl.process_ie_result(info_dict)
  294. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  295. self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
  296. class TestYoutubeDL(unittest.TestCase):
  297. def test_subtitles(self):
  298. def s_formats(lang, autocaption=False):
  299. return [{
  300. 'ext': ext,
  301. 'url': 'http://localhost/video.%s.%s' % (lang, ext),
  302. '_auto': autocaption,
  303. } for ext in ['vtt', 'srt', 'ass']]
  304. subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
  305. auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
  306. info_dict = {
  307. 'id': 'test',
  308. 'title': 'Test',
  309. 'url': 'http://localhost/video.mp4',
  310. 'subtitles': subtitles,
  311. 'automatic_captions': auto_captions,
  312. 'extractor': 'TEST',
  313. }
  314. def get_info(params={}):
  315. params.setdefault('simulate', True)
  316. ydl = YDL(params)
  317. ydl.report_warning = lambda *args, **kargs: None
  318. return ydl.process_video_result(info_dict, download=False)
  319. result = get_info()
  320. self.assertFalse(result.get('requested_subtitles'))
  321. self.assertEqual(result['subtitles'], subtitles)
  322. self.assertEqual(result['automatic_captions'], auto_captions)
  323. result = get_info({'writesubtitles': True})
  324. subs = result['requested_subtitles']
  325. self.assertTrue(subs)
  326. self.assertEqual(set(subs.keys()), set(['en']))
  327. self.assertTrue(subs['en'].get('data') is None)
  328. self.assertEqual(subs['en']['ext'], 'ass')
  329. result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
  330. subs = result['requested_subtitles']
  331. self.assertEqual(subs['en']['ext'], 'srt')
  332. result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
  333. subs = result['requested_subtitles']
  334. self.assertTrue(subs)
  335. self.assertEqual(set(subs.keys()), set(['es', 'fr']))
  336. result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  337. subs = result['requested_subtitles']
  338. self.assertTrue(subs)
  339. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  340. self.assertFalse(subs['es']['_auto'])
  341. self.assertTrue(subs['pt']['_auto'])
  342. result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  343. subs = result['requested_subtitles']
  344. self.assertTrue(subs)
  345. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  346. self.assertTrue(subs['es']['_auto'])
  347. self.assertTrue(subs['pt']['_auto'])
  348. def test_add_extra_info(self):
  349. test_dict = {
  350. 'extractor': 'Foo',
  351. }
  352. extra_info = {
  353. 'extractor': 'Bar',
  354. 'playlist': 'funny videos',
  355. }
  356. YDL.add_extra_info(test_dict, extra_info)
  357. self.assertEqual(test_dict['extractor'], 'Foo')
  358. self.assertEqual(test_dict['playlist'], 'funny videos')
  359. def test_prepare_filename(self):
  360. info = {
  361. 'id': '1234',
  362. 'ext': 'mp4',
  363. 'width': None,
  364. }
  365. def fname(templ):
  366. ydl = YoutubeDL({'outtmpl': templ})
  367. return ydl.prepare_filename(info)
  368. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  369. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  370. # Replace missing fields with 'NA'
  371. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  372. def test_format_note(self):
  373. ydl = YoutubeDL()
  374. self.assertEqual(ydl._format_note({}), '')
  375. assertRegexpMatches(self, ydl._format_note({
  376. 'vbr': 10,
  377. }), '^\s*10k$')
  378. def test_postprocessors(self):
  379. filename = 'post-processor-testfile.mp4'
  380. audiofile = filename + '.mp3'
  381. class SimplePP(PostProcessor):
  382. def run(self, info):
  383. with open(audiofile, 'wt') as f:
  384. f.write('EXAMPLE')
  385. return [info['filepath']], info
  386. def run_pp(params, PP):
  387. with open(filename, 'wt') as f:
  388. f.write('EXAMPLE')
  389. ydl = YoutubeDL(params)
  390. ydl.add_post_processor(PP())
  391. ydl.post_process(filename, {'filepath': filename})
  392. run_pp({'keepvideo': True}, SimplePP)
  393. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  394. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  395. os.unlink(filename)
  396. os.unlink(audiofile)
  397. run_pp({'keepvideo': False}, SimplePP)
  398. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  399. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  400. os.unlink(audiofile)
  401. class ModifierPP(PostProcessor):
  402. def run(self, info):
  403. with open(info['filepath'], 'wt') as f:
  404. f.write('MODIFIED')
  405. return [], info
  406. run_pp({'keepvideo': False}, ModifierPP)
  407. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  408. os.unlink(filename)
  409. def test_match_filter(self):
  410. class FilterYDL(YDL):
  411. def __init__(self, *args, **kwargs):
  412. super(FilterYDL, self).__init__(*args, **kwargs)
  413. self.params['simulate'] = True
  414. def process_info(self, info_dict):
  415. super(YDL, self).process_info(info_dict)
  416. def _match_entry(self, info_dict, incomplete):
  417. res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
  418. if res is None:
  419. self.downloaded_info_dicts.append(info_dict)
  420. return res
  421. first = {
  422. 'id': '1',
  423. 'url': TEST_URL,
  424. 'title': 'one',
  425. 'extractor': 'TEST',
  426. 'duration': 30,
  427. 'filesize': 10 * 1024,
  428. }
  429. second = {
  430. 'id': '2',
  431. 'url': TEST_URL,
  432. 'title': 'two',
  433. 'extractor': 'TEST',
  434. 'duration': 10,
  435. 'description': 'foo',
  436. 'filesize': 5 * 1024,
  437. }
  438. videos = [first, second]
  439. def get_videos(filter_=None):
  440. ydl = FilterYDL({'match_filter': filter_})
  441. for v in videos:
  442. ydl.process_ie_result(v, download=True)
  443. return [v['id'] for v in ydl.downloaded_info_dicts]
  444. res = get_videos()
  445. self.assertEqual(res, ['1', '2'])
  446. def f(v):
  447. if v['id'] == '1':
  448. return None
  449. else:
  450. return 'Video id is not 1'
  451. res = get_videos(f)
  452. self.assertEqual(res, ['1'])
  453. f = match_filter_func('duration < 30')
  454. res = get_videos(f)
  455. self.assertEqual(res, ['2'])
  456. f = match_filter_func('description = foo')
  457. res = get_videos(f)
  458. self.assertEqual(res, ['2'])
  459. f = match_filter_func('description =? foo')
  460. res = get_videos(f)
  461. self.assertEqual(res, ['1', '2'])
  462. f = match_filter_func('filesize > 5KiB')
  463. res = get_videos(f)
  464. self.assertEqual(res, ['1'])
  465. def test_playlist_items_selection(self):
  466. entries = [{
  467. 'id': compat_str(i),
  468. 'title': compat_str(i),
  469. 'url': TEST_URL,
  470. } for i in range(1, 5)]
  471. playlist = {
  472. '_type': 'playlist',
  473. 'id': 'test',
  474. 'entries': entries,
  475. 'extractor': 'test:playlist',
  476. 'extractor_key': 'test:playlist',
  477. 'webpage_url': 'http://example.com',
  478. }
  479. def get_ids(params):
  480. ydl = YDL(params)
  481. # make a copy because the dictionary can be modified
  482. ydl.process_ie_result(playlist.copy())
  483. return [int(v['id']) for v in ydl.downloaded_info_dicts]
  484. result = get_ids({})
  485. self.assertEqual(result, [1, 2, 3, 4])
  486. result = get_ids({'playlistend': 10})
  487. self.assertEqual(result, [1, 2, 3, 4])
  488. result = get_ids({'playlistend': 2})
  489. self.assertEqual(result, [1, 2])
  490. result = get_ids({'playliststart': 10})
  491. self.assertEqual(result, [])
  492. result = get_ids({'playliststart': 2})
  493. self.assertEqual(result, [2, 3, 4])
  494. result = get_ids({'playlist_items': '2-4'})
  495. self.assertEqual(result, [2, 3, 4])
  496. result = get_ids({'playlist_items': '2,4'})
  497. self.assertEqual(result, [2, 4])
  498. result = get_ids({'playlist_items': '10'})
  499. self.assertEqual(result, [])
  500. if __name__ == '__main__':
  501. unittest.main()