Skip to content

Commit bfb8640

Browse files
authored
Update playlists.py
1 parent 46ac95e commit bfb8640

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

tests/async/playlists.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,48 @@
33
import time
44
from youtubesearchpython.__future__ import *
55

6-
def print_json(data):
6+
def pretty_print(data, fn_name, elapsed):
77
print(json.dumps(data, indent=2, ensure_ascii=False))
8-
print("-" * 60)
8+
print(f"\n{fn_name} took {elapsed:.3f} seconds\n{'-'*60}\n")
99

10-
def print_timing(fn_name, elapsed):
11-
print(f"⏱ {fn_name}: {elapsed:.3f}s")
12-
print("=" * 60)
13-
14-
async def timed(fn_name, coro):
10+
async def timed_call(fn_name, coro):
1511
start = time.perf_counter()
1612
result = await coro
1713
elapsed = time.perf_counter() - start
18-
return fn_name, elapsed, result
14+
return result, fn_name, elapsed
1915

2016
async def main():
2117
url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK"
2218
url2 = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ"
2319

24-
for fn, coro in [
25-
("Playlist.get", Playlist.get(url1)),
26-
("Playlist.getInfo", Playlist.getInfo(url1)),
27-
("Playlist.getVideos", Playlist.getVideos(url1)),
28-
("Playlist.get (cached)", Playlist.get(url1)),
29-
("Playlist.get (video+playlist URL)", Playlist.get(url2)),
30-
]:
31-
name, elapsed, result = await timed(fn, coro)
32-
print_timing(name, elapsed)
33-
print_json(result)
20+
playlist, fn, t = await timed_call(
21+
"Playlist.get",
22+
Playlist.get(url1),
23+
)
24+
pretty_print(playlist, fn, t)
25+
26+
playlistInfo, fn, t = await timed_call(
27+
"Playlist.getInfo",
28+
Playlist.getInfo(url1),
29+
)
30+
pretty_print(playlistInfo, fn, t)
31+
32+
playlistVideos, fn, t = await timed_call(
33+
"Playlist.getVideos",
34+
Playlist.getVideos(url1),
35+
)
36+
pretty_print(playlistVideos, fn, t)
37+
38+
playlist, fn, t = await timed_call(
39+
"Playlist.get",
40+
Playlist.get(url1),
41+
)
42+
pretty_print(playlist, fn, t)
43+
44+
playlist, fn, t = await timed_call(
45+
"Playlist.get (video+playlist URL)",
46+
Playlist.get(url2),
47+
)
48+
pretty_print(playlist, fn, t)
3449

3550
asyncio.run(main())

0 commit comments

Comments
 (0)