|
3 | 3 | import time |
4 | 4 | from youtubesearchpython.__future__ import * |
5 | 5 |
|
6 | | -TIMEOUT = 60 |
7 | | - |
8 | 6 | def print_json(data): |
9 | 7 | print(json.dumps(data, indent=2, ensure_ascii=False)) |
10 | 8 |
|
11 | | -async def run_get(url): |
12 | | - start = time.perf_counter() |
13 | | - try: |
14 | | - result = await Playlist.get(url, timeout=TIMEOUT) |
15 | | - elapsed = time.perf_counter() - start |
16 | | - print_json(result) |
17 | | - print(f"\n⏱ Playlist.get took {elapsed:.3f} seconds\n{'-'*60}\n") |
18 | | - except Exception as e: |
19 | | - elapsed = time.perf_counter() - start |
20 | | - print_json({"error": type(e).__name__, "message": str(e)}) |
21 | | - print(f"\n⏱ Playlist.get failed after {elapsed:.3f} seconds\n{'-'*60}\n") |
| 9 | +def print_timing(fn_name, elapsed): |
| 10 | + print(f"⏱ {fn_name}: {elapsed:.3f}s") |
| 11 | + print("-" * 60) |
22 | 12 |
|
23 | | -async def run_get_info(url): |
| 13 | +async def timed(fn_name, coro): |
24 | 14 | start = time.perf_counter() |
25 | | - try: |
26 | | - result = await Playlist.getInfo(url, timeout=TIMEOUT) |
27 | | - elapsed = time.perf_counter() - start |
28 | | - print_json(result) |
29 | | - print(f"\n⏱ Playlist.getInfo took {elapsed:.3f} seconds\n{'-'*60}\n") |
30 | | - except Exception as e: |
31 | | - elapsed = time.perf_counter() - start |
32 | | - print_json({"error": type(e).__name__, "message": str(e)}) |
33 | | - print(f"\n⏱ Playlist.getInfo failed after {elapsed:.3f} seconds\n{'-'*60}\n") |
34 | | - |
35 | | -async def run_get_videos(url): |
36 | | - start = time.perf_counter() |
37 | | - try: |
38 | | - result = await Playlist.getVideos(url, timeout=TIMEOUT) |
39 | | - elapsed = time.perf_counter() - start |
40 | | - print_json(result) |
41 | | - print(f"\n⏱ Playlist.getVideos took {elapsed:.3f} seconds\n{'-'*60}\n") |
42 | | - except Exception as e: |
43 | | - elapsed = time.perf_counter() - start |
44 | | - print_json({"error": type(e).__name__, "message": str(e)}) |
45 | | - print(f"\n⏱ Playlist.getVideos failed after {elapsed:.3f} seconds\n{'-'*60}\n") |
| 15 | + result = await coro |
| 16 | + elapsed = time.perf_counter() - start |
| 17 | + print_timing(fn_name, elapsed) |
| 18 | + return result |
46 | 19 |
|
47 | 20 | async def main(): |
48 | 21 | url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK" |
49 | 22 | url2 = "https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ" |
50 | 23 |
|
51 | | - await run_get(url1) |
52 | | - await run_get_info(url1) |
53 | | - await run_get_videos(url1) |
54 | | - |
55 | | - await run_get(url1) |
56 | | - await run_get(url2) |
| 24 | + print_json(await timed("Playlist.get", Playlist.get(url1))) |
| 25 | + print_json(await timed("Playlist.getInfo", Playlist.getInfo(url1))) |
| 26 | + print_json(await timed("Playlist.getVideos", Playlist.getVideos(url1))) |
| 27 | + print_json(await timed("Playlist.get (cached)", Playlist.get(url1))) |
| 28 | + print_json(await timed("Playlist.get (video+playlist URL)", Playlist.get(url2))) |
57 | 29 |
|
58 | 30 | asyncio.run(main()) |
0 commit comments