This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
syncExample.py
256 lines (165 loc) · 7.59 KB
/
syncExample.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
from youtubesearchpython import *
'''
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.
'''
allSearch = Search('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
print(allSearch.result())
'''
Searches only for videos in YouTube.
'''
videosSearch = VideosSearch('NoCopyrightSounds', limit = 10, language = 'en', region = 'US')
print(videosSearch.result(mode = ResultMode.json))
'''
Searches only for channels in YouTube.
'''
channelsSearch = ChannelsSearch('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
'''
Setting mode = ResultMode.dict for getting dictionary result instead of JSON. Default is ResultMode.json.
'''
print(channelsSearch.result(mode = ResultMode.json))
'''
Searches only for playlists in YouTube.
'''
playlistsSearch = PlaylistsSearch('NoCopyrightSounds', limit = 1, language = 'en', region = 'US')
print(playlistsSearch.result())
'''
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')
print(customSearch.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
for video in search.result()['result']:
print(str(index) + ' - ' + video['title'])
index += 1
'''Getting result on 2nd page.'''
search.next()
for video in search.result()['result']:
print(str(index) + ' - ' + video['title'])
index += 1
'''Getting result on 3rd page.'''
search.next()
for video in search.result()['result']:
print(str(index) + ' - ' + video['title'])
index += 1
'''
Getting information about playlist or videos in it using its link.
`Playlist.get` method will give both information & formats of the playlist
`Playlist.getInfo` method will give only information about the playlist.
`Playlist.getVideos` method will give only videos in the playlist.
'''
playlist = Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
print(playlist)
playlistInfo = Playlist.getInfo('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
print(playlistInfo)
playlistVideos = Playlist.getVideos('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(playlistVideos)
'''
More tests to buggy Playlist class
'''
playlist = Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
print(playlist)
playlist = Playlist.get('https://www.youtube.com/watch?v=bplUXwTTgbI&list=PL6edxAMqu2xfxgbf7Q09hSg1qCMfDI7IZ', mode = ResultMode.json)
print(playlist)
playlist = Playlist('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
print(f'Videos Retrieved: {len(playlist.videos)}')
while playlist.hasMoreVideos:
print('Getting more videos...')
playlist.getNextVideos()
print(f'Videos Retrieved: {len(playlist.videos)}')
print('Found all the videos.')
'''
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.
YouTube doesn't provide uploadDate and publishDate in its InnerTube API, thus we have to use HTML requests to get it.
This is disabled by default as it is very inefficient, but if you really need it, you can explicitly set parameter to Video class: enableHTML=True
'''
video = Video.get('https://www.youtube.com/watch?v=z0GKGpObgPY', mode = ResultMode.json, get_upload_date=True)
print(video)
videoInfo = Video.getInfo('https://youtu.be/z0GKGpObgPY', mode = ResultMode.json)
print(videoInfo)
videoFormats = Video.getFormats('z0GKGpObgPY')
print(videoFormats)
'''
Getting search suggestions from YouTube.
You may show search suggestions to users before making any search.
'''
suggestions = Suggestions(language = 'en', region = 'US')
print(suggestions.get('NoCopyrightSounds', mode = ResultMode.json))
'''
Getting videos by hashtag.
'''
hashtag = Hashtag('ncs', limit = 1)
print(hashtag.result())
channel = ChannelSearch("Watermelon Sugar", "UCZFWPqqPkFlNwIxcpsLOwew")
print(channel.result(mode=ResultMode.json))
'''
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.
'''
'''
Instantiate the class (do it only once).
'''
fetcher = StreamURLFetcher()
'''
Get video information.
'''
videoA = Video.get("https://www.youtube.com/watch?v=aqz-KE-bpKQ")
videoB = Video.get("https://www.youtube.com/watch?v=ZwNxYJfW-eU")
'''
Get direct stream URLs without any web requests.
'''
singleUrlA = fetcher.get(videoA, 22)
allUrlsB = fetcher.getAll(videoB)
print(singleUrlA)
print(allUrlsB)
comments = Comments("_ZdsmLgCVdU")
print(len(comments.comments["result"]))
while len(comments.comments["result"]) < 100:
comments.getNextComments()
print(len(comments.comments["result"]))
print("Found all comments")
print(Transcript.get("https://www.youtube.com/watch?v=L7kF4MXXCoA"))
url = "https://www.youtube.com/watch?v=-1xu0IP35FI"
transcript_en = 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 = Transcript.get(url, transcript_en["languages"][-1]["params"]) # in my case, it'd output Spanish.
print(transcript_2)
print(Channel.get("UC_aEa8K-EOJ3D6gOs7HcyNg"))
# Retrieve playlists of a channel
channel = Channel("UC_aEa8K-EOJ3D6gOs7HcyNg")
print(len(channel.result["playlists"]))
while channel.has_more_playlists():
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
'''