Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/utils/voice.py
327 views
1
import re
2
import sys
3
import time as pytime
4
from datetime import datetime
5
from time import sleep
6
7
from cleantext import clean
8
from requests import Response
9
10
from utils import settings
11
12
if sys.version_info[0] >= 3:
13
from datetime import timezone
14
15
16
def check_ratelimit(response: Response) -> bool:
17
"""
18
Checks if the response is a ratelimit response.
19
If it is, it sleeps for the time specified in the response.
20
"""
21
if response.status_code == 429:
22
try:
23
time = int(response.headers["X-RateLimit-Reset"])
24
print(f"Ratelimit hit. Sleeping for {time - int(pytime.time())} seconds.")
25
sleep_until(time)
26
return False
27
except KeyError: # if the header is not present, we don't know how long to wait
28
return False
29
30
return True
31
32
33
def sleep_until(time) -> None:
34
"""
35
Pause your program until a specific end time.
36
'time' is either a valid datetime object or unix timestamp in seconds (i.e. seconds since Unix epoch)
37
"""
38
end = time
39
40
# Convert datetime to unix timestamp and adjust for locality
41
if isinstance(time, datetime):
42
# If we're on Python 3 and the user specified a timezone, convert to UTC and get the timestamp.
43
if sys.version_info[0] >= 3 and time.tzinfo:
44
end = time.astimezone(timezone.utc).timestamp()
45
else:
46
zoneDiff = pytime.time() - (datetime.now() - datetime(1970, 1, 1)).total_seconds()
47
end = (time - datetime(1970, 1, 1)).total_seconds() + zoneDiff
48
49
# Type check
50
if not isinstance(end, (int, float)):
51
raise Exception("The time parameter is not a number or datetime object")
52
53
# Now we wait
54
while True:
55
now = pytime.time()
56
diff = end - now
57
58
#
59
# Time is up!
60
#
61
if diff <= 0:
62
break
63
else:
64
# 'logarithmic' sleeping to minimize loop iterations
65
sleep(diff / 2)
66
67
68
def sanitize_text(text: str) -> str:
69
r"""Sanitizes the text for tts.
70
What gets removed:
71
- following characters`^_~@!&;#:-%“”‘"%*/{}[]()\|<>?=+`
72
- any http or https links
73
74
Args:
75
text (str): Text to be sanitized
76
77
Returns:
78
str: Sanitized text
79
"""
80
81
# remove any urls from the text
82
regex_urls = r"((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*"
83
84
result = re.sub(regex_urls, " ", text)
85
86
# note: not removing apostrophes
87
regex_expr = r"\s['|’]|['|’]\s|[\^_~@!&;#:\-%—“”‘\"%\*/{}\[\]\(\)\\|<>=+]"
88
result = re.sub(regex_expr, " ", result)
89
result = result.replace("+", "plus").replace("&", "and")
90
91
# emoji removal if the setting is enabled
92
if settings.config["settings"]["tts"]["no_emojis"]:
93
result = clean(result, no_emoji=True)
94
95
# remove extra whitespace
96
return " ".join(result.split())
97
98