Skip to content

Commit dd23839

Browse files
authored
Update playlists.py
1 parent 5789450 commit dd23839

1 file changed

Lines changed: 73 additions & 18 deletions

File tree

tests/sync/playlists.py

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,79 @@
1-
from youtubesearchpython import *
1+
import asyncio
2+
import json
3+
import time
24

3-
playlist = Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
4-
print(playlist)
5-
playlistInfo = Playlist.getInfo('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
6-
print(playlistInfo)
7-
playlistVideos = Playlist.getVideos('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
8-
print(playlistVideos)
5+
from youtubesearchpython.__future__ import Playlist
96

107

11-
playlist = Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
12-
print(playlist)
13-
playlist = Playlist.get('https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ', mode = ResultMode.json)
14-
print(playlist)
8+
def pretty_print(data, fn_name, elapsed):
9+
print(json.dumps(data, indent=2, ensure_ascii=False))
10+
print(f"\n{fn_name} took {elapsed:.3f} seconds\n{'-' * 60}\n")
1511

1612

17-
playlist = Playlist('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
18-
print(f'Videos Retrieved: {len(playlist.videos)}')
19-
while playlist.hasMoreVideos:
20-
print('Getting more videos...')
21-
playlist.getNextVideos()
22-
print(f'Videos Retrieved: {len(playlist.videos)}')
13+
async def timed_call(fn_name, coro):
14+
start = time.perf_counter()
15+
result = await coro
16+
elapsed = time.perf_counter() - start
17+
return result, fn_name, elapsed
2318

24-
print('Found all the videos.')
19+
20+
async def timed_thread_call(fn_name, func, *args, **kwargs):
21+
start = time.perf_counter()
22+
result = await asyncio.to_thread(func, *args, **kwargs)
23+
elapsed = time.perf_counter() - start
24+
return result, fn_name, elapsed
25+
26+
27+
async def main():
28+
start_all = time.perf_counter()
29+
30+
url_playlist = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK"
31+
url_video_playlist = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ"
32+
33+
playlist, fn, t = await timed_call(
34+
"Playlist.get (playlist url)",
35+
Playlist.get(url_playlist),
36+
)
37+
pretty_print(playlist, fn, t)
38+
39+
playlist_info, fn, t = await timed_call(
40+
"Playlist.getInfo",
41+
Playlist.getInfo(url_playlist),
42+
)
43+
pretty_print(playlist_info, fn, t)
44+
45+
playlist_videos, fn, t = await timed_call(
46+
"Playlist.getVideos",
47+
Playlist.getVideos(url_playlist),
48+
)
49+
pretty_print(playlist_videos, fn, t)
50+
51+
playlist_from_video, fn, t = await timed_call(
52+
"Playlist.get (video+list url)",
53+
Playlist.get(url_video_playlist),
54+
)
55+
pretty_print(playlist_from_video, fn, t)
56+
57+
playlist_obj = Playlist(url_playlist)
58+
59+
_, fn, t = await timed_thread_call(
60+
"Playlist.init",
61+
playlist_obj.init,
62+
)
63+
print(f"⏱ {fn} took {t:.3f} seconds")
64+
print(f"Videos Retrieved: {len(playlist_obj.videos)}\n{'-' * 60}\n")
65+
66+
while playlist_obj.has_more_videos():
67+
_, fn, t = await timed_thread_call(
68+
"Playlist.getNextVideos",
69+
playlist_obj.getNextVideos,
70+
)
71+
print(f"⏱ {fn} took {t:.3f} seconds | Videos Retrieved: {len(playlist_obj.videos)}")
72+
73+
print("\nFound all the videos.")
74+
75+
total_time = time.perf_counter() - start_all
76+
print(f"\n✅ All tasks completed successfully in {total_time:.3f} seconds")
77+
78+
79+
asyncio.run(main())

0 commit comments

Comments
 (0)