-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
288 lines (228 loc) · 8.99 KB
/
utils.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import pathlib
import warnings
from typing import Optional, Any
from urllib import parse
def extract_video_id(url: str) -> Optional[str]:
"""
This should work for every url listed here:
https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486#file-activeyoutubeurlformats-txt
and more such as i.ytimg.com urls.
Args:
url (str): The url to strip the id from.
Returns:
Optional[str]: The video id with the rest of the url removed.
"""
components = parse.urlparse(url.replace("&", "?", 1) if "?" not in url else url)
queries = parse.parse_qs(components.query)
encoded_query_matches = {'u', 'url'}.intersection(set(queries.keys()))
if 'v' in queries:
return queries["v"][0]
elif encoded_query_matches:
return extract_video_id(parse.unquote(queries[encoded_query_matches.pop()][0]))
elif components.hostname.endswith("ytimg.com"):
return pathlib.Path(components.path).parts[2]
elif pathlib.Path(components.path).name not in ["playlist"]:
return pathlib.Path(components.path).name
def extract_playlist_id(url: str) -> Optional[str]:
"""
This should work for every url listed here:
https://github.com/Revnoplex/ayt-api/blob/main/test-playlist-urls.txt
Don't expect this to work on YouTube mixes.
Args:
url (str): The url to strip the id from.
Returns:
Optional[str]: The playlist id with the rest of the url remove.
"""
components = parse.urlparse(url.replace("&", "?", 1) if "?" not in url else url)
queries = parse.parse_qs(components.query)
encoded_query_matches = {'u', 'url'}.intersection(set(queries.keys()))
if 'list' in queries:
return queries["list"][0]
elif encoded_query_matches:
return extract_playlist_id(parse.unquote(queries[encoded_query_matches.pop()][0]))
def extract_channel_id(url: str) -> Optional[str]:
"""
This should work for every url listed here:
https://github.com/Revnoplex/ayt-api/blob/main/test-channel-urls.txt
Args:
url (str): The url to strip the id from.
Returns:
Optional[str]: The channel id with the rest of the url removed.
"""
components = parse.urlparse(url.replace("&", "?", 1) if "?" not in url else url)
queries = parse.parse_qs(components.query)
encoded_query_matches = {'u', 'url'}.intersection(set(queries.keys()))
if encoded_query_matches:
return extract_channel_id(parse.unquote(queries[encoded_query_matches.pop()][0]))
else:
return pathlib.Path(components.path).name
def extract_comment_id(url: str) -> Optional[str]:
"""
This should work for every url listed here:
https://github.com/Revnoplex/ayt-api/blob/main/test-comment-urls.txt
Args:
url (str): The url to strip the id from.
Returns:
Optional[str]: The comment id with the rest of the url removed.
"""
components = parse.urlparse(url.replace("&", "?", 1) if "?" not in url else url)
queries = parse.parse_qs(components.query)
encoded_query_matches = {'u', 'url'}.intersection(set(queries.keys()))
if 'lc' in queries:
return queries["lc"][0]
elif encoded_query_matches:
return extract_comment_id(parse.unquote(queries[encoded_query_matches.pop()][0]))
def id_str_to_int(youtube_id: str) -> int:
"""Converts a base 64 YouTube ID string into an integer.
Args:
youtube_id (str): The YouTube ID as a base 64 string.
Returns:
int: The YouTube ID as an integer.
Raises:
ValueError: There were invalid characters in the YouTube ID.
"""
number = 0
last_chars = ["-", "_"]
for idx, char in enumerate(reversed(youtube_id)):
ord_var = ord(char)
if 65 <= ord_var <= 90:
number += (ord_var - 65) * 64 ** idx
elif 97 <= ord_var <= 122:
number += (ord_var - 71) * 64 ** idx
elif 48 <= ord_var <= 57:
number += (ord_var + 4) * 64 ** idx
elif char in last_chars:
number += (62 + last_chars.index(char)) * 64 ** idx
else:
raise ValueError(f"Invalid YouTube ID character: {char}")
return number
def camel_to_snake(string: str) -> str:
"""Converts words in the camel case convention to the snake case convention.
e.g. Converts ``fooBar`` to ``foo_bar``.
Args:
string (str): The words in the camel case convention.
Returns:
str: The words in the snake case convention.
"""
snake_string = ""
for char in string:
if char.isupper():
snake_string += "_" + char.lower()
else:
snake_string += char
return snake_string
def snake_to_camel(string: str) -> str:
"""Converts words in the snake case convention to the camel case convention.
e.g. Converts ``foo_bar`` to ``fooBar``.
Args:
string (str): The words in the snake case convention.
Returns:
str: The words in the camel case convention.
"""
camel_string = ""
capitalise = False
for char in string:
if char == "_":
capitalise = True
elif capitalise:
camel_string += char.upper()
capitalise = False
else:
camel_string += char
return camel_string[0].lower() + camel_string[1:]
def snake_keys(dictionary: dict) -> dict:
"""Converts keys in a dictionary from camel case to snake case.
Args:
dictionary (dict): The dictionary with keys using the camel case convention.
Returns:
dict: The dictionary with keys using the snake case convention.
"""
snake_dict = {}
for key, value in dictionary.items():
snake_dict[camel_to_snake(key)] = value
return snake_dict
def censor_key(call_url: str) -> str:
"""Censors the api key in an api call url.
Args:
call_url (str): The api call url containing the uncensored api key.
Returns:
str: The url with the api key censored.
"""
components = parse.urlparse(call_url)
queries = parse.parse_qs(components.query)
if "key" in queries:
queries["key"] = ["API_KEY"]
censored_components = components._replace(query=parse.urlencode(queries, doseq=True))
return censored_components.geturl()
def censor_token(call_url: str) -> str:
"""Alias of censor_key
.. deprecated:: 0.4.0
Use :func:`censor_key` instead
Args:
call_url (str): The api call url containing the uncensored api key.
Returns:
str: The url with the api key censored.
"""
warnings.warn(
"censor_token is deprecated since 0.4.0 and is scheduled "
"for removal in a later release. Use censor_key instead.",
DeprecationWarning
)
return censor_key(call_url)
def basic_html_page(title: str, description: str) -> str:
"""
Builds a basic html page
.. versionadded:: 0.4.0
This is used in :func:`ayt_api.api.AsyncYoutubeAPI.with_oauth_flow_generator`
Args:
title (str): The title and heading for the page
description (str): The description that will be displayed on the page
Returns:
str: The html page
"""
return f"""\
<!doctype html>
<html lang="en">
<head>
<title>{title}</title>
<link rel="icon" href="https://ayt-api.revnoplex.xyz/ayt-api-square.svg">
<link rel="stylesheet" type="text/css" href="https://revnoplex.xyz/css/main.css">
</head>
<body>
<h1>{title}</h1>
<p>{description}.</p>
</body>
</html>\
"""
def use_existing(existing_value: Any, argument: Any) -> Any:
"""
A check used in the updated functions to decide when to use the existing value if the argument has a value of
``EXISTING`` or use the value of the argument.
.. versionadded:: 0.4.0
Args:
existing_value (Any): The existing value that will be used if ``argument`` is ``EXISTING``.
argument (Any): The value to overwrite ``existing_value`` if not ``EXISTING``.
Returns:
Any: The existing value or argument.
"""
from ayt_api.types import EXISTING
return existing_value if argument is EXISTING else argument
def ensure_missing_keys(original: dict, minimised: dict) -> dict:
"""
Ensure a dictionary with possible missing keys from the first dictionary includes them if the value for the key
was ``None`` or an empty value.
.. versionadded:: 0.4.0
Note:
This util will only check the first layer of keys and will not check any deeper nested keys.
Args:
original (dict): The original dictionary with the full set of keys.
minimised (dict): The version of the dictionary that had keys with empty values removed.
Returns:
dict: The ``minimised`` version of the dictionary with values added back from the original depending on if they
were empty values.
"""
updated = minimised.copy()
for key, value in original.items():
if key not in minimised and (not value):
updated[key] = value
return updated