-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
133 lines (105 loc) · 3.86 KB
/
helpers.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
import json
from datetime import datetime, timedelta, timezone
from urllib.parse import unquote
def convert_datetime_str_to_utc(datetime_str):
decimal_index = datetime_str.find(".")
if decimal_index != -1:
# Ensure only 3 digits after the decimal point for milliseconds
datetime_str = datetime_str[: decimal_index + 4]
return datetime.fromisoformat(datetime_str).replace(tzinfo=timezone.utc)
def format_duration(seconds):
message = ""
duration_td = timedelta(seconds=seconds)
days, day_remainder = divmod(duration_td.total_seconds(), 86400)
hours, remainder = divmod(day_remainder, 3600)
minutes, seconds = divmod(remainder, 60)
if days:
message = f"{int(days)} days "
if hours:
message = message + f"{int(hours)} hours "
if minutes:
message = message + f"{int(minutes)} minute "
if seconds:
message = message + f"{int(seconds)} seconds"
return message.strip()
def remove_query_id_from_tg_web_data(tg_web_data):
data_to_be_splitted = tg_web_data
splitted_original_data = data_to_be_splitted.split("&")
return "&".join(splitted_original_data[1:])
def mapping_role_color(role):
if role == "admin":
role = f"[cyan]{role}[/cyan]"
elif role == "premium":
role = f"[magenta]{role}[/magenta]"
return role
def decode_query_id(query_id):
query_string = query_id
if "tgWebAppData" in query_id:
query_string = unquote(
string=query_id.split("tgWebAppData=", maxsplit=1)[1].split(
"&tgWebAppVersion", maxsplit=1
)[0]
)
parameters = query_string.split("&")
decoded_pairs = [(param.split("=")[0], unquote(param.split("=")[1])) for param in parameters]
result = dict()
for key, value in decoded_pairs:
result[key] = value
reassign(result)
return result
def reassign(d):
"""
check if you have a dict after using literal_eval and reassign
"""
for k, v in d.items():
if v[0] in {"{", "["}:
try:
evald = json.loads(v)
if isinstance(evald, dict):
d[k] = evald
except ValueError as err:
pass
async def get_query_ids():
temp_lines = []
with open("query_ids.txt", "r") as file:
temp_lines = file.readlines()
lines = [line.strip() for line in temp_lines]
return lines
def get_tele_user_obj_from_query_id(query_id):
# formatted_query_id = unquote(string=query_id)
query_obj = decode_query_id(query_id)
tele_user_obj = query_obj.get("user", {})
return tele_user_obj
def populate_not_started_tasks(tasks: list):
not_started_tasks = []
for task in tasks:
if task.get("title") == "Promo":
continue
sub_tasks = task.get("tasks")
for sub_task in sub_tasks:
if (
sub_task.get("status") == "NOT_STARTED"
and sub_task.get("socialSubscription", {}).get("openInTelegram") is not True
and sub_task.get("isHidden") is not True
):
temp_task = {
"task_id": sub_task.get("id"),
"task_title": sub_task.get("title"),
}
not_started_tasks.append(temp_task)
return not_started_tasks
def populate_not_claimed_tasks(tasks: list):
not_claimed_tasks = []
for task in tasks:
if task.get("title") == "Promo":
continue
sub_tasks = task.get("tasks")
for sub_task in sub_tasks:
if sub_task.get("status") == "READY_FOR_CLAIM":
temp_task = {
"task_id": sub_task.get("id"),
"task_title": sub_task.get("title"),
"task_reward": sub_task.get("reward"),
}
not_claimed_tasks.append(temp_task)
return not_claimed_tasks