-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathleaflow_checkin.py
More file actions
642 lines (530 loc) · 26 KB
/
leaflow_checkin.py
File metadata and controls
642 lines (530 loc) · 26 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/env python3
"""
Leaflow 多账号自动签到脚本
变量名:LEAFLOW_ACCOUNTS
变量值:邮箱1:密码1,邮箱2:密码2,邮箱3:密码3
"""
import os
import time
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import requests
from datetime import datetime
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class LeaflowAutoCheckin:
def __init__(self, email, password):
self.email = email
self.password = password
self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN', '')
self.telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID', '')
if not self.email or not self.password:
raise ValueError("邮箱和密码不能为空")
self.driver = None
self.setup_driver()
def setup_driver(self):
"""设置Chrome驱动选项"""
chrome_options = Options()
# GitHub Actions环境配置
if os.getenv('GITHUB_ACTIONS'):
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1920,1080')
# 通用配置
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
def close_popup(self):
"""关闭初始弹窗"""
try:
logger.info("尝试关闭初始弹窗...")
time.sleep(3) # 等待弹窗加载
# 尝试关闭弹窗
try:
actions = ActionChains(self.driver)
actions.move_by_offset(10, 10).click().perform()
logger.info("已成功关闭弹窗")
time.sleep(2)
return True
except:
pass
return False
except Exception as e:
logger.warning(f"关闭弹窗时出错: {e}")
return False
def wait_for_element_clickable(self, by, value, timeout=10):
"""等待元素可点击"""
return WebDriverWait(self.driver, timeout).until(
EC.element_to_be_clickable((by, value))
)
def wait_for_element_present(self, by, value, timeout=10):
"""等待元素出现"""
return WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located((by, value))
)
def login(self):
"""执行登录流程"""
logger.info(f"开始登录流程")
# 访问登录页面
self.driver.get("https://leaflow.net/login")
time.sleep(5)
# 关闭弹窗
self.close_popup()
# 输入邮箱
try:
logger.info("查找邮箱输入框...")
# 等待页面稳定
time.sleep(2)
# 尝试多种选择器找到邮箱输入框
email_selectors = [
"input[type='text']",
"input[type='email']",
"input[placeholder*='邮箱']",
"input[placeholder*='邮件']",
"input[placeholder*='email']",
"input[name='email']",
"input[name='username']"
]
email_input = None
for selector in email_selectors:
try:
email_input = self.wait_for_element_clickable(By.CSS_SELECTOR, selector, 5)
logger.info(f"找到邮箱输入框")
break
except:
continue
if not email_input:
raise Exception("找不到邮箱输入框")
# 清除并输入邮箱
email_input.clear()
email_input.send_keys(self.email)
logger.info("邮箱输入完成")
time.sleep(2)
except Exception as e:
logger.error(f"输入邮箱时出错: {e}")
# 尝试使用JavaScript直接设置值
try:
self.driver.execute_script(f"document.querySelector('input[type=\"text\"], input[type=\"email\"]').value = '{self.email}';")
logger.info("通过JavaScript设置邮箱")
time.sleep(2)
except:
raise Exception(f"无法输入邮箱: {e}")
# 等待密码输入框出现并输入密码
try:
logger.info("查找密码输入框...")
# 等待密码框出现
password_input = self.wait_for_element_clickable(
By.CSS_SELECTOR, "input[type='password']", 10
)
password_input.clear()
password_input.send_keys(self.password)
logger.info("密码输入完成")
time.sleep(1)
except TimeoutException:
raise Exception("找不到密码输入框")
# 点击登录按钮
try:
logger.info("查找登录按钮...")
login_btn_selectors = [
"//button[contains(text(), '登录')]",
"//button[contains(text(), 'Login')]",
"//button[@type='submit']",
"//input[@type='submit']",
"button[type='submit']"
]
login_btn = None
for selector in login_btn_selectors:
try:
if selector.startswith("//"):
login_btn = self.wait_for_element_clickable(By.XPATH, selector, 5)
else:
login_btn = self.wait_for_element_clickable(By.CSS_SELECTOR, selector, 5)
logger.info(f"找到登录按钮")
break
except:
continue
if not login_btn:
raise Exception("找不到登录按钮")
login_btn.click()
logger.info("已点击登录按钮")
except Exception as e:
raise Exception(f"点击登录按钮失败: {e}")
# 等待登录完成
try:
WebDriverWait(self.driver, 20).until(
lambda driver: "dashboard" in driver.current_url or "workspaces" in driver.current_url or "login" not in driver.current_url
)
# 检查当前URL确认登录成功
current_url = self.driver.current_url
if "dashboard" in current_url or "workspaces" in current_url or "login" not in current_url:
logger.info(f"登录成功,当前URL: {current_url}")
return True
else:
raise Exception("登录后未跳转到正确页面")
except TimeoutException:
# 检查是否登录失败
try:
error_selectors = [".error", ".alert-danger", "[class*='error']", "[class*='danger']"]
for selector in error_selectors:
try:
error_msg = self.driver.find_element(By.CSS_SELECTOR, selector)
if error_msg.is_displayed():
raise Exception(f"登录失败: {error_msg.text}")
except:
continue
raise Exception("登录超时,无法确认登录状态")
except Exception as e:
raise e
def get_balance(self):
"""获取当前账号的总余额"""
try:
logger.info("获取账号余额...")
# 跳转到仪表板页面
self.driver.get("https://leaflow.net/dashboard")
time.sleep(3)
# 等待页面加载
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "body"))
)
# 尝试多种选择器查找余额元素
balance_selectors = [
"//*[contains(text(), '¥') or contains(text(), '¥') or contains(text(), '元')]",
"//*[contains(@class, 'balance')]",
"//*[contains(@class, 'money')]",
"//*[contains(@class, 'amount')]",
"//button[contains(@class, 'dollar')]",
"//span[contains(@class, 'font-medium')]"
]
for selector in balance_selectors:
try:
elements = self.driver.find_elements(By.XPATH, selector)
for element in elements:
text = element.text.strip()
# 查找包含数字和货币符号的文本
if any(char.isdigit() for char in text) and ('¥' in text or '¥' in text or '元' in text):
# 提取数字部分
import re
numbers = re.findall(r'\d+\.?\d*', text)
if numbers:
balance = numbers[0]
logger.info(f"找到余额: {balance}元")
return f"{balance}元"
except:
continue
logger.warning("未找到余额信息")
return "未知"
except Exception as e:
logger.warning(f"获取余额时出错: {e}")
return "未知"
def wait_for_checkin_page_loaded(self, max_retries=3, wait_time=20):
"""等待签到页面完全加载,支持重试"""
for attempt in range(max_retries):
logger.info(f"等待签到页面加载,尝试 {attempt + 1}/{max_retries},等待 {wait_time} 秒...")
time.sleep(wait_time)
try:
# 检查页面是否包含签到相关元素
checkin_indicators = [
"button.checkin-btn", # 优先使用这个选择器
"//button[contains(text(), '立即签到')]",
"//button[contains(text(), '已签到')]",
"//*[contains(text(), '每日签到')]",
"//*[contains(text(), '签到')]"
]
for indicator in checkin_indicators:
try:
if indicator.startswith("//"):
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, indicator))
)
else:
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, indicator))
)
if element.is_displayed():
logger.info(f"找到签到页面元素")
return True
except:
continue
logger.warning(f"第 {attempt + 1} 次尝试未找到签到按钮,继续等待...")
except Exception as e:
logger.warning(f"第 {attempt + 1} 次检查签到页面时出错: {e}")
return False
def find_and_click_checkin_button(self):
"""查找并点击签到按钮 - 处理已签到状态"""
logger.info("查找签到按钮...")
try:
# 先等待页面可能的重载
time.sleep(5)
# 使用和单账号成功时相同的选择器
checkin_selectors = [
"button.checkin-btn",
"//button[contains(text(), '立即签到')]",
"//button[contains(@class, 'checkin')]",
"button[type='submit']",
"button[name='checkin']"
]
for selector in checkin_selectors:
try:
if selector.startswith("//"):
checkin_btn = WebDriverWait(self.driver, 15).until(
EC.presence_of_element_located((By.XPATH, selector))
)
else:
checkin_btn = WebDriverWait(self.driver, 15).until(
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
)
if checkin_btn.is_displayed():
# 检查按钮文本,如果包含"已签到"则说明今天已经签到过了
btn_text = checkin_btn.text.strip()
if "已签到" in btn_text:
logger.info("伙计,今日你已经签到过了!")
return "already_checked_in"
# 检查按钮是否可用
if checkin_btn.is_enabled():
logger.info(f"找到并点击立即签到按钮")
checkin_btn.click()
return True
else:
logger.info("签到按钮不可用,可能已经签到过了")
return "already_checked_in"
except Exception as e:
logger.debug(f"选择器未找到按钮: {e}")
continue
logger.error("找不到签到按钮")
return False
except Exception as e:
logger.error(f"查找签到按钮时出错: {e}")
return False
def checkin(self):
"""执行签到流程"""
logger.info("跳转到签到页面...")
# 跳转到签到页面
self.driver.get("https://checkin.leaflow.net")
# 等待签到页面加载(最多重试3次,每次等待20秒)
if not self.wait_for_checkin_page_loaded(max_retries=3, wait_time=20):
raise Exception("签到页面加载失败,无法找到签到相关元素")
# 查找并点击立即签到按钮
checkin_result = self.find_and_click_checkin_button()
if checkin_result == "already_checked_in":
return "今日已签到"
elif checkin_result is True:
logger.info("已点击立即签到按钮")
time.sleep(5) # 等待签到结果
# 获取签到结果
result_message = self.get_checkin_result()
return result_message
else:
raise Exception("找不到立即签到按钮或按钮不可点击")
def get_checkin_result(self):
"""获取签到结果消息"""
try:
# 给页面一些时间显示结果
time.sleep(3)
# 尝试查找各种可能的成功消息元素
success_selectors = [
".alert-success",
".success",
".message",
"[class*='success']",
"[class*='message']",
".modal-content", # 弹窗内容
".ant-message", # Ant Design 消息
".el-message", # Element UI 消息
".toast", # Toast消息
".notification" # 通知
]
for selector in success_selectors:
try:
element = self.driver.find_element(By.CSS_SELECTOR, selector)
if element.is_displayed():
text = element.text.strip()
if text:
return text
except:
continue
# 如果没有找到特定元素,检查页面文本
page_text = self.driver.find_element(By.TAG_NAME, "body").text
important_keywords = ["成功", "签到", "获得", "恭喜", "谢谢", "感谢", "完成", "已签到", "连续签到"]
for keyword in important_keywords:
if keyword in page_text:
# 提取包含关键词的行
lines = page_text.split('\n')
for line in lines:
if keyword in line and len(line.strip()) < 100: # 避免提取过长的文本
return line.strip()
# 检查签到按钮状态变化
try:
checkin_btn = self.driver.find_element(By.CSS_SELECTOR, "button.checkin-btn")
if not checkin_btn.is_enabled() or "已签到" in checkin_btn.text or "disabled" in checkin_btn.get_attribute("class"):
return "今日已签到完成"
except:
pass
return "签到完成,但未找到具体结果消息"
except Exception as e:
return f"获取签到结果时出错: {str(e)}"
def run(self):
"""单个账号执行流程"""
try:
logger.info(f"开始处理账号")
# 登录
if self.login():
# 签到
result = self.checkin()
# 获取余额
balance = self.get_balance()
logger.info(f"签到结果: {result}, 余额: {balance}")
return True, result, balance
else:
raise Exception("登录失败")
except Exception as e:
error_msg = f"自动签到失败: {str(e)}"
logger.error(error_msg)
return False, error_msg, "未知"
finally:
if self.driver:
self.driver.quit()
class MultiAccountManager:
"""多账号管理器 - 简化配置版本"""
def __init__(self):
self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN', '')
self.telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID', '')
self.accounts = self.load_accounts()
def load_accounts(self):
"""从环境变量加载多账号信息,支持冒号分隔多账号和单账号"""
accounts = []
logger.info("开始加载账号配置...")
# 方法1: 冒号分隔多账号格式
accounts_str = os.getenv('LEAFLOW_ACCOUNTS', '').strip()
if accounts_str:
try:
logger.info("尝试解析冒号分隔多账号配置")
account_pairs = [pair.strip() for pair in accounts_str.split(',')]
logger.info(f"找到 {len(account_pairs)} 个账号")
for i, pair in enumerate(account_pairs):
if ':' in pair:
email, password = pair.split(':', 1)
email = email.strip()
password = password.strip()
if email and password:
accounts.append({
'email': email,
'password': password
})
logger.info(f"成功添加第 {i+1} 个账号")
else:
logger.warning(f"账号对格式错误")
else:
logger.warning(f"账号对缺少冒号分隔符")
if accounts:
logger.info(f"从冒号分隔格式成功加载了 {len(accounts)} 个账号")
return accounts
else:
logger.warning("冒号分隔配置中没有找到有效的账号信息")
except Exception as e:
logger.error(f"解析冒号分隔账号配置失败: {e}")
# 方法2: 单账号格式
single_email = os.getenv('LEAFLOW_EMAIL', '').strip()
single_password = os.getenv('LEAFLOW_PASSWORD', '').strip()
if single_email and single_password:
accounts.append({
'email': single_email,
'password': single_password
})
logger.info("加载了单个账号配置")
return accounts
# 如果所有方法都失败
logger.error("未找到有效的账号配置")
logger.error("请检查以下环境变量设置:")
logger.error("1. LEAFLOW_ACCOUNTS: 冒号分隔多账号 (email1:pass1,email2:pass2)")
logger.error("2. LEAFLOW_EMAIL 和 LEAFLOW_PASSWORD: 单账号")
raise ValueError("未找到有效的账号配置")
def send_notification(self, results):
"""发送汇总通知到Telegram - 按照指定模板格式"""
if not self.telegram_bot_token or not self.telegram_chat_id:
logger.info("Telegram配置未设置,跳过通知")
return
try:
# 构建通知消息
success_count = sum(1 for _, success, _, _ in results if success)
total_count = len(results)
current_date = datetime.now().strftime("%Y/%m/%d")
message = f"🎁 Leaflow自动签到通知\n"
message += f"📊 成功: {success_count}/{total_count}\n"
message += f"📅 签到时间:{current_date}\n\n"
for email, success, result, balance in results:
# 隐藏邮箱部分字符以保护隐私
masked_email = email[:3] + "***" + email[email.find("@"):]
if success:
status = "✅"
message += f"账号:{masked_email}\n"
message += f"{status} {result}!\n"
message += f"💰 当前总余额:{balance}。\n\n"
else:
status = "❌"
message += f"账号:{masked_email}\n"
message += f"{status} {result}\n\n"
url = f"https://api.telegram.org/bot{self.telegram_bot_token}/sendMessage"
data = {
"chat_id": self.telegram_chat_id,
"text": message,
"parse_mode": "HTML"
}
response = requests.post(url, data=data, timeout=10)
if response.status_code == 200:
logger.info("Telegram汇总通知发送成功")
else:
logger.error(f"Telegram通知发送失败: {response.text}")
except Exception as e:
logger.error(f"发送Telegram通知时出错: {e}")
def run_all(self):
"""运行所有账号的签到流程"""
logger.info(f"开始执行 {len(self.accounts)} 个账号的签到任务")
results = []
for i, account in enumerate(self.accounts, 1):
logger.info(f"处理第 {i}/{len(self.accounts)} 个账号")
try:
auto_checkin = LeaflowAutoCheckin(account['email'], account['password'])
success, result, balance = auto_checkin.run()
results.append((account['email'], success, result, balance))
# 在账号之间添加间隔,避免请求过于频繁
if i < len(self.accounts):
wait_time = 5
logger.info(f"等待{wait_time}秒后处理下一个账号...")
time.sleep(wait_time)
except Exception as e:
error_msg = f"处理账号时发生异常: {str(e)}"
logger.error(error_msg)
results.append((account['email'], False, error_msg, "未知"))
# 发送汇总通知
self.send_notification(results)
# 返回总体结果
success_count = sum(1 for _, success, _, _ in results if success)
return success_count == len(self.accounts), results
def main():
"""主函数"""
try:
manager = MultiAccountManager()
overall_success, detailed_results = manager.run_all()
if overall_success:
logger.info("✅ 所有账号签到成功")
exit(0)
else:
success_count = sum(1 for _, success, _, _ in detailed_results if success)
logger.warning(f"⚠️ 部分账号签到失败: {success_count}/{len(detailed_results)} 成功")
# 即使有失败,也不退出错误状态,因为可能部分成功
exit(0)
except Exception as e:
logger.error(f"❌ 脚本执行出错: {e}")
exit(1)
if __name__ == "__main__":
main()