-
Notifications
You must be signed in to change notification settings - Fork 0
/
cash_functions.py
324 lines (259 loc) · 10.9 KB
/
cash_functions.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
import asyncio
import json
import os
from asyncio.log import logger
from datetime import datetime
import aiohttp
from bs4 import BeautifulSoup
from telegram import Update
from telegram.ext import ContextTypes
CASH_WATCHERS_FILE = "cash_watchers.json"
cash_watchers = {}
def load_cash_watchers():
global cash_watchers
if os.path.exists(CASH_WATCHERS_FILE):
with open(CASH_WATCHERS_FILE, "r") as f:
cash_watchers = json.load(f)
else:
cash_watchers = {}
async def remove_cash_watcher(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
user_id = str(update.effective_user.id)
args = context.args
if len(args) < 1:
await update.message.reply_text("Usage: /removeCashWatcher <username>")
return
username_to_remove = args[0]
if user_id not in cash_watchers or not cash_watchers[user_id]:
await update.message.reply_text("You don't have any registered cash watchers.")
return
for entry in cash_watchers[user_id]:
if entry["username"].lower() == username_to_remove.lower():
cash_watchers[user_id].remove(entry)
save_cash_watchers()
schedule_cash_updates(context)
await update.message.reply_text(
f"Successfully removed cash watcher for {entry['username']}."
)
return
await update.message.reply_text(
f"No cash watcher found for username: {username_to_remove}"
)
def schedule_cash_updates(context: ContextTypes.DEFAULT_TYPE):
# Remove all existing cash update jobs
for job in context.job_queue.get_jobs_by_name("cash_update"):
job.schedule_removal()
# Create a dictionary to group accounts by update time
update_times = {}
# Group accounts by update time
for user_id, accounts in cash_watchers.items():
for account in accounts:
update_time = account["update_time"]
if update_time not in update_times:
update_times[update_time] = []
update_times[update_time].append((user_id, account))
# Schedule new jobs for each update time
for update_time, accounts in update_times.items():
time = datetime.strptime(update_time, "%H:%M").time()
context.job_queue.run_daily(
send_grouped_cash_update,
time=time,
name="cash_update",
data=accounts,
)
async def send_grouped_cash_update(context: ContextTypes.DEFAULT_TYPE) -> None:
job = context.job
accounts = job.data
async def fetch_cash(user_id, account):
maplelegends_id = account["id"]
stored_username = account["username"]
last_cash = account.get("last_cash", 0)
try:
async with aiohttp.ClientSession() as session:
username, cash_amount = await get_cash_amount(maplelegends_id, session)
difference = cash_amount - last_cash
message = f"{username}: {cash_amount:,} ({difference:+,} since last check)"
# Update the stored cash amount
account["last_cash"] = cash_amount
account["username"] = username
except Exception as e:
logger.error(f"Error getting cash for user {maplelegends_id}: {str(e)}")
message = f"{stored_username} (ID {maplelegends_id}): Error fetching data"
return user_id, message
# Fetch cash for all accounts concurrently
results = await asyncio.gather(
*[fetch_cash(user_id, account) for user_id, account in accounts]
)
# Group results by user_id
grouped_results = {}
for user_id, message in results:
if user_id not in grouped_results:
grouped_results[user_id] = []
grouped_results[user_id].append(message)
# Send grouped messages to each user
for user_id, messages in grouped_results.items():
full_message = "Vote Cash update:\n" + "\n".join(messages)
try:
await context.bot.send_message(chat_id=user_id, text=full_message)
except Exception as e:
logger.error(f"Error sending cash update to user {user_id}: {str(e)}")
save_cash_watchers()
def save_cash_watchers():
with open(CASH_WATCHERS_FILE, "w") as f:
json.dump(cash_watchers, f)
async def watch_cash(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = str(update.effective_user.id)
args = context.args
if len(args) < 2:
await update.message.reply_text(
"Usage: /watchCash <HH:MM> <your_maplelegends_id>"
)
return
update_time = args[0]
maplelegends_id = args[1]
# Validate time format
try:
datetime.strptime(update_time, "%H:%M")
except ValueError:
await update.message.reply_text("Invalid time format. Please use HH:MM.")
return
try:
async with aiohttp.ClientSession() as session:
username, cash_amount = await get_cash_amount(maplelegends_id, session)
except Exception as e:
await update.message.reply_text(f"Error fetching data: {str(e)}")
return
if user_id not in cash_watchers:
cash_watchers[user_id] = []
existing_entry = next(
(item for item in cash_watchers[user_id] if item["id"] == maplelegends_id), None
)
if existing_entry:
existing_entry["update_time"] = update_time
await update.message.reply_text(
f"Updated: You will receive daily cash updates for {username} at {update_time} UTC"
)
else:
cash_watchers[user_id].append(
{
"id": maplelegends_id,
"username": username,
"last_cash": cash_amount,
"update_time": update_time,
}
)
await update.message.reply_text(
f"You will now receive daily cash updates for {username} at {update_time} UTC"
)
save_cash_watchers()
schedule_cash_updates(context)
async def send_cash_update(context: ContextTypes.DEFAULT_TYPE) -> None:
job = context.job
user_id = job.chat_id
account = job.data
maplelegends_id = account["id"]
stored_username = account["username"]
last_cash = account.get("last_cash", 0)
try:
async with aiohttp.ClientSession() as session:
username, cash_amount = await get_cash_amount(maplelegends_id, session)
difference = cash_amount - last_cash
message = f"Vote Cash update for {username}: {cash_amount:,} ({difference:+,} since last check)"
# Update the stored cash amount
account["last_cash"] = cash_amount
account["username"] = username
except Exception as e:
logger.error(f"Error getting cash for user {maplelegends_id}: {str(e)}")
message = f"Error fetching data for {stored_username} (ID {maplelegends_id})"
try:
await context.bot.send_message(chat_id=user_id, text=message)
except Exception as e:
logger.error(f"Error sending cash update to user {user_id}: {str(e)}")
save_cash_watchers()
async def get_cash_amount(user_id, session):
"""Helper function to get cash amount and username."""
url = "https://maplelegends.com/my/account"
headers = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"cookie": f"mlTheme=light; webpy_session_id={user_id}",
"referer": "https://maplelegends.com/vote",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
}
async with session.get(url, headers=headers) as response:
response.raise_for_status()
text = await response.text()
soup = BeautifulSoup(text, "html.parser")
vote_cash_element = soup.select_one('div.col-md-6:-soup-contains("Vote Cash:") b')
username_element = soup.select_one(
"ul.nav.navbar-nav.pull-right li.visible-md.visible-lg a.spa"
)
if vote_cash_element and username_element:
cash_amount = vote_cash_element.text.strip().replace(",", "")
return username_element.text.strip(), int(float(cash_amount))
else:
raise ValueError(
f"Unable to find Vote Cash or username information for user ID {user_id}"
)
async def get_cash(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Fetch and display the vote cash for a given user ID."""
if not context.args:
await update.message.reply_text(
"Please provide a user ID. Usage: /getCash <id>"
)
return
user_id = context.args[0]
try:
async with aiohttp.ClientSession() as session:
username, vote_cash = await get_cash_amount(user_id, session)
await update.message.reply_text(f"Vote Cash amount for {username}: {vote_cash}")
except ValueError as e:
await update.message.reply_text(str(e))
except Exception as e:
await update.message.reply_text(f"Error fetching data: {str(e)}")
async def update_cash(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = str(update.effective_user.id)
if user_id not in cash_watchers or not cash_watchers[user_id]:
await update.message.reply_text(
"You haven't registered any accounts to watch. Use /watchCash to add accounts."
)
return
# Send an initial message
message = await update.message.reply_text("Fetching cash amounts...")
async def fetch_cash(entry):
async with aiohttp.ClientSession() as session:
maplelegends_id = entry["id"]
stored_username = entry["username"]
last_cash = entry.get("last_cash", 0)
try:
async with aiohttp.ClientSession() as session:
username, cash_amount = await get_cash_amount(
maplelegends_id, session
)
difference = cash_amount - last_cash
result = (
f"{username}: {cash_amount:,} ({difference:+,} since last check)\n"
)
entry["last_cash"] = cash_amount
entry["username"] = username
return result
except Exception as e:
logger.error(f"Error getting cash for user {maplelegends_id}: {str(e)}")
return (
f"{stored_username} (ID {maplelegends_id}): Error fetching data\n"
)
# Create tasks for all cash fetching operations
tasks = [fetch_cash(entry) for entry in cash_watchers[user_id]]
# Run all tasks concurrently
results = await asyncio.gather(*tasks)
# Combine results
result_message = "Current Vote Cash amounts:\n" + "".join(results)
# Update the message with the results
await message.edit_text(result_message)
save_cash_watchers() # Save the updated cash amounts
# Modify your command handler to use create_task
async def handle_update_cash(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
asyncio.create_task(update_cash(update, context))