forked from alexmercerind/youtube-search-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncExample.py
259 lines (178 loc) · 8.27 KB
/
asyncExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from youtubesearchpython.__future__ import *
import asyncio
async def main():
'''
Searches for all types of results like videos, channels & playlists in YouTube.
'type' key in the JSON/Dictionary may be used to differentiate between the types of result.
'''
search = Search('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
result = await search.next()
print(result)
'''
Searches only for videos in YouTube.
'''
videosSearch = VideosSearch('NoCopyrightSounds', limit = 10, language = 'en', region = 'US')
videosResult = await videosSearch.next()
print(videosResult)
'''
Searches only for channels in YouTube.
'''
channelsSearch = ChannelsSearch('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
channelsResult = await channelsSearch.next()
print(channelsResult)
'''
Searches only for playlists in YouTube.
'''
playlistsSearch = PlaylistsSearch('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
playlistsResult = await playlistsSearch.next()
print(playlistsResult)
playlist = Playlist('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
while playlist.hasMoreVideos:
print('Getting more videos...')
await playlist.getNextVideos()
print(f'Videos Retrieved: {len(playlist.videos)}')
print('Found all the videos.')
'''
Can be used to get search results with custom defined filters.
Setting second parameter as VideoSortOrder.uploadDate, to get video results sorted according to upload date.
Few of the predefined filters for you to use are:
SearchMode.videos
VideoUploadDateFilter.lastHour
VideoDurationFilter.long
VideoSortOrder.viewCount
There are many other for you to check out.
If this much control isn't enough then, you may pass custom string yourself by seeing the YouTube query in any web browser e.g.
"EgQIBRAB" from "https://www.youtube.com/results?search_query=NoCopyrightSounds&sp=EgQIBRAB" may be passed as second parameter to get only videos, which are uploaded this year.
'''
customSearch = CustomSearch('NoCopyrightSounds', VideoSortOrder.uploadDate, language = 'en', region = 'US')
customResult = await customSearch.next()
print(customResult)
search = ChannelSearch('Watermelon Sugar', "UCZFWPqqPkFlNwIxcpsLOwew")
result = await search.next()
print(result)
'''
Getting search results from the next pages on YouTube.
Generally you'll get maximum of 20 videos in one search, for getting subsequent results, you may call `next` method.
'''
search = VideosSearch('NoCopyrightSounds')
index = 0
''' Getting result on 1st page '''
result = await search.next()
''' Displaying the result '''
for video in result['result']:
index += 1
print(f'{index} - {video["title"]}')
''' Getting result on 2nd page '''
result = await search.next()
''' Displaying the result '''
for video in result['result']:
index += 1
print(f'{index} - {video["title"]}')
''' Getting result on 3rd page '''
result = await search.next()
''' Displaying the result '''
for video in result['result']:
index += 1
print(f'{index} - {video["title"]}')
'''
Getting information about video or its formats using video link or video ID.
`Video.get` method will give both information & formats of the video
`Video.getInfo` method will give only information about the video.
`Video.getFormats` method will give only formats of the video.
You may either pass link or ID, method will take care itself.
'''
video = await Video.get('https://www.youtube.com/watch?v=z0GKGpObgPY', get_upload_date=True)
print(video)
videoInfo = await Video.getInfo('https://youtu.be/z0GKGpObgPY')
print(videoInfo)
videoFormats = await Video.getFormats('z0GKGpObgPY')
print(videoFormats)
'''
Getting information about playlist or videos in it using link.
`Playlist.get` method will give both information & videos in the playlist
`Playlist.getInfo` method will give only information about the playlist.
`Playlist.getFormats` method will give only formats of the playlist.
'''
playlist = await Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(playlist)
playlistInfo = await Playlist.getInfo('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(playlistInfo)
playlistVideos = await Playlist.getVideos('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(playlistVideos)
'''
More tests to buggy Playlist class
'''
playlist = await Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(playlist)
playlist = await Playlist.get('https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ')
print(playlist)
'''
Getting search suggestions from YouTube.
You may show search suggestions to users before making any search.
'''
suggestions = await Suggestions.get('NoCopyrightSounds', language = 'en', region = 'US')
print(suggestions)
'''
Getting videos by hashtag.
'''
hashtag = Hashtag('ncs', limit = 1)
result = await hashtag.next()
print(result)
'''
Getting direct stream URL for a video.
You may show search suggestions to users before making any search.
To use this, you must have PyTube installed.
StreamURLFetcher can fetch direct video URLs without any additional network requests (that's really fast).
Call `get` or `getAll` method of StreamURLFetcher & pass response returned by `Video.get` or `Video.getFormats` as parameter to fetch direct URLs.
Getting URLs or downloading streams using youtube-dl or PyTube is can be a slow, because of the fact that they make requests to fetch the same content, which one might have already recieved at the time of showing it to the user etc.
StreamURLFetcher makes use of PyTube (if installed) & makes some slight improvements to functioning of PyTube.
Avoid instantiating StreamURLFetcher more than once, it will be slow (making global object of the class will be a recommended solution).
`get` method can be handy for getting URL of a particular kind. `getAll` returns all stream URLs in a dictionary.
'''
fetcher = StreamURLFetcher()
'''
Call this method after instanciating StreamURLFetcher & avoid calling more than once.
'''
await fetcher.getJavaScript()
'''
Get video information.
'''
videoA = await Video.get("https://www.youtube.com/watch?v=aqz-KE-bpKQ")
videoB = await Video.get("https://www.youtube.com/watch?v=ZwNxYJfW-eU")
'''
Get direct stream URLs without any web requests.
'''
singleUrlA = await fetcher.get(videoA, 22)
allUrlsB = await fetcher.getAll(videoB)
print(singleUrlA)
print(allUrlsB)
comments = Comments("_ZdsmLgCVdU")
await comments.getNextComments()
print(len(comments.comments["result"]))
while len(comments.comments["result"]) < 100:
await comments.getNextComments()
print(len(comments.comments["result"]))
print("Found all comments")
print(await Transcript.get("https://www.youtube.com/watch?v=L7kF4MXXCoA"))
url = "https://www.youtube.com/watch?v=-1xu0IP35FI"
transcript_en = await Transcript.get(url)
# you actually don't have to pass a valid URL in following Transcript call. You can input an empty string, but I do recommend still inputing a valid URL.
transcript_2 = await Transcript.get(url, transcript_en["languages"][-1]["params"]) # in my case, it'd output Spanish.
print(transcript_2)
print(await Channel.get("UC_aEa8K-EOJ3D6gOs7HcyNg"))
# Retrieve playlists of a channel
channel = Channel("UC_aEa8K-EOJ3D6gOs7HcyNg")
await channel.init()
print(len(channel.result["playlists"]))
while channel.has_more_playlists():
await channel.next()
print(len(channel.result["playlists"]))
'''
You may add/omit the optional parameters according to your requirement & use case.
'''
'''
Thanks for your support & love!
- github.com/alexmercerind
'''
if __name__ == '__main__':
asyncio.run(main())