|
1 | | -from youtubesearchpython import * |
2 | | - |
3 | | - |
4 | | -allSearch = Search('EkdinMeri', limit = 1, language = 'en', region = 'US') |
5 | | -print(allSearch.result()) |
6 | | - |
7 | | - |
8 | | -videosSearch = VideosSearch('Mehrbanhua', limit = 5, language = 'en', region = 'US') |
9 | | -print(videosSearch.result(mode = ResultMode.json)) |
10 | | - |
11 | | - |
12 | | -channelsSearch = ChannelsSearch('TipsOfficial', limit = 2, language = 'en', region = 'US') |
13 | | -print(channelsSearch.result(mode = ResultMode.json)) |
14 | | - |
15 | | - |
16 | | -playlistsSearch = PlaylistsSearch('TipsOfficial', limit = 1, language = 'en', region = 'US') |
17 | | -print(playlistsSearch.result()) |
18 | | - |
19 | | - |
20 | | -customSearch = CustomSearch('hindisongs', VideoSortOrder.uploadDate, language = 'en', region = 'US') |
21 | | -print(customSearch.result()) |
22 | | - |
23 | | - |
24 | | -search = VideosSearch('Suzume') |
25 | | -index = 0 |
26 | | -for video in search.result()['result']: |
27 | | - print(str(index) + ' - ' + video['title']) |
28 | | - index += 1 |
29 | | -search.next() |
30 | | -for video in search.result()['result']: |
31 | | - print(str(index) + ' - ' + video['title']) |
32 | | - index += 1 |
33 | | -search.next() |
34 | | -for video in search.result()['result']: |
35 | | - print(str(index) + ' - ' + video['title']) |
36 | | - index += 1 |
37 | | - |
38 | | - |
39 | | -channel = ChannelSearch("Watermelon Sugar", "UCZFWPqqPkFlNwIxcpsLOwew") |
40 | | -print(channel.result(mode=ResultMode.json)) |
41 | | - |
42 | | -channel = ChannelSearch('The Beatles - Topic', 'UC2XdaAVUannpujzv32jcouQ') |
43 | | -print(channel.result(mode=ResultMode.json)) |
44 | | - |
45 | | -#channel = ChannelPlaylistSearch('PewDiePie', 'UC-lHJZR3Gqxm24_Vd_AJ5Yw') |
46 | | -#print(channel.result()) |
47 | | - |
48 | | -#channel = ChannelPlaylistSearch('The Beatles - Topic', 'UC2XdaAVUannpujzv32jcouQ') |
49 | | -#print(channel.result()) |
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import time |
| 4 | + |
| 5 | +from youtubesearchpython.__future__ import ( |
| 6 | + Search, |
| 7 | + VideosSearch, |
| 8 | + ChannelsSearch, |
| 9 | + PlaylistsSearch, |
| 10 | + CustomSearch, |
| 11 | + ChannelSearch, |
| 12 | + VideoSortOrder, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def pretty_print(data, fn_name, elapsed): |
| 17 | + print(json.dumps(data, indent=2, ensure_ascii=False)) |
| 18 | + print(f"\n⏱ {fn_name} took {elapsed:.3f} seconds\n{'-' * 60}\n") |
| 19 | + |
| 20 | + |
| 21 | +async def timed_call(fn_name, coro): |
| 22 | + start = time.perf_counter() |
| 23 | + result = await coro |
| 24 | + elapsed = time.perf_counter() - start |
| 25 | + return result, fn_name, elapsed |
| 26 | + |
| 27 | + |
| 28 | +async def main(): |
| 29 | + start_all = time.perf_counter() |
| 30 | + |
| 31 | + all_search, fn, t = await timed_call( |
| 32 | + "Search.result", |
| 33 | + Search("EkdinMeri", limit=1, language="en", region="US").next(), |
| 34 | + ) |
| 35 | + pretty_print(all_search, fn, t) |
| 36 | + |
| 37 | + videos_search, fn, t = await timed_call( |
| 38 | + "VideosSearch.result", |
| 39 | + VideosSearch("Mehrbanhua", limit=5, language="en", region="US").next(), |
| 40 | + ) |
| 41 | + pretty_print(videos_search, fn, t) |
| 42 | + |
| 43 | + channels_search, fn, t = await timed_call( |
| 44 | + "ChannelsSearch.result", |
| 45 | + ChannelsSearch("TipsOfficial", limit=2, language="en", region="US").next(), |
| 46 | + ) |
| 47 | + pretty_print(channels_search, fn, t) |
| 48 | + |
| 49 | + playlists_search, fn, t = await timed_call( |
| 50 | + "PlaylistsSearch.result", |
| 51 | + PlaylistsSearch("TipsOfficial", limit=1, language="en", region="US").next(), |
| 52 | + ) |
| 53 | + pretty_print(playlists_search, fn, t) |
| 54 | + |
| 55 | + custom_search, fn, t = await timed_call( |
| 56 | + "CustomSearch.result", |
| 57 | + CustomSearch( |
| 58 | + "hindisongs", |
| 59 | + VideoSortOrder.uploadDate, |
| 60 | + language="en", |
| 61 | + region="US", |
| 62 | + ).next(), |
| 63 | + ) |
| 64 | + pretty_print(custom_search, fn, t) |
| 65 | + |
| 66 | + search = VideosSearch("Suzume", limit=5) |
| 67 | + index = 0 |
| 68 | + |
| 69 | + first_page, fn, t = await timed_call("VideosSearch.next (page 1)", search.next()) |
| 70 | + print(f"⏱ {fn} took {t:.3f} seconds") |
| 71 | + for v in first_page["result"]: |
| 72 | + print(f"{index} - {v['title']}") |
| 73 | + index += 1 |
| 74 | + print("-" * 60) |
| 75 | + |
| 76 | + second_page, fn, t = await timed_call("VideosSearch.next (page 2)", search.next()) |
| 77 | + print(f"⏱ {fn} took {t:.3f} seconds") |
| 78 | + for v in second_page["result"]: |
| 79 | + print(f"{index} - {v['title']}") |
| 80 | + index += 1 |
| 81 | + print("-" * 60) |
| 82 | + |
| 83 | + third_page, fn, t = await timed_call("VideosSearch.next (page 3)", search.next()) |
| 84 | + print(f"⏱ {fn} took {t:.3f} seconds") |
| 85 | + for v in third_page["result"]: |
| 86 | + print(f"{index} - {v['title']}") |
| 87 | + index += 1 |
| 88 | + print("-" * 60) |
| 89 | + |
| 90 | + channel_search_1, fn, t = await timed_call( |
| 91 | + "ChannelSearch.result (Watermelon Sugar)", |
| 92 | + ChannelSearch( |
| 93 | + "Watermelon Sugar", |
| 94 | + "UCZFWPqqPkFlNwIxcpsLOwew", |
| 95 | + ).next(), |
| 96 | + ) |
| 97 | + pretty_print(channel_search_1, fn, t) |
| 98 | + |
| 99 | + channel_search_2, fn, t = await timed_call( |
| 100 | + "ChannelSearch.result (The Beatles - Topic)", |
| 101 | + ChannelSearch( |
| 102 | + "The Beatles - Topic", |
| 103 | + "UC2XdaAVUannpujzv32jcouQ", |
| 104 | + ).next(), |
| 105 | + ) |
| 106 | + pretty_print(channel_search_2, fn, t) |
| 107 | + |
| 108 | + total_time = time.perf_counter() - start_all |
| 109 | + print(f"\n✅ All tasks completed successfully in {total_time:.3f} seconds") |
| 110 | + |
| 111 | + |
| 112 | +asyncio.run(main()) |
0 commit comments