-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathmain.py
157 lines (130 loc) · 5.26 KB
/
main.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
import os
import random
import time
import functools
import sys
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("USERNAME")
PASSWORD = os.environ.get("PASSWORD")
HOME_URL = "https://linux.do/"
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")
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) # 使用非零退出码终止整个程序
self.click_topic()
self.print_connect_info()
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()
if __name__ == "__main__":
if not USERNAME or not PASSWORD:
print("Please set USERNAME and PASSWORD")
exit(1)
l = LinuxDoBrowser()
l.run()