X-Git-Url: https://jfr.im/git/yt-dlp.git/blobdiff_plain/5e5ddcfbcf62d975e46ffaa0aaee8744f70913e1..104a7b5a46dc1805157fb4cc11c05876934d37c1:/test/test_youtube_lists.py diff --git a/test/test_youtube_lists.py b/test/test_youtube_lists.py index e352e5ab9..b3f323e21 100644 --- a/test/test_youtube_lists.py +++ b/test/test_youtube_lists.py @@ -1,75 +1,71 @@ -#!/usr/bin/env python - -import sys -import unittest -import socket -import json +#!/usr/bin/env python3 # Allow direct execution import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +import sys +import unittest -from youtube_dl.InfoExtractors import YoutubeUserIE,YoutubePlaylistIE -from youtube_dl.utils import * +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json") -with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: - parameters = json.load(pf) -# General configuration (from __init__, not very elegant...) -jar = compat_cookiejar.CookieJar() -cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar) -proxy_handler = compat_urllib_request.ProxyHandler() -opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler()) -compat_urllib_request.install_opener(opener) -socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words) +from test.helper import FakeYDL, is_download_test +from yt_dlp.extractor import YoutubeIE, YoutubeTabIE +from yt_dlp.utils import ExtractorError -class FakeDownloader(object): - def __init__(self): - self.result = [] - self.params = parameters - def to_screen(self, s): - print(s) - def trouble(self, s): - raise Exception(s) - def download(self, x): - self.result.append(x) +@is_download_test class TestYoutubeLists(unittest.TestCase): - def test_youtube_playlist(self): - DL = FakeDownloader() - IE = YoutubePlaylistIE(DL) - IE.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re') - self.assertEqual(DL.result, [ - ['http://www.youtube.com/watch?v=bV9L5Ht9LgY'], - ['http://www.youtube.com/watch?v=FXxLjLQi3Fg'], - ['http://www.youtube.com/watch?v=tU3Bgo5qJZE'] - ]) + def assertIsPlaylist(self, info): + """Make sure the info has '_type' set to 'playlist'""" + self.assertEqual(info['_type'], 'playlist') + + def test_youtube_playlist_noplaylist(self): + dl = FakeYDL() + dl.params['noplaylist'] = True + ie = YoutubeTabIE(dl) + result = ie.extract('https://www.youtube.com/watch?v=OmJ-4B-mS-Y&list=PLydZ2Hrp_gPRJViZjLFKaBMgCQOYEEkyp&index=2') + self.assertEqual(result['_type'], 'url') + self.assertEqual(result['ie_key'], YoutubeIE.ie_key()) + self.assertEqual(YoutubeIE.extract_id(result['url']), 'OmJ-4B-mS-Y') + + def test_youtube_mix(self): + dl = FakeYDL() + ie = YoutubeTabIE(dl) + result = ie.extract('https://www.youtube.com/watch?v=tyITL_exICo&list=RDCLAK5uy_kLWIr9gv1XLlPbaDS965-Db4TrBoUTxQ8') + entries = list(result['entries']) + self.assertTrue(len(entries) >= 50) + original_video = entries[0] + self.assertEqual(original_video['id'], 'tyITL_exICo') - def test_youtube_playlist_long(self): - DL = FakeDownloader() - IE = YoutubePlaylistIE(DL) - IE.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q') - self.assertTrue(len(DL.result) >= 799) + def test_youtube_flat_playlist_extraction(self): + dl = FakeYDL() + dl.params['extract_flat'] = True + ie = YoutubeTabIE(dl) + result = ie.extract('https://www.youtube.com/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc') + self.assertIsPlaylist(result) + entries = list(result['entries']) + self.assertTrue(len(entries) == 1) + video = entries[0] + self.assertEqual(video['_type'], 'url') + self.assertEqual(video['ie_key'], 'Youtube') + self.assertEqual(video['id'], 'BaW_jenozKc') + self.assertEqual(video['url'], 'https://www.youtube.com/watch?v=BaW_jenozKc') + self.assertEqual(video['title'], 'youtube-dl test video "\'/\\ä↭𝕐') + self.assertEqual(video['duration'], 10) + self.assertEqual(video['uploader'], 'Philipp Hagemeister') - def test_youtube_course(self): - DL = FakeDownloader() - IE = YoutubePlaylistIE(DL) - # TODO find a > 100 (paginating?) videos course - IE.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8') - self.assertEqual(DL.result[0], ['http://www.youtube.com/watch?v=j9WZyLZCBzs']) - self.assertEqual(len(DL.result), 25) - self.assertEqual(DL.result[-1], ['http://www.youtube.com/watch?v=rYefUsYuEp0']) + def test_youtube_channel_no_uploads(self): + dl = FakeYDL() + dl.params['extract_flat'] = True + ie = YoutubeTabIE(dl) + # no uploads + with self.assertRaisesRegex(ExtractorError, r'no uploads'): + ie.extract('https://www.youtube.com/channel/UC2yXPzFejc422buOIzn_0CA') - def test_youtube_channel(self): - # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long - pass # TODO + # no uploads and no UCID given + with self.assertRaisesRegex(ExtractorError, r'no uploads'): + ie.extract('https://www.youtube.com/news') - def test_youtube_user(self): - DL = FakeDownloader() - IE = YoutubeUserIE(DL) - IE.extract('https://www.youtube.com/user/TheLinuxFoundation') - self.assertTrue(len(DL.result) >= 320) if __name__ == '__main__': unittest.main()