|
3 | 3 | import time |
4 | 4 | from youtubesearchpython.__future__ import * |
5 | 5 |
|
6 | | -def print_json(data): |
| 6 | +def pretty_print(data, fn_name, elapsed): |
7 | 7 | 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") |
9 | 9 |
|
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): |
15 | 11 | start = time.perf_counter() |
16 | 12 | result = await coro |
17 | 13 | elapsed = time.perf_counter() - start |
18 | | - return fn_name, elapsed, result |
| 14 | + return result, fn_name, elapsed |
19 | 15 |
|
20 | 16 | async def main(): |
21 | 17 | url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK" |
22 | 18 | url2 = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ" |
23 | 19 |
|
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) |
34 | 49 |
|
35 | 50 | asyncio.run(main()) |
0 commit comments