test_YoutubeDL.py 21 KB

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