|
3 | 3 | import time |
4 | 4 | from youtubesearchpython.__future__ import * |
5 | 5 |
|
6 | | -def pretty_print(data, fn_name, elapsed): |
| 6 | +def print_json(data): |
7 | 7 | print(json.dumps(data, indent=2, ensure_ascii=False)) |
8 | | - print(f"\n⏱ {fn_name} took {elapsed:.3f} seconds\n{'-'*60}\n") |
9 | 8 |
|
10 | | -async def timed_call(fn_name, coro): |
| 9 | +async def run_get(url): |
11 | 10 | start = time.perf_counter() |
12 | | - result = await coro |
| 11 | + result = await Playlist.get(url) |
13 | 12 | elapsed = time.perf_counter() - start |
14 | | - return result, fn_name, elapsed |
| 13 | + print_json(result) |
| 14 | + print(f"\n⏱ Playlist.get took {elapsed:.3f} seconds\n{'-'*60}\n") |
| 15 | + |
| 16 | +async def run_get_info(url): |
| 17 | + start = time.perf_counter() |
| 18 | + result = await Playlist.getInfo(url) |
| 19 | + elapsed = time.perf_counter() - start |
| 20 | + print_json(result) |
| 21 | + print(f"\n⏱ Playlist.getInfo took {elapsed:.3f} seconds\n{'-'*60}\n") |
| 22 | + |
| 23 | +async def run_get_videos(url): |
| 24 | + start = time.perf_counter() |
| 25 | + result = await Playlist.getVideos(url) |
| 26 | + elapsed = time.perf_counter() - start |
| 27 | + print_json(result) |
| 28 | + print(f"\n⏱ Playlist.getVideos took {elapsed:.3f} seconds\n{'-'*60}\n") |
15 | 29 |
|
16 | 30 | async def main(): |
17 | 31 | url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK" |
18 | 32 | url2 = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ" |
19 | 33 |
|
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 | + await run_get(url1) |
| 35 | + await run_get_info(url1) |
| 36 | + await run_get_videos(url1) |
| 37 | + |
| 38 | + await run_get(url1) |
| 39 | + await run_get(url2) |
49 | 40 |
|
50 | 41 | asyncio.run(main()) |
0 commit comments