forked from doveppp/linuxdo-checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (69 loc) · 2.53 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
import os
import time
import random
from tabulate import tabulate
from playwright.sync_api import sync_playwright
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)
self.context = self.browser.new_context()
self.page = self.context.new_page()
self.page.goto(HOME_URL)
def login(self):
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:
print("Login failed")
return False
else:
print("Check in success")
return True
def click_topic(self):
for topic in self.page.query_selector_all("#list-area .title"):
page = self.context.new_page()
page.goto(HOME_URL + topic.get_attribute("href"))
time.sleep(3)
if random.random() < 0.02: # 100 * 0.02 * 30 = 60
self.click_like(page)
time.sleep(3)
page.close()
def run(self):
if not self.login():
return
self.click_topic()
self.print_connect_info()
def click_like(self, page):
page.locator(".discourse-reactions-reaction-button").first.click()
print("Like success")
def print_connect_info(self):
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()