forked from WowYiJiu/Linux.do-KeepAlive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinux.do.py
More file actions
475 lines (382 loc) · 16.9 KB
/
Linux.do.py
File metadata and controls
475 lines (382 loc) · 16.9 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
# -*- coding: utf-8 -*-
import os
import time
import logging
import random
from os import path
from io import StringIO
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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.common.action_chains import ActionChains
from selenium.common.exceptions import (
TimeoutException,
NoSuchElementException,
WebDriverException,
)
import shutil
logger = logging.getLogger()
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter(
"[%(asctime)s %(levelname)s] %(message)s", datefmt="%H:%M:%S"
)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
missing_configs = []
username_env = os.getenv("LINUXDO_USERNAME")
password_env = os.getenv("LINUXDO_PASSWORD")
if not username_env:
missing_configs.append("环境变量 'LINUXDO_USERNAME' 未设置或为空")
if not password_env:
missing_configs.append("环境变量 'LINUXDO_PASSWORD' 未设置或为空")
if missing_configs:
logging.error(f"缺少必要配置: {', '.join(missing_configs)},请在环境变量中设置。")
exit(1)
USERNAME = [line.strip() for line in username_env.splitlines() if line.strip()]
PASSWORD = [line.strip() for line in password_env.splitlines() if line.strip()]
SCROLL_DURATION = int(os.getenv("SCROLL_DURATION", 0))
VIEW_COUNT = int(os.getenv("VIEW_COUNT", 1000))
HOME_URL = os.getenv("HOME_URL", "https://linux.do/")
CONNECT_URL = os.getenv("CONNECT_URL", "https://connect.linux.do/")
browse_count = 0
connect_info = ""
like_count = 0
account_info = []
chrome_options = ""
chromedriver_path = ""
user_count = len(USERNAME)
if user_count != len(PASSWORD):
logging.error("用户名和密码的数量不一致,请检查环境变量设置。")
exit(1)
logging.info(f"共找到 {user_count} 个账户")
def load_send():
cur_path = path.abspath(path.dirname(__file__))
if path.exists(cur_path + "/notify.py"):
try:
from notify import send
return send
except ImportError:
return False
else:
return False
class LinuxDoBrowser:
def __init__(self) -> None:
logging.info("启动 Selenium")
global chrome_options
global chromedriver_path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chromedriver_path = shutil.which("chromedriver")
if not chromedriver_path:
logging.error("chromedriver 未找到,请确保已安装并配置正确的路径。")
exit(1)
self.driver = None
def simulate_typing(self, element, text, typing_speed=0.1, random_delay=True):
for char in text:
element.send_keys(char)
if random_delay:
time.sleep(typing_speed + random.uniform(0, 0.1))
else:
time.sleep(typing_speed)
def login(self) -> bool:
try:
logging.info(f"--- 开始尝试登录:{self.username}---")
login_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".login-button .d-button-label")
)
)
login_button.click()
username_field = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#login-account-name"))
)
self.simulate_typing(username_field, self.username)
password_field = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "#login-account-password")
)
)
self.simulate_typing(password_field, self.password)
submit_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "#login-button"))
)
submit_button.click()
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#current-user"))
)
logging.info("登录成功")
return True
except Exception as e:
error_message = self.driver.find_elements(
By.CSS_SELECTOR, "#modal-alert.alert-error"
)
if error_message:
logging.error("登录失败:用户名、电子邮件或密码不正确")
else:
logging.error(f"登录失败:{e}")
return False
def load_all_topics(self):
end_time = time.time() + SCROLL_DURATION
actions = ActionChains(self.driver)
while time.time() < end_time:
actions.scroll_by_amount(0, 500).perform()
time.sleep(0.1)
logging.info("页面滚动完成,已停止加载更多帖子")
def click_topic(self):
try:
logging.info("--- 开始滚动页面加载更多帖子 ---")
self.load_all_topics()
topics = self.driver.find_elements(By.CSS_SELECTOR, "#list-area .title")
total_topics = len(topics)
logging.info(f"共找到 {total_topics} 个帖子")
logging.info("--- 开始浏览帖子 ---")
global browse_count
for idx, topic in enumerate(topics):
try:
parent_element = topic.find_element(By.XPATH, "./ancestor::tr")
is_pinned = parent_element.find_elements(
By.CSS_SELECTOR, ".topic-statuses .pinned"
)
if is_pinned:
logging.info(f"跳过置顶的帖子:{topic.text.strip()}")
continue
views_element = parent_element.find_element(
By.CSS_SELECTOR, ".num.views .number"
)
views_title = views_element.get_attribute("title")
if "此话题已被浏览 " in views_title and " 次" in views_title:
views_count_str = views_title.split("此话题已被浏览 ")[1].split(" 次")[0]
views_count = int(views_count_str.replace(",", ""))
else:
logging.warning(f"无法解析浏览次数,跳过该帖子: {views_title}")
continue
article_title = topic.text.strip()
logging.info(f"打开第 {idx + 1}/{total_topics} 个帖子 :{article_title}")
article_url = topic.get_attribute("href")
try:
self.driver.execute_script("window.open('');")
self.driver.switch_to.window(self.driver.window_handles[-1])
browse_start_time = time.time()
self.driver.set_page_load_timeout(10) # 设置页面加载超时时间
try:
self.driver.get(article_url)
except TimeoutException:
logging.warning(f"加载帖子超时: {article_title}")
raise # 重新抛出异常,让外层catch处理
browse_count += 1
start_time = time.time()
if views_count > VIEW_COUNT:
logging.info(f"📈 当前帖子浏览量为{views_count} 大于设定值 {VIEW_COUNT},🥳 开始进行点赞操作")
self.click_like()
scroll_duration = random.uniform(5, 10)
try:
while time.time() - start_time < scroll_duration:
self.driver.execute_script(
"window.scrollBy(0, window.innerHeight);"
)
time.sleep(1)
except Exception as e:
logging.warning(f"在滚动过程中发生错误: {e}")
browse_end_time = time.time()
total_browse_time = browse_end_time - browse_start_time
logging.info(f"浏览该帖子时间: {total_browse_time:.2f}秒")
except Exception as e:
logging.error(f"处理帖子时发生错误: {e}")
finally:
# 确保无论如何都会关闭新打开的标签页
if len(self.driver.window_handles) > 1:
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[0])
logging.info(f"已关闭第 {idx + 1}/{total_topics} 个帖子 : {article_title}")
except Exception as e:
logging.error(f"处理帖子 {idx + 1} 时发生错误: {e}")
continue # 继续处理下一个帖子
logging.info("所有帖子处理完毕")
except Exception as e:
logging.error(f"click_topic 方法发生错误: {e}")
def run(self):
global browse_count
global connect_info
global like_count
for i in range(user_count):
start_time = time.time()
self.username = USERNAME[i]
self.password = PASSWORD[i]
logging.info(f"▶️▶️▶️ 开始执行第{i+1}个账号: {self.username}")
try:
self.driver = webdriver.Chrome(
service=Service(chromedriver_path), options=chrome_options
)
logging.info("导航到 LINUX DO 首页")
self.driver.get(HOME_URL)
if not self.login():
logging.error(f"{self.username} 登录失败")
continue
self.click_topic()
logging.info(f"🎉 恭喜:{self.username},帖子浏览全部完成")
self.print_connect_info()
self.logout()
except WebDriverException as e:
logging.error(f"WebDriver 初始化失败: {e}")
logging.info("请尝试重新搭建青龙面板或换个机器运行")
exit(1)
except Exception as e:
logging.error(f"运行过程中出错: {e}")
finally:
if self.driver is not None:
self.driver.quit()
end_time = time.time()
spend_time = int((end_time - start_time) // 60)
account_info.append(
{
"username": self.username,
"browse_count": browse_count,
"like_count": like_count,
"spend_time": spend_time,
"connect_info": connect_info,
}
)
# 重置状态
browse_count = 0
like_count = 0
connect_info = ""
logging.info("所有账户处理完毕")
summary = ""
for info in account_info:
summary += (
f"用户:{info['username']}\n\n"
f"本次共浏览 {info['browse_count']} 个帖子\n"
f"共点赞{info['like_count']} 个帖子\n"
f"共用时 {info['spend_time']} 分钟\n"
f"{info['connect_info']}\n\n"
)
send = load_send()
if callable(send):
send("Linux.do浏览帖子", summary)
else:
print("\n加载通知服务失败")
def click_like(self):
try:
global like_count
like_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".btn-toggle-reaction-like")
)
)
if "移除此赞" in like_button.get_attribute("title"):
logging.info("该帖子已点赞,跳过点赞操作。")
else:
self.driver.execute_script("arguments[0].click();", like_button)
like_count += 1
logging.info("点赞帖子成功")
except TimeoutException:
logging.error("点赞操作失败:点赞按钮定位超时")
except WebDriverException as e:
logging.error(f"点赞操作失败: {e}")
except Exception as e:
logging.error(f"未知错误导致点赞操作失败: {e}")
def print_connect_info(self):
self.driver.execute_script("window.open('');")
self.driver.switch_to.window(self.driver.window_handles[-1])
logging.info("导航到LINUX DO Connect页面")
self.driver.get(CONNECT_URL)
global connect_info
rows = self.driver.find_elements(By.CSS_SELECTOR, "table tr")
info = []
for row in rows:
cells = row.find_elements(By.TAG_NAME, "td")
if len(cells) >= 3:
project = cells[0].text.strip()
current = cells[1].text.strip()
requirement = cells[2].text.strip()
info.append([project, current, requirement])
column_widths = [24, 22, 16]
def calculate_content_width(content):
return sum(2 if ord(char) > 127 else 1 for char in content)
def format_cell(content, width, alignment="left"):
content_length = calculate_content_width(content)
padding = width - content_length
if padding > 0:
if alignment == "left":
return content + " " * padding
elif alignment == "right":
return " " * padding + content
elif alignment == "center":
left_padding = padding // 2
right_padding = padding - left_padding
return " " * left_padding + content + " " * right_padding
else:
return content[:width]
def build_row(cells):
return "| " + " | ".join(cells) + " |"
def build_separator():
return "+" + "+".join(["-" * (width + 2) for width in column_widths]) + "+"
formatted_info = [
build_row(
[
format_cell(row[0], column_widths[0]),
format_cell(row[1], column_widths[1], "center"),
format_cell(row[2], column_widths[2], "center"),
]
)
for row in info
]
header = build_row(
[
format_cell("项目", column_widths[0]),
format_cell("当前", column_widths[1], "center"),
format_cell("要求", column_widths[2], "center"),
]
)
separator = build_separator()
output = StringIO()
output.write("在过去 💯 天内:\n")
output.write(separator + "\n")
output.write(header + "\n")
output.write(separator.replace("-", "=") + "\n")
output.write("\n".join(formatted_info) + "\n")
output.write(separator + "\n")
table_output = output.getvalue()
output.close()
print(table_output)
connect_info = "\n在过去 💯 天内:\n" + "\n".join(
[f"{row[0]}({row[2]}):{row[1]}" for row in info]
)
self.driver.close()
self.driver.switch_to.window(self.driver.window_handles[0])
def logout(self):
try:
user_menu_button = self.driver.find_element(By.ID, "toggle-current-user")
user_menu_button.click()
profile_tab_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable((By.ID, "user-menu-button-profile"))
)
profile_tab_button.click()
logout_button = WebDriverWait(self.driver, 20).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "li.logout button.profile-tab-btn")
)
)
logout_button.click()
self.driver.refresh()
elements = self.driver.find_elements(
By.CSS_SELECTOR, ".header-buttons .login-button"
)
if elements:
logging.info(f"{self.username}登出成功")
else:
logging.info(f"{self.username}登出失败")
except (TimeoutException, NoSuchElementException) as e:
logging.error(f"登出失败,可能由于元素未找到或超时: {e}")
except Exception as e:
logging.error(f"登出过程中发生错误: {e}")
if __name__ == "__main__":
linuxdo_browser = LinuxDoBrowser()
linuxdo_browser.run()