def test_videos(youtube, request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = playlist_html playlist = Playlist(url) playlist._find_load_more_url = MagicMock(return_value=None) request_get.assert_called() assert len(list(playlist.videos)) == 12
def test_title(request_get): request_get.return_value = ("<title>(149) Python Tutorial for Beginners " "(For Absolute Beginners) - YouTube</title>") url = "https://www.fakeurl.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n" pl = Playlist(url) pl_title = pl.title() assert pl_title == "(149) Python Tutorial for Beginners (For Absolute Beginners)"
def test_sequence(request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = playlist_html playlist = Playlist(url) playlist._find_load_more_url = MagicMock(return_value=None) assert playlist[0] == "https://www.youtube.com/watch?v=ujTCoH21GlA" assert len(playlist) == 12
def main(): """Command line application to download youtube videos.""" # noinspection PyTypeChecker parser = argparse.ArgumentParser(description=main.__doc__) args = _parse_args(parser) if args.verbosity: log_level = min(args.verbosity, 4) * 10 setup_logger(logging.FATAL - log_level) if not args.url or "youtu" not in args.url: parser.print_help() sys.exit(1) if "/playlist" in args.url: print("Loading playlist...") playlist = Playlist(args.url) if not args.target: args.target = safe_filename(playlist.title()) for youtube_video in playlist.videos: try: _perform_args_on_youtube(youtube_video, args) except PytubeError as e: print(f"There was an error with video: {youtube_video}") print(e) else: print("Loading video...") youtube = YouTubeItem(args.url) _perform_args_on_youtube(youtube, args)
def test_load_more(youtube, request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.side_effect = [ playlist_html, '{"content_html":"", "load_more_widget_html":""}', ] playlist = Playlist(url) playlist._find_load_more_url = MagicMock(side_effect=["dummy", None]) request_get.assert_called() assert len(list(playlist.videos)) == 12
def test_trimmed_pagination_not_found(request_get, playlist_html, playlist_long_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.side_effect = [ playlist_long_html, '{"content_html":"<a href=\\"/watch?v=BcWz41-4cDk&feature=plpp_video&ved' '=CCYQxjQYACITCO33n5-pn-cCFUG3xAodLogN2yj6LA\\">}", "load_more_widget_html":""}', "{}", ] playlist = Playlist(url) assert len(list(playlist.trimmed("wont-be-found"))) == 101
def test_trimmed(request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = playlist_html playlist = Playlist(url) playlist._find_load_more_url = MagicMock(return_value=None) assert request_get.call_count == 1 trimmed = list(playlist.trimmed("1BYu65vLKdA")) assert trimmed == [ "https://www.youtube.com/watch?v=ujTCoH21GlA", "https://www.youtube.com/watch?v=45ryDIPHdGg", ]
def test_init_with_watch_id(request_get): request_get.return_value = "" playlist = Playlist("PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n") assert ( playlist.playlist_url == "https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n" )
def test_init_with_watch_url(request_get): request_get.return_value = "" url = ("https://www.youtube.com/watch?v=1KeYzjILqDo&" "list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n&index=2&t=661s") playlist = Playlist(url) assert ( playlist.playlist_url == "https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n" )
def test_repr(request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = playlist_html playlist = Playlist(url) playlist._find_load_more_url = MagicMock(return_value=None) request_get.assert_called() assert ( repr(playlist) == "['https://www.youtube.com/watch?v=ujTCoH21GlA', " "'https://www.youtube.com/watch?v=45ryDIPHdGg', " "'https://www.youtube.com/watch?v=1BYu65vLKdA', " "'https://www.youtube.com/watch?v=3AQ_74xrch8', " "'https://www.youtube.com/watch?v=ddqQUz9mZaM', " "'https://www.youtube.com/watch?v=vwLT6bZrHEE', " "'https://www.youtube.com/watch?v=TQKI0KE-JYY', " "'https://www.youtube.com/watch?v=dNBvQ38MlT8', " "'https://www.youtube.com/watch?v=JHxyrMgOUWI', " "'https://www.youtube.com/watch?v=l2I8NycJMCY', " "'https://www.youtube.com/watch?v=g1Zbuk1gAfk', " "'https://www.youtube.com/watch?v=zixd-si9Q-o']")
def test_video_urls(request_get, playlist_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = playlist_html playlist = Playlist(url) playlist._find_load_more_url = MagicMock(return_value=None) request_get.assert_called() assert playlist.video_urls == [ "https://www.youtube.com/watch?v=ujTCoH21GlA", "https://www.youtube.com/watch?v=45ryDIPHdGg", "https://www.youtube.com/watch?v=1BYu65vLKdA", "https://www.youtube.com/watch?v=3AQ_74xrch8", "https://www.youtube.com/watch?v=ddqQUz9mZaM", "https://www.youtube.com/watch?v=vwLT6bZrHEE", "https://www.youtube.com/watch?v=TQKI0KE-JYY", "https://www.youtube.com/watch?v=dNBvQ38MlT8", "https://www.youtube.com/watch?v=JHxyrMgOUWI", "https://www.youtube.com/watch?v=l2I8NycJMCY", "https://www.youtube.com/watch?v=g1Zbuk1gAfk", "https://www.youtube.com/watch?v=zixd-si9Q-o", ]
def test_playlist_failed_pagination(request_get, playlist_long_html): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.side_effect = [ playlist_long_html, "{}", ] playlist = Playlist(url) video_urls = playlist.video_urls assert len(video_urls) == 100 assert request_get.call_count == 2 request_get.assert_called_with( "https://www.youtube.com/browse_ajax?action_continuation=1&continuation" "=4qmFsgIsEhpWTFVVYS12aW9HaGUyYnRCY1puZWFQb25LQRoOZWdaUVZEcERSMUUlM0Q%253D" )
def test_last_update(request_get, playlist_html): expected = datetime.date(2019, 3, 7) request_get.return_value = playlist_html playlist = Playlist("url") assert playlist.last_update == expected
def test_init_with_playlist_url(request_get): request_get.return_value = "" url = "https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n" playlist = Playlist(url) assert playlist.playlist_url == url
def test_proxy(install_proxy, request_get): url = "https://www.fakeurl.com/playlist?list=whatever" request_get.return_value = "" Playlist(url, proxies={"http": "things"}) install_proxy.assert_called_with({"http": "things"})