-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapetube.py
256 lines (207 loc) · 8.28 KB
/
scrapetube.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
import json
import time
from typing import Generator
import requests
from typing_extensions import Literal
type_property_map = {
"videos": "videoRenderer",
"streams": "videoRenderer",
"shorts": "reelItemRenderer",
}
def get_channel(
channel_id: str = "",
channel_url: str = "",
limit: int = 0,
sleep: int = 1,
sort_by: Literal["newest", "oldest", "popular"] = "newest",
content_type: Literal["videos", "shorts", "streams"] = "videos",
) -> Generator[dict, None, None]:
"""Get videos for a channel.
Parameters:
channel_id (``str``, *optional*):
The channel id from the channel you want to get the videos for.
If you prefer to use the channel url instead, see ``channel_url`` below.
channel_url (``str``, *optional*):
The url to the channel you want to get the videos for.
Since there is a few type's of channel url's, you can use the one you want
by passing it here instead of using ``channel_id``.
limit (``int``, *optional*):
Limit the number of videos you want to get.
sleep (``int``, *optional*):
Seconds to sleep between API calls to youtube, in order to prevent getting blocked.
Defaults to 1.
sort_by (``str``, *optional*):
In what order to retrieve to videos. Pass one of the following values.
``"newest"``: Get the new videos first.
``"oldest"``: Get the old videos first.
``"popular"``: Get the popular videos first. Defaults to "newest".
content_type (``str``, *optional*):
In order to get content type. Pass one of the following values.
``"videos"``: Videos
``"shorts"``: Shorts
``"streams"``: Streams
"""
sort_by_map = {"newest": "dd", "oldest": "da", "popular": "p"}
url = "{url}/{content_type}?view=0&sort={sort_by}&flow=grid".format(
url=channel_url or f"https://www.youtube.com/channel/{channel_id}",
content_type=content_type,
sort_by=sort_by_map[sort_by],
)
api_endpoint = "https://www.youtube.com/youtubei/v1/browse"
videos = get_videos(
url, api_endpoint, type_property_map[content_type], limit, sleep
)
for video in videos:
yield video
def get_playlist(
playlist_id: str, limit: int = 0, sleep: int = 1
) -> Generator[dict, None, None]:
"""Get videos for a playlist.
Parameters:
playlist_id (``str``):
The playlist id from the playlist you want to get the videos for.
limit (``int``, *optional*):
Limit the number of videos you want to get.
sleep (``int``, *optional*):
Seconds to sleep between API calls to youtube, in order to prevent getting blocked.
Defaults to 1.
"""
url = f"https://www.youtube.com/playlist?list={playlist_id}"
api_endpoint = "https://www.youtube.com/youtubei/v1/browse"
videos = get_videos(url, api_endpoint, "playlistVideoRenderer", limit, sleep)
for video in videos:
yield video
def get_search(
query: str,
limit: int = 0,
sleep: int = 1,
sort_by: Literal["relevance", "upload_date", "view_count", "rating"] = "relevance",
results_type: Literal["video", "channel", "playlist", "movie"] = "video",
) -> Generator[dict, None, None]:
"""Search youtube and get videos.
Parameters:
query (``str``):
The term you want to search for.
limit (``int``, *optional*):
Limit the number of videos you want to get.
sleep (``int``, *optional*):
Seconds to sleep between API calls to youtube, in order to prevent getting blocked.
Defaults to 1.
sort_by (``str``, *optional*):
In what order to retrieve to videos. Pass one of the following values.
``"relevance"``: Get the new videos in order of relevance.
``"upload_date"``: Get the new videos first.
``"view_count"``: Get the popular videos first.
``"rating"``: Get videos with more likes first.
Defaults to "relevance".
results_type (``str``, *optional*):
What type you want to search for. Pass one of the following values:
``"video"|"channel"|"playlist"|"movie"``. Defaults to "video".
"""
sort_by_map = {
"relevance": "A",
"upload_date": "I",
"view_count": "M",
"rating": "E",
}
results_type_map = {
"video": ["B", "videoRenderer"],
"channel": ["C", "channelRenderer"],
"playlist": ["D", "playlistRenderer"],
"movie": ["E", "videoRenderer"],
}
param_string = f"CA{sort_by_map[sort_by]}SAhA{results_type_map[results_type][0]}"
url = f"https://www.youtube.com/results?search_query={query}&sp={param_string}"
api_endpoint = "https://www.youtube.com/youtubei/v1/search"
videos = get_videos(
url, api_endpoint, results_type_map[results_type][1], limit, sleep
)
for video in videos:
yield video
def get_videos(
url: str, api_endpoint: str, selector: str, limit: int, sleep: int
) -> Generator[dict, None, None]:
session = requests.Session()
session.headers[
"User-Agent"
] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"
is_first = True
quit = False
count = 0
while True:
if is_first:
html = get_initial_data(session, url)
client = json.loads(
get_json_from_html(html, "INNERTUBE_CONTEXT", 2, '"}},') + '"}}'
)["client"]
api_key = get_json_from_html(html, "innertubeApiKey", 3)
session.headers["X-YouTube-Client-Name"] = "1"
session.headers["X-YouTube-Client-Version"] = client["clientVersion"]
data = json.loads(
get_json_from_html(html, "var ytInitialData = ", 0, "};") + "}"
)
next_data = get_next_data(data)
is_first = False
else:
data = get_ajax_data(session, api_endpoint, api_key, next_data, client)
next_data = get_next_data(data)
for result in get_videos_items(data, selector):
try:
count += 1
yield result
if count == limit:
quit = True
break
except GeneratorExit:
quit = True
break
if not next_data or quit:
break
time.sleep(sleep)
session.close()
def get_initial_data(session: requests.Session, url: str) -> str:
session.cookies.set("CONSENT", "YES+cb", domain=".youtube.com")
response = session.get(url)
html = response.text
return html
def get_ajax_data(
session: requests.Session,
api_endpoint: str,
api_key: str,
next_data: dict,
client: dict,
) -> dict:
data = {
"context": {"clickTracking": next_data["click_params"], "client": client},
"continuation": next_data["token"],
}
response = session.post(api_endpoint, params={"key": api_key}, json=data)
return response.json()
def get_json_from_html(html: str, key: str, num_chars: int = 2, stop: str = '"') -> str:
pos_begin = html.find(key) + len(key) + num_chars
pos_end = html.find(stop, pos_begin)
return html[pos_begin:pos_end]
def get_next_data(data: dict) -> dict:
raw_next_data = next(search_dict(data, "continuationEndpoint"), None)
if not raw_next_data:
return dict()
next_data = {
"token": raw_next_data["continuationCommand"]["token"],
"click_params": {"clickTrackingParams": raw_next_data["clickTrackingParams"]},
}
return next_data
def search_dict(partial: dict, search_key: str) -> Generator[dict, None, None]:
stack = [partial]
while stack:
current_item = stack.pop(0)
if isinstance(current_item, dict):
for key, value in current_item.items():
if key == search_key:
yield value
else:
stack.append(value)
elif isinstance(current_item, list):
for value in current_item:
stack.append(value)
def get_videos_items(data: dict, selector: str) -> Generator[dict, None, None]:
return search_dict(data, selector)