|
1 | 1 | import asyncio |
2 | | -from youtubesearchpython.__future__ import Video, StreamURLFetcher, Suggestions |
| 2 | +import json |
| 3 | +import time |
| 4 | + |
| 5 | +from youtubesearchpython.__future__ import ( |
| 6 | + Video, |
| 7 | + StreamURLFetcher, |
| 8 | + Suggestions, |
| 9 | +) |
3 | 10 | from youtubesearchpython import Hashtag, Comments, Transcript, Channel |
4 | 11 |
|
| 12 | + |
| 13 | +def pretty_print(data, fn_name, elapsed): |
| 14 | + print(json.dumps(data, indent=2, ensure_ascii=False)) |
| 15 | + print(f"\n⏱ {fn_name} took {elapsed:.3f} seconds\n{'-' * 60}\n") |
| 16 | + |
| 17 | + |
| 18 | +async def timed_call(fn_name, coro): |
| 19 | + start = time.perf_counter() |
| 20 | + result = await coro |
| 21 | + elapsed = time.perf_counter() - start |
| 22 | + return result, fn_name, elapsed |
| 23 | + |
| 24 | + |
| 25 | +async def timed_thread_call(fn_name, func, *args, **kwargs): |
| 26 | + start = time.perf_counter() |
| 27 | + result = await asyncio.to_thread(func, *args, **kwargs) |
| 28 | + elapsed = time.perf_counter() - start |
| 29 | + return result, fn_name, elapsed |
| 30 | + |
| 31 | + |
5 | 32 | async def main(): |
6 | | - video = await Video.get('https://youtu.be/dQw4w9WgXcQ', get_upload_date=True) |
7 | | - print(video) |
8 | | - |
9 | | - videoInfo = await Video.getInfo('https://youtu.be/dQw4w9WgXcQ') |
10 | | - print(videoInfo) |
11 | | - |
12 | | - videoFormats = await Video.getFormats('dQw4w9WgXcQ') |
13 | | - print(videoFormats) |
14 | | - |
15 | | - suggestions = await Suggestions.get('AgarTumSaathHo', language='en', region='US') |
16 | | - print(suggestions) |
17 | | - |
18 | | - hashtag = Hashtag('IndianArmy', limit=5) |
19 | | - result = await asyncio.to_thread(hashtag.result) |
20 | | - print(result) |
| 33 | + start_all = time.perf_counter() |
| 34 | + |
| 35 | + video, fn, t = await timed_call( |
| 36 | + "Video.get", |
| 37 | + Video.get("https://youtu.be/dQw4w9WgXcQ", get_upload_date=True), |
| 38 | + ) |
| 39 | + pretty_print(video, fn, t) |
| 40 | + |
| 41 | + video_info, fn, t = await timed_call( |
| 42 | + "Video.getInfo", |
| 43 | + Video.getInfo("https://youtu.be/dQw4w9WgXcQ"), |
| 44 | + ) |
| 45 | + pretty_print(video_info, fn, t) |
| 46 | + |
| 47 | + video_formats, fn, t = await timed_call( |
| 48 | + "Video.getFormats", |
| 49 | + Video.getFormats("dQw4w9WgXcQ"), |
| 50 | + ) |
| 51 | + pretty_print(video_formats, fn, t) |
| 52 | + |
| 53 | + suggestions, fn, t = await timed_call( |
| 54 | + "Suggestions.get", |
| 55 | + Suggestions.get("AgarTumSaathHo", language="en", region="US"), |
| 56 | + ) |
| 57 | + pretty_print(suggestions, fn, t) |
| 58 | + |
| 59 | + hashtag = Hashtag("IndianArmy", limit=5) |
| 60 | + hashtag_result, fn, t = await timed_thread_call( |
| 61 | + "Hashtag.result", |
| 62 | + hashtag.result, |
| 63 | + ) |
| 64 | + pretty_print(hashtag_result, fn, t) |
21 | 65 |
|
22 | 66 | fetcher = StreamURLFetcher() |
23 | | - |
| 67 | + |
24 | 68 | videoA = await Video.get("https://www.youtube.com/watch?v=aqz-KE-bpKQ") |
25 | 69 | videoB = await Video.get("https://www.youtube.com/watch?v=ZwNxYJfW-eU") |
26 | 70 | videoC = await Video.get("https://youtu.be/dQw4w9WgXcQ") |
27 | | - videoD = await Video.get("https://www.youtube.com/watch?v=9bZkp7q19f0") # Gangnam Style - good public video for streams |
28 | | - |
29 | | - singleUrlA = await fetcher.get(videoA, 251) |
30 | | - allUrlsB = await fetcher.getAll(videoB) |
31 | | - singleUrlC = await fetcher.get(videoC, 251) |
32 | | - allUrlsC = await fetcher.getAll(videoC) |
33 | | - singleUrlD_251 = await fetcher.get(videoD, 251) |
34 | | - singleUrlD_140 = await fetcher.get(videoD, 140) |
35 | | - allUrlsD = await fetcher.getAll(videoD) |
36 | | - |
37 | | - print(singleUrlA) |
38 | | - print(allUrlsB) |
39 | | - print(singleUrlC) |
40 | | - print(allUrlsC) |
41 | | - print(singleUrlD_251) |
42 | | - print(singleUrlD_140) |
43 | | - print(allUrlsD) |
| 71 | + videoD = await Video.get("https://www.youtube.com/watch?v=9bZkp7q19f0") |
| 72 | + |
| 73 | + singleA, fn, t = await timed_call( |
| 74 | + "StreamURLFetcher.get (251)", |
| 75 | + fetcher.get(videoA, 251), |
| 76 | + ) |
| 77 | + pretty_print(singleA, fn, t) |
| 78 | + |
| 79 | + allB, fn, t = await timed_call( |
| 80 | + "StreamURLFetcher.getAll", |
| 81 | + fetcher.getAll(videoB), |
| 82 | + ) |
| 83 | + pretty_print(allB, fn, t) |
| 84 | + |
| 85 | + singleC, fn, t = await timed_call( |
| 86 | + "StreamURLFetcher.get (251)", |
| 87 | + fetcher.get(videoC, 251), |
| 88 | + ) |
| 89 | + pretty_print(singleC, fn, t) |
| 90 | + |
| 91 | + allC, fn, t = await timed_call( |
| 92 | + "StreamURLFetcher.getAll", |
| 93 | + fetcher.getAll(videoC), |
| 94 | + ) |
| 95 | + pretty_print(allC, fn, t) |
| 96 | + |
| 97 | + singleD251, fn, t = await timed_call( |
| 98 | + "StreamURLFetcher.get (251)", |
| 99 | + fetcher.get(videoD, 251), |
| 100 | + ) |
| 101 | + pretty_print(singleD251, fn, t) |
| 102 | + |
| 103 | + singleD140, fn, t = await timed_call( |
| 104 | + "StreamURLFetcher.get (140)", |
| 105 | + fetcher.get(videoD, 140), |
| 106 | + ) |
| 107 | + pretty_print(singleD140, fn, t) |
| 108 | + |
| 109 | + allD, fn, t = await timed_call( |
| 110 | + "StreamURLFetcher.getAll", |
| 111 | + fetcher.getAll(videoD), |
| 112 | + ) |
| 113 | + pretty_print(allD, fn, t) |
44 | 114 |
|
45 | 115 | comments = Comments("dQw4w9WgXcQ") |
46 | | - await asyncio.to_thread(comments.getNextComments) |
| 116 | + |
| 117 | + _, fn, t = await timed_thread_call( |
| 118 | + "Comments.getNextComments", |
| 119 | + comments.getNextComments, |
| 120 | + ) |
| 121 | + print(f"⏱ {fn} took {t:.3f} seconds\n{'-' * 60}\n") |
| 122 | + |
47 | 123 | while len(comments.comments["result"]) < 100 and getattr(comments, "hasMoreComments", False): |
48 | | - print(len(comments.comments["result"])) |
49 | 124 | await asyncio.to_thread(comments.getNextComments) |
50 | | - print("Found all comments") |
51 | 125 |
|
52 | | - transcript_en = await asyncio.to_thread(Transcript.get, "https://youtu.be/89d02K5pIU8?si=z0NtBv6iV1Rc37Mq") |
53 | | - print(transcript_en) |
54 | | - |
| 126 | + pretty_print(comments.comments, "Comments (100+)", 0.0) |
| 127 | + |
| 128 | + transcript, fn, t = await timed_thread_call( |
| 129 | + "Transcript.get", |
| 130 | + Transcript.get, |
| 131 | + "https://youtu.be/89d02K5pIU8?si=z0NtBv6iV1Rc37Mq", |
| 132 | + ) |
| 133 | + pretty_print(transcript, fn, t) |
| 134 | + |
55 | 135 | url = "https://youtu.be/dQw4w9WgXcQ" |
56 | | - transcript_en = await asyncio.to_thread(Transcript.get, url) |
57 | | - if transcript_en.get("languages"): |
58 | | - transcript_2 = await asyncio.to_thread(Transcript.get, url, transcript_en["languages"][-1]["params"]) |
59 | | - print(transcript_2) |
| 136 | + transcript_main, fn, t = await timed_thread_call( |
| 137 | + "Transcript.get (default)", |
| 138 | + Transcript.get, |
| 139 | + url, |
| 140 | + ) |
| 141 | + pretty_print(transcript_main, fn, t) |
60 | 142 |
|
61 | | - channel_info = await asyncio.to_thread(Channel.get, "UC_aEa8K-EOJ3d6gOs7HcyNg") |
62 | | - print(channel_info) |
| 143 | + if transcript_main.get("languages"): |
| 144 | + transcript_alt, fn, t = await timed_thread_call( |
| 145 | + "Transcript.get (alt language)", |
| 146 | + Transcript.get, |
| 147 | + url, |
| 148 | + transcript_main["languages"][-1]["params"], |
| 149 | + ) |
| 150 | + pretty_print(transcript_alt, fn, t) |
| 151 | + |
| 152 | + channel_info, fn, t = await timed_thread_call( |
| 153 | + "Channel.get", |
| 154 | + Channel.get, |
| 155 | + "UC_aEa8K-EOJ3d6gOs7HcyNg", |
| 156 | + ) |
| 157 | + pretty_print(channel_info, fn, t) |
63 | 158 |
|
64 | 159 | channel = Channel("UC_aEa8K-EOJ3d6gOs7HcyNg") |
65 | | - await asyncio.to_thread(channel.init) |
66 | | - print(len(channel.result["playlists"])) |
| 160 | + |
| 161 | + _, fn, t = await timed_thread_call( |
| 162 | + "Channel.init", |
| 163 | + channel.init, |
| 164 | + ) |
| 165 | + print(f"⏱ {fn} took {t:.3f} seconds") |
| 166 | + print(f"Playlists: {len(channel.result['playlists'])}\n{'-' * 60}\n") |
| 167 | + |
67 | 168 | while channel.has_more_playlists(): |
68 | 169 | await asyncio.to_thread(channel.next) |
69 | | - print(len(channel.result["playlists"])) |
| 170 | + print(f"Playlists: {len(channel.result['playlists'])}") |
| 171 | + |
| 172 | + total_time = time.perf_counter() - start_all |
| 173 | + print(f"\n✅ All tasks completed successfully in {total_time:.3f} seconds") |
| 174 | + |
70 | 175 |
|
71 | 176 | asyncio.run(main()) |
0 commit comments