-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidoGetQuestion.py
More file actions
177 lines (126 loc) · 5.65 KB
/
SlidoGetQuestion.py
File metadata and controls
177 lines (126 loc) · 5.65 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
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import tkinter as tk
from tkinter import simpledialog
import tkinter.font as tkFont
import textwrap
root = tk.Tk()
root.title('Slido Questions')
root.overrideredirect(False)
BORDERLESS_HOTKEY = "<Control-Shift-C>"
MAX_QUESTIONS = 10
BOX_WIDTH = 280
BOX_HEIGHT = 60
BOX_fill_COLOR = 'black'
BOX_OUTLINE_COLOR = 'black'
BOX_SPACING = 10
RADIUS = 5
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 750
fill_COLOR = 'white'
AUTHOR_FONT = tkFont.Font(family='微軟正黑體', size=8)
CONTENT_FONT = tkFont.Font(family='微軟正黑體', size=12, weight='bold')
VOTE_FONT = tkFont.Font(family='微軟正黑體', size=12)
current_questions = []
canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg='green', highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=tk.YES)
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(5)
url = simpledialog.askstring("Slido URL", "Enter the Slido URL:")
driver.get(url)
def on_keys_press(event):
HOTKEY = event.keysym
dialog_title = "Hotkey Setting"
diakog_text = "Press the hotkey you want to bind: "
HOTKEY = simpledialog.askstring(dialog_title, diakog_text)
root.focus_set()
def toggle_borderless(event=None):
current_BorderState = root.overrideredirect()
root.overrideredirect(not current_BorderState)
def click_Recent_tab(driver):
# Wait for the 'Recent' tab to become visible''
Recent_tab = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//button[text()='Recent']")))
# Click on the 'Recent' tab
Recent_tab.click()
# Wait for the new order of questions to load
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".question-item__body")))
def get_questions(driver):
WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, '.question-item__body')))
click_Recent_tab(driver)
html_content = driver.page_source
soup = BeautifulSoup(html_content, 'html.parser')
authors = soup.find_all('div', class_='question-item__author truncate')
comments = soup.find_all('div', class_='question-item__body')
votes = soup.find_all(
'button', class_='score__btn score__btn--plus btn-plain')
questions = []
for i, (author, comment, vote) in enumerate(zip(authors, comments, votes)):
author_text = author.text.strip()
question_text = comment.text.strip()
vote_text = vote.get('aria-label').split()[0]
questions.append(
{'author': author_text, 'content': question_text, 'vote': vote_text})
return questions
def update_questions():
canvas.delete(tk.ALL)
global current_questions
current_questions = get_questions(driver)
current_questions.reverse()
while len(current_questions) > MAX_QUESTIONS:
question_box = current_questions.pop(0)
if 'box' in question_box:
box_id = question_box.pop('box')
canvas.delete(box_id)
y = 10
canvas_height = 0
for i, question in enumerate(current_questions):
content_lines = textwrap.wrap(question['content'], width=12)
num_lines = len(content_lines)
content_text = '\n'.join(content_lines)
box_height = 50 + 20 * num_lines
x1, y1 = 10, y
x2, y2 = 10+BOX_WIDTH, y+box_height
arc = 2 * RADIUS
box_arc1 = canvas.create_arc(x1, y1, x1+arc, y1+arc, start=90, extent=90, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
box_arc2 = canvas.create_arc(x2-arc, y1, x2, y1+arc, start=0, extent=90, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
box_arc3 = canvas.create_arc(x1, y2-arc, x1+arc, y2, start=180, extent=90, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
box_arc4 = canvas.create_arc(x2-arc, y2-arc, x2, y2, start=270, extent=90, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
box_rectangle1 = canvas.create_rectangle(x1+RADIUS, y1, x2-RADIUS, y2, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
box_rectangle2 = canvas.create_rectangle(x1, y1+RADIUS, x2, y2-RADIUS, fill=BOX_fill_COLOR, outline=BOX_OUTLINE_COLOR)
author = canvas.create_text(
20, y+10, anchor=tk.NW, text=question['author'], font=AUTHOR_FONT, fill=fill_COLOR)
content = canvas.create_text(
20, y+box_height-10-20*num_lines, anchor=tk.NW, text=content_text, font=CONTENT_FONT, fill=fill_COLOR)
vote = canvas.create_text(
280, y+box_height-30, anchor=tk.NE, text=f"❤ {question['vote']}", font=VOTE_FONT, fill=fill_COLOR)
question['box_arc1'] = box_arc1
question['box_arc2'] = box_arc2
question['box_arc3'] = box_arc3
question['box_arc4'] = box_arc4
question['box_rectangle1'] = box_rectangle1
question['box_rectangle2'] = box_rectangle2
question['author'] = author
question['text'] = content
question['vote'] = vote
y += box_height + BOX_SPACING
canvas_height += box_height + BOX_SPACING
if i == MAX_QUESTIONS - 1:
break
current_questions.reverse()
canvas.configure(scrollregion=(0, 0, WINDOW_WIDTH,
canvas_height + BOX_SPACING))
canvas.yview_moveto(1.0)
canvas.after(1000, update_questions)
root.bind(BORDERLESS_HOTKEY, toggle_borderless)
root.after(3000, update_questions)
root.mainloop()
driver.quit()