-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
121 lines (93 loc) · 3.24 KB
/
utils.py
File metadata and controls
121 lines (93 loc) · 3.24 KB
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
import os
import json
import sys
import time
from datetime import datetime
import pytz
from tzlocal import get_localzone
import requests
import textwrap
import telegramify_markdown
def load_json_file(file_path):
try:
with open(file_path, 'r') as f:
return json.load(f)
except FileNotFoundError:
print("\n{} not found or is invalid.\n".format(file_path))
return {}
def save_json_file(file_path, data):
with open(file_path, 'w') as f:
if isinstance(data, list):
json.dump(data, f, separators=(',', ':'), ensure_ascii=False)
else:
json.dump(data, f, indent=4)
def load_txt_file_to_list(file_path):
try:
with open(file_path, 'r') as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
return lines
except FileNotFoundError:
print("\nThe file not found or is invalid.\n")
sys.exit(1)
def can_be_float(string):
try:
float(string)
return True
except ValueError:
return False
def convert_utc_to_user_timezone(utc_time_str):
utc_time = datetime.strptime(utc_time_str,
"%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.UTC)
local_timezone = get_localzone()
user_time = utc_time.astimezone(local_timezone)
return user_time
def is_list_of_strings(variable):
return isinstance(variable, list) and all(
isinstance(item, str) for item in variable)
def is_list_of_dicts(variable):
return isinstance(variable, list) and all(
isinstance(item, dict) for item in variable)
def send_telegram_message(bot_token, chat_id, message, verbose=True):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
payload = {"chat_id": chat_id, "text": message, "parse_mode": "MarkdownV2"}
response = requests.post(url, json=payload)
if verbose:
if response.status_code == 200:
print("Message sent to Telegram successfully!")
else:
print(f"Failed to send message. Error: {response.text}")
def chunk_message(messages, max_length=4096):
chunks = []
current_chunk = ""
for message in messages:
markdown_text = textwrap.dedent(message)
can_be_sent = telegramify_markdown.markdownify(markdown_text)
if len(current_chunk) + len(can_be_sent) <= max_length:
current_chunk += can_be_sent + "\n"
else:
chunks.append(current_chunk.strip())
current_chunk = can_be_sent + "\n"
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def send_to_telegram(msg_list,
bot,
chat_id,
max_retries,
try_sleep=3,
except_sleep=60):
chunks = chunk_message(msg_list)
for chunk in chunks:
retry_count = 0
while retry_count < max_retries:
try:
bot.send_message(chat_id, chunk, parse_mode='MarkdownV2')
time.sleep(try_sleep)
break
except:
retry_count += 1
time.sleep(except_sleep)
else:
print("Max retries reached. No new message will be sent.\n")
break