-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutilities.py
440 lines (382 loc) · 15.6 KB
/
utilities.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright 2018-2020 Twixes
# This file is part of Somsiad - the Polish Discord bot.
# Somsiad is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# Somsiad is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with Somsiad.
# If not, see <https://www.gnu.org/licenses/>.
import calendar
import datetime as dt
import locale
from math import ceil
import os
import re
from dataclasses import dataclass
from numbers import Number
from typing import Optional, Sequence, Tuple, Union
import numpy as np
from googleapiclient.discovery import build
AI_ALLOWED_SERVER_IDS = [276488080914120704, 294182757209473024, 479458694354960385, 682561082719731742]
@dataclass
class DatetimeFormat:
format: str
@property
def imply_year(self) -> bool:
return '%Y' not in self.format
@property
def imply_month(self) -> bool:
return '%m' not in self.format
@property
def imply_day(self) -> bool:
return '%d' not in self.format
DHMS_REGEX = re.compile(
r'^(?!$)(?:(?P<days>(?:\d+(?:[.,]\d*)?)|(?:[.,]\d*))d)?'
r'(?:(?P<hours>(?:\d+(?:[.,]\d*)?)|(?:[.,]\d*))[hg])?'
r'(?:(?P<minutes>(?:\d+(?:[.,]\d*)?)|(?:[.,]\d*))m(?:in)?)?'
r'(?:(?P<seconds>(?:\d+(?:[.,]\d*)?)|(?:[.,]\d*))s(?:ec)?)?$'
)
DATETIME_FORMATS = (
DatetimeFormat('%d.%m.%YT%H.%M'),
DatetimeFormat('%d.%m.%yT%H.%M'),
DatetimeFormat('%d.%mT%H.%M'),
DatetimeFormat('%dT%H.%M'),
DatetimeFormat('%Y.%m.%dT%H.%M'),
DatetimeFormat('%H.%M'),
)
URL_REGEX = re.compile(r'(https?:\/\/\S+\.\S+)')
URL_REGEX_PROTOCOL_SEPARATE = re.compile(r'(?:(\w+):\/\/)?(\S+\.\S+)')
class GoogleClient:
FOOTER_TEXT = 'Google'
FOOTER_ICON_URL = 'https://www.google.com/favicon.ico'
@dataclass
class GoogleResult:
title: str
snippet: Optional[str]
source_link: str
display_link: str
root_link: str
image_link: Optional[str]
type: Optional[str]
def __init__(self, developer_key: str, custom_search_engine_id: str):
self.custom_search_engine_id = custom_search_engine_id
self.search_client = build('customsearch', 'v1', developerKey=developer_key).cse()
def search(
self, query: str, *, language: str = 'pl', safe: str = 'active', search_type: str = None
) -> Optional[GoogleResult]:
list_query = self.search_client.list(
q=query,
cx=self.custom_search_engine_id,
hl=language,
num=5 if search_type == 'image' else 1,
safe=safe,
searchType=search_type,
)
results = list_query.execute()
if results['searchInformation']['totalResults'] != '0':
if search_type == 'image':
for result in results['items']:
if not result['link'] or not result['link'].startswith('http'):
continue
return self.GoogleResult(
result['title'],
result.get('snippet'),
result['image']['contextLink'],
result['displayLink'],
f'{result["link"].split("://")[0]}://{result["displayLink"]}',
result['link'],
search_type,
)
else:
result = results['items'][0]
try:
image_link = result['pagemap']['cse_image'][0]['src']
if not image_link.startswith('http'):
raise ValueError
except (KeyError, ValueError):
image_link = None
return self.GoogleResult(
result['title'],
result.get('snippet'),
result['link'],
result['displayLink'],
f'{result["link"].split("://")[0]}://{result["displayLink"]}',
image_link,
search_type,
)
return None
class YouTubeClient:
FOOTER_ICON_URL = (
'https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/'
'YouTube_full-color_icon_%282017%29.svg/60px-YouTube_full-color_icon_%282017%29.svg.png'
)
FOOTER_TEXT = 'YouTube'
@dataclass
class YouTubeResult:
id: str
title: str
url: str
thumbnail_url: str
def __init__(self, developer_key: str):
self.search_client = build('youtube', 'v3', developerKey=developer_key).search()
def search(self, query: str) -> Optional[YouTubeResult]:
list_query = self.search_client.list(q=query, part='snippet', maxResults=1, type='video')
response = list_query.execute()
items = response.get('items')
if not items:
return None
video_id = items[0]['id']['videoId']
return self.YouTubeResult(
video_id,
items[0]['snippet']['title'],
f'https://www.youtube.com/watch?v={video_id}',
items[0]['snippet']['thumbnails']['medium']['url'],
)
def first_url(string: str, *, protocol_separate: bool = False) -> Union[str, Tuple[Optional[str], Optional[str]]]:
"""Return the first well-formed URL found in the string."""
if protocol_separate:
search_result = URL_REGEX_PROTOCOL_SEPARATE.search(string)
if search_result is None:
return None, None
else:
return tuple(
(
part.lower().rstrip('()[{]};:\'",<.>') if part is not None else None
for part in search_result.groups()
)
)
else:
search_result = URL_REGEX.search(string)
return search_result.group().rstrip('()[{]};:\'",<.>') if search_result is not None else None
def text_snippet(text: str, limit: int) -> str:
"""Return the text limited in length to the specified number of characters."""
if not text:
return ''
stripped_text = text.strip()
if len(stripped_text) <= limit:
return stripped_text
words = stripped_text.split(' ')
if limit > len(words[0]):
cut_text = words[0]
for word in words[1:]:
if len(cut_text) + 1 + len(word) < limit:
cut_text += ' ' + word
else:
break
return cut_text.rstrip(',') + '…'
return '…'
def with_preposition_form(number: Union[int, float]) -> str:
"""Return the gramatically correct form of the "with" preposition in Polish."""
while number > 1000:
number /= 1000
return 'ze' if 100 <= number < 200 else 'z'
def word_number_form(
number: Union[int, float, str],
singular_form: str,
plural_form: str,
plural_form_5_to_1: str = None,
fractional_form: str = None,
*,
include_number: bool = True,
include_with: bool = False,
) -> str:
"""Return the gramatically correct form of the specifiec word in Polish based on the number."""
if isinstance(number, str):
return f'{number} {plural_form_5_to_1 or plural_form}'
absolute_number = abs(number)
absolute_number_floored = int(absolute_number)
if fractional_form is not None and absolute_number != absolute_number_floored:
proper_form = fractional_form
else:
absolute_number = absolute_number_floored
if absolute_number == 1:
proper_form = singular_form
elif absolute_number % 10 in (2, 3, 4) and absolute_number % 100 not in (12, 13, 14):
proper_form = plural_form
else:
if plural_form_5_to_1 is not None:
proper_form = plural_form_5_to_1
else:
proper_form = plural_form
parts = []
if include_with:
parts.append(with_preposition_form(number))
if include_number:
parts.append(f'{number:n}')
parts.append(proper_form)
return ' '.join(parts)
def join_using_and(elements: Sequence[str]) -> str:
if not elements:
return "brak"
if len(elements) == 1:
return elements[0]
return ", ".join(elements[:-1]) + " i " + elements[-1]
def utc_to_naive_local(datetime: dt.datetime) -> dt.datetime:
if datetime.tzinfo == dt.timezone.utc:
return datetime.astimezone().replace(tzinfo=None)
elif datetime.tzinfo is None:
return datetime.replace(tzinfo=dt.timezone.utc).astimezone().replace(tzinfo=None)
else:
raise ValueError('datetime is neither naive nor UTC-aware')
def human_datetime(
datetime: Optional[dt.datetime] = None,
*,
utc: bool = False,
date: bool = True,
time: bool = True,
days_difference: bool = True,
name_month: bool = True,
now_override: dt.datetime = None,
) -> str:
"""Return the difference between the provided datetime and now in Polish."""
if datetime is None:
datetime = dt.datetime.now()
else:
datetime = datetime if not utc else utc_to_naive_local(datetime)
time_difference_parts = []
if date or time:
datetime_parts = []
if date:
if name_month:
datetime_parts.append(datetime.strftime('%-d %B %Y'))
else:
datetime_parts.append(datetime.strftime('%-d.%m.%Y'))
if time:
datetime_parts.append(datetime.strftime('%-H:%M'))
time_difference_parts.append(' o '.join(datetime_parts))
if days_difference:
timedelta = datetime.date() - (dt.date.today() if now_override is None else now_override.date())
if timedelta.days == -2:
time_difference_parts.append('przedwczoraj')
elif timedelta.days == -1:
time_difference_parts.append('wczoraj')
elif timedelta.days == 0:
time_difference_parts.append('dzisiaj')
elif timedelta.days == 1:
time_difference_parts.append('jutro')
elif timedelta.days == 2:
time_difference_parts.append('pojutrze')
elif timedelta.days < 0:
time_difference_parts.append(f'{word_number_form(-timedelta.days, "dzień", "dni")} temu')
else:
time_difference_parts.append(f'za {word_number_form(timedelta.days, "dzień", "dni")}')
return ', '.join(time_difference_parts)
def human_amount_of_time(time: Union[dt.timedelta, int, float]) -> str:
"""Return the provided amoutt of in Polish."""
if isinstance(time, dt.timedelta):
total_seconds = int(ceil(time.total_seconds()))
elif isinstance(time, (int, float)):
total_seconds = int(ceil(time))
else:
raise TypeError('time must be datetime.timedelta or numbers.Number')
if total_seconds == 0.0:
return '0 s'
days = total_seconds // 86400
total_seconds -= days * 86400
hours = total_seconds // 3600
total_seconds -= hours * 3600
minutes = total_seconds // 60
total_seconds -= minutes * 60
seconds = total_seconds
information = []
if days >= 1:
information.append(f'{days:n} d')
if hours >= 1:
information.append(f'{hours:n} h')
if minutes >= 1:
information.append(f'{minutes:n} min')
if seconds >= 1:
information.append(f'{seconds:n} s')
return ' '.join(information)
def days_as_weeks(number_of_days: int, none_if_no_weeks: bool = True) -> Optional[str]:
number_of_weeks, number_of_leftover_days = divmod(number_of_days, 7)
if number_of_weeks == 0:
return None if none_if_no_weeks else word_number_form(number_of_leftover_days, 'dzień', 'dni')
elif number_of_leftover_days == 0:
return word_number_form(number_of_weeks, 'tydzień', 'tygodnie', 'tygodni')
else:
return (
f'{word_number_form(number_of_weeks, "tydzień", "tygodnie", "tygodni")} '
f'i {word_number_form(number_of_leftover_days, "dzień", "dni")}'
)
def interpret_str_as_datetime(
string: str, roll_over: bool = True, now_override: dt.datetime = None, years_in_future_limit: Optional[int] = 5
) -> dt.datetime:
"""Interpret the provided string as a datetime."""
string = string.replace(',', '.')
if string.endswith('.'):
raise ValueError # Ignore strings ending with a dot or comma to prevent ordinals being misinterpreted
timedelta_arguments = {}
# Strategy 1: string as number of minutes
try:
timedelta_arguments['minutes'] = float(string)
except ValueError:
pass
# Strategy 2: string as custom 'dhms' format
match = DHMS_REGEX.match(string)
if match is not None:
for group, value in match.groupdict().items():
if value is None:
continue
try:
timedelta_arguments[group] = float(value)
except ValueError:
timedelta_arguments.clear()
break
now = now_override or dt.datetime.now()
if any(timedelta_arguments.values()):
datetime = now + dt.timedelta(**timedelta_arguments)
if years_in_future_limit is not None and datetime > now.replace(year=now.year + years_in_future_limit):
raise ValueError
else:
# Strategy 3: string as datetime
string = string.replace('-', '.').replace('/', '.').replace(':', '.').strip('T')
for datetime_format in DATETIME_FORMATS:
try:
datetime = dt.datetime.strptime(string, datetime_format.format)
except ValueError:
continue
else:
if datetime_format.imply_day:
datetime = datetime.replace(day=now.day)
if datetime_format.imply_month:
datetime = datetime.replace(month=now.month)
if datetime_format.imply_year:
datetime = datetime.replace(year=now.year)
if roll_over and datetime < now:
# if implied date is in the past, roll it over so it's not
if datetime_format.imply_day:
datetime += dt.timedelta(1)
elif datetime_format.imply_month:
if now.month == 12:
datetime = datetime.replace(year=now.year + 1, month=1)
else:
datetime = datetime.replace(month=now.month + 1)
elif datetime_format.imply_year:
datetime = datetime.replace(year=now.year + 1)
break
else:
raise ValueError
return datetime
def calculate_age(birth_date: dt.date, at_date: Optional[dt.date] = None) -> int:
at_date = at_date or dt.date.today()
age = at_date.year - birth_date.year
if (at_date.month, at_date.day) < (birth_date.month, birth_date.day):
age -= 1
elif (2, 29) == (at_date.month, at_date.day) == (birth_date.month, birth_date.day):
age //= 4
return age
def md_link(text: str, url: Optional[str], *, unroll=True) -> str:
if not unroll:
url = f'<{url}>' if url else ''
return f'[{text}]({url})' if url else text
def rolling_average(data: Sequence[Number], roll: int, pad_mode: str = 'constant') -> np.ndarray:
data_np = np.pad(data, roll // 2, pad_mode)
data_np = np.cumsum(data_np)
data_np[roll:] = data_np[roll:] - data_np[:-roll]
result = data_np[roll - 1 :] / roll
return result
def localize():
"""Set program locale and first day of the week."""
locale.setlocale(locale.LC_ALL, os.getenv('LC_ALL'))
calendar.setfirstweekday(calendar.MONDAY)