-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
154 lines (112 loc) · 3.91 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
from datetime import datetime as dt, timedelta
from decimal import Decimal
from enum import Enum
import traceback
import threading
import requests
import math
import pytz
import re
class BaseEnum(str, Enum):
@classmethod
def value_of(cls, value):
for k, v in cls.__members__.items():
if k == value or v == value:
return v
else:
raise ValueError(f"{cls.__name__} enum not found for {value}")
def send_post_ss(session: requests.Session, url, data, files=None):
try:
response = session.post(url, data, files=files)
try:
return response.json()
finally:
response.close()
except Exception:
traceback.print_exc()
def send_tg(session: requests.Session,
bot_token: str,
chat_id: str,
text: str,
parse_mode: str = None,
send_async: bool = True):
url = "https://api.telegram.org/bot" + bot_token + "/sendMessage"
data = {
"chat_id": chat_id,
"text": text,
}
if parse_mode is not None:
data["parse_mode"] = parse_mode
if send_async:
threading.Thread(target=send_post_ss, args=(session, url, data)).start()
else:
return send_post_ss(session, url, data)
def send_document(session: requests.Session,
bot_token: str,
chat_id: str,
document: bytes,
filename: str,
caption: str = None,
send_async: bool = True):
url = "https://api.telegram.org/bot" + bot_token + "/sendDocument"
data = {
"chat_id": chat_id,
}
files = {
"document": (filename, document)
}
if caption is not None:
data["caption"] = caption
if send_async:
threading.Thread(target=send_post_ss, args=(session, url, data, files)).start()
else:
return send_post_ss(session, url, data, files)
def reduce_year_from_string(input_string):
matches = re.findall(r"\d{4}", input_string)
for match in matches:
input_string = input_string.replace(match, match[-1])
return input_string
def round_price(price: Decimal, tick_size: Decimal):
return round(math.floor(price / tick_size) * tick_size,
len(decimal_to_string(tick_size).split('.')[1]))
def decimal_to_string(val: Decimal):
return format(Decimal(str(val)), "f")
def add_to_set(file_path, element):
try:
with open(file_path, "r") as file:
elements = set(file.read().splitlines())
except FileNotFoundError:
elements = set()
elements.add(element)
with open(file_path, "w") as file:
for item in elements:
file.write("%s\n" % item)
def get_all_elements(file_path):
try:
with open(file_path, "r") as file:
elements = set(file.read().splitlines())
return elements
except FileNotFoundError:
return set()
def is_within_time_window(current_time, windows):
for window_start, window_end in windows:
if window_start <= current_time < window_end:
return True, window_end
return False, None
def get_utc_time_windows(windows_str):
utc_now = dt.now(pytz.utc)
windows = []
for window in windows_str:
start, end = window.split("-")
start_dt = utc_now.replace(hour=int(start.split(":")[0]),
minute=int(start.split(":")[1]),
second=int(start.split(":")[2]),
microsecond=0)
end_dt = utc_now.replace(hour=int(end.split(":")[0]),
minute=int(end.split(":")[1]),
second=int(start.split(":")[2]),
microsecond=0)
if end_dt <= start_dt:
end_dt += timedelta(days=1)
windows.append((start_dt, end_dt))
return windows