forked from doveppp/linuxdo-checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (195 loc) · 8.03 KB
/
main.py
File metadata and controls
228 lines (195 loc) · 8.03 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
"""
cron: 0 */6 * * *
new Env("Linux.Do 签到")
"""
import os
import random
import time
import functools
import sys
import requests
import re
from loguru import logger
from playwright.sync_api import sync_playwright
from tabulate import tabulate
def retry_decorator(retries=3):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(retries):
try:
return func(*args, **kwargs)
except Exception as e:
if attempt == retries - 1: # 最后一次尝试
logger.error(f"函数 {func.__name__} 最终执行失败: {str(e)}")
logger.warning(f"函数 {func.__name__} 第 {attempt + 1}/{retries} 次尝试失败: {str(e)}")
time.sleep(1)
return None
return wrapper
return decorator
os.environ.pop("DISPLAY", None)
os.environ.pop("DYLD_LIBRARY_PATH", None)
USERNAME = os.environ.get("LINUXDO_USERNAME")
PASSWORD = os.environ.get("LINUXDO_PASSWORD")
BROWSE_ENABLED = os.environ.get("BROWSE_ENABLED", "true").strip().lower() not in ['false', '0', 'off']
if not USERNAME:
USERNAME = os.environ.get('USERNAME')
if not PASSWORD:
PASSWORD = os.environ.get('PASSWORD')
GOTIFY_URL = os.environ.get("GOTIFY_URL") # Gotify 服务器地址
GOTIFY_TOKEN = os.environ.get("GOTIFY_TOKEN") # Gotify 应用的 API Token
SC3_PUSH_KEY = os.environ.get("SC3_PUSH_KEY") # Server酱³ SendKey
HOME_URL = "https://linux.do/"
LOGIN_URL = "https://linux.do/login"
class LinuxDoBrowser:
def __init__(self) -> None:
self.pw = sync_playwright().start()
self.browser = self.pw.firefox.launch(headless=True, timeout=30000)
self.context = self.browser.new_context()
self.page = self.context.new_page()
self.page.goto(HOME_URL)
def login(self):
logger.info("开始登录")
# self.page.click(".login-button .d-button-label")
self.page.goto(LOGIN_URL)
time.sleep(2)
self.page.fill("#login-account-name", USERNAME)
time.sleep(2)
self.page.fill("#login-account-password", PASSWORD)
time.sleep(2)
self.page.click("#login-button")
time.sleep(10)
user_ele = self.page.query_selector("#current-user")
if not user_ele:
logger.error("登录失败")
return False
else:
logger.info("登录成功")
return True
def click_topic(self):
topic_list = self.page.query_selector_all("#list-area .title")
logger.info(f"发现 {len(topic_list)} 个主题帖")
for topic in topic_list:
self.click_one_topic(topic.get_attribute("href"))
@retry_decorator()
def click_one_topic(self, topic_url):
page = self.context.new_page()
page.goto(HOME_URL + topic_url)
if random.random() < 0.3: # 0.3 * 30 = 9
self.click_like(page)
self.browse_post(page)
page.close()
def browse_post(self, page):
prev_url = None
# 开始自动滚动,最多滚动10次
for _ in range(10):
# 随机滚动一段距离
scroll_distance = random.randint(550, 650) # 随机滚动 550-650 像素
logger.info(f"向下滚动 {scroll_distance} 像素...")
page.evaluate(f"window.scrollBy(0, {scroll_distance})")
logger.info(f"已加载页面: {page.url}")
if random.random() < 0.03: # 33 * 4 = 132
logger.success("随机退出浏览")
break
# 检查是否到达页面底部
at_bottom = page.evaluate("window.scrollY + window.innerHeight >= document.body.scrollHeight")
current_url = page.url
if current_url != prev_url:
prev_url = current_url
elif at_bottom and prev_url == current_url:
logger.success("已到达页面底部,退出浏览")
break
# 动态随机等待
wait_time = random.uniform(2, 4) # 随机等待 2-4 秒
logger.info(f"等待 {wait_time:.2f} 秒...")
time.sleep(wait_time)
def run(self):
if not self.login(): # 登录
logger.error("登录失败,程序终止")
sys.exit(1) # 使用非零退出码终止整个程序
if BROWSE_ENABLED:
self.click_topic() # 点击主题
logger.info("完成浏览任务")
self.print_connect_info() # 打印连接信息
self.send_notifications(BROWSE_ENABLED) # 发送通知
def click_like(self, page):
try:
# 专门查找未点赞的按钮
like_button = page.locator('.discourse-reactions-reaction-button[title="点赞此帖子"]').first
if like_button:
logger.info("找到未点赞的帖子,准备点赞")
like_button.click()
logger.info("点赞成功")
time.sleep(random.uniform(1, 2))
else:
logger.info("帖子可能已经点过赞了")
except Exception as e:
logger.error(f"点赞失败: {str(e)}")
def print_connect_info(self):
logger.info("获取连接信息")
page = self.context.new_page()
page.goto("https://connect.linux.do/")
rows = page.query_selector_all("table tr")
info = []
for row in rows:
cells = row.query_selector_all("td")
if len(cells) >= 3:
project = cells[0].text_content().strip()
current = cells[1].text_content().strip()
requirement = cells[2].text_content().strip()
info.append([project, current, requirement])
print("--------------Connect Info-----------------")
print(tabulate(info, headers=["项目", "当前", "要求"], tablefmt="pretty"))
page.close()
def send_notifications(self, browse_enabled):
status_msg = "✅每日登录成功"
if browse_enabled:
status_msg += " + 浏览任务完成"
if GOTIFY_URL and GOTIFY_TOKEN:
try:
response = requests.post(
f"{GOTIFY_URL}/message",
params={"token": GOTIFY_TOKEN},
json={
"title": "LINUX DO",
"message": status_msg,
"priority": 1
},
timeout=10
)
response.raise_for_status()
logger.success("消息已推送至Gotify")
except Exception as e:
logger.error(f"Gotify推送失败: {str(e)}")
else:
logger.info("未配置Gotify环境变量,跳过通知发送")
if SC3_PUSH_KEY:
match = re.match(r"sct(\d+)t", SC3_PUSH_KEY, re.I)
if not match:
logger.error("❌ SC3_PUSH_KEY格式错误,未获取到UID,无法使用Server酱³推送")
return
uid = match.group(1)
url = f'https://{uid}.push.ft07.com/send/{SC3_PUSH_KEY}'
params = {
"title": "LINUX DO",
"desp": status_msg
}
attempts = 5
for attempt in range(attempts):
try:
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
logger.success(f"Server酱³推送成功: {response.text}")
break
except Exception as e:
logger.error(f"Server酱³推送失败: {str(e)}")
if attempt < attempts - 1:
sleep_time = random.randint(180, 360)
logger.info(f"将在 {sleep_time} 秒后重试...")
time.sleep(sleep_time)
if __name__ == "__main__":
if not USERNAME or not PASSWORD:
print("Please set USERNAME and PASSWORD")
exit(1)
l = LinuxDoBrowser()
l.run()