|
3 | 3 | import time |
4 | 4 | from youtubesearchpython.__future__ import * |
5 | 5 |
|
| 6 | +TIMEOUT = 60 |
| 7 | + |
6 | 8 | def print_json(data): |
7 | 9 | print(json.dumps(data, indent=2, ensure_ascii=False)) |
8 | 10 |
|
9 | 11 | async def run_get(url): |
10 | 12 | start = time.perf_counter() |
11 | | - result = await Playlist.get(url) |
12 | | - elapsed = time.perf_counter() - start |
13 | | - print_json(result) |
14 | | - print(f"\n⏱ Playlist.get took {elapsed:.3f} seconds\n{'-'*60}\n") |
| 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") |
15 | 22 |
|
16 | 23 | async def run_get_info(url): |
17 | 24 | 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") |
| 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") |
22 | 34 |
|
23 | 35 | async def run_get_videos(url): |
24 | 36 | 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") |
| 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") |
29 | 46 |
|
30 | 47 | async def main(): |
31 | 48 | url1 = "https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK" |
|
0 commit comments