-
Notifications
You must be signed in to change notification settings - Fork 99
/
auto_scroll_single.py
199 lines (155 loc) · 6.44 KB
/
auto_scroll_single.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
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
import math
import threading
import time
import traceback
import pyautogui
import pyperclip
import win32con
import win32gui
from retry import retry
from win32api import GetSystemMetrics
import log
class AutoScrollSingle(threading.Thread):
def __init__(self, gui, search_hwnd, friend_name):
self.gui = gui
self.search_hwnd = search_hwnd
self.friend_name = friend_name
self.scrolling = False
self.resolutions = ['', '1920', '1600', '2560_125', '2560_175', '2560_100', '1366']
super().__init__()
@retry(tries=5, delay=2)
def find_moments_tab(self):
result = None
for resolution in self.resolutions:
try:
result = pyautogui.locateCenterOnScreen(f'resource/auto_gui/{resolution}/moments_tab.png',
grayscale=True, confidence=0.8)
break
except Exception as e:
log.LOG.warn("Can't find_moments_tab in resolution: " + resolution)
pass
if result is None:
raise Exception("Can 't find_moments_tab")
return result
@retry(tries=5, delay=2)
def find_search_button(self):
element = None
for resolution in self.resolutions:
try:
element = pyautogui.locateOnScreen(f'resource/auto_gui/{resolution}/search_button.png',
grayscale=True, confidence=0.8)
break
except Exception as e:
log.LOG.warn("Can't find_moments_tab in resolution: " + resolution)
pass
if element is None:
raise Exception("Can 't search_button")
return element
@retry(tries=5, delay=2)
def find_friends(self):
result = None
for resolution in self.resolutions:
try:
result = pyautogui.locateCenterOnScreen(f'resource/auto_gui/{resolution}/friends.png',
grayscale=True, confidence=0.8)
break
except Exception as e:
log.LOG.warn("Can't find_friends in resolution: " + resolution)
pass
if result is None:
raise Exception("Can 't find_friends")
return result
@retry(tries=5, delay=2)
def find_complete(self):
result = None
for resolution in self.resolutions:
try:
result = pyautogui.locateCenterOnScreen(f'resource/auto_gui/{resolution}/complete.png',
grayscale=True, confidence=0.8)
break
except Exception as e:
log.LOG.warn("Can't find_complete in resolution: " + resolution)
pass
if result is None:
raise Exception("Can 't find_complete")
return result
def run(self) -> None:
self.scrolling = True
try:
search_hwnd = win32gui.FindWindow('Chrome_WidgetWin_0', '微信')
wechat_hwnd = win32gui.FindWindow('WeChatMainWndForPC', '微信')
# 先把微信主窗口放置前台
win32gui.SetForegroundWindow(wechat_hwnd)
win32gui.ShowWindow(wechat_hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetWindowPos(wechat_hwnd, None, 100, 100, 0, 0, win32con.SWP_NOSIZE)
time.sleep(0.3)
# 先把搜一搜窗口放前台
win32gui.SetForegroundWindow(search_hwnd)
win32gui.ShowWindow(search_hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetWindowPos(search_hwnd, None, 50, 50, 0, 0, win32con.SWP_NOSIZE)
# 点击朋友圈三个字
x, y = self.find_moments_tab()
pyautogui.click(x, y)
time.sleep(0.1)
# 点击搜索按钮左侧
element = self.find_search_button()
pyautogui.click(element.left - 100, element.top + element.height / 2)
time.sleep(0.25)
# 输入字符
pyautogui.write('1')
time.sleep(0.25)
# 搜索
pyautogui.click(element.left + element.width / 2, element.top + element.height / 2)
time.sleep(1.5)
# 展开朋友
x, y = self.find_friends()
pyautogui.click(x, y)
time.sleep(0.5)
# 搜索好友
pyperclip.copy(self.friend_name)
time.sleep(0.25)
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.5)
# 回车
pyautogui.press('enter')
time.sleep(0.5)
# 点击完成
x, y = self.find_complete()
pyautogui.click(x, y)
time.sleep(0.25)
# 点击搜索按钮左侧
element = self.find_search_button()
pyautogui.click(element.left - 100, element.top + element.height / 2)
time.sleep(0.25)
pyautogui.press('backspace')
time.sleep(0.1)
pyautogui.press('backspace')
time.sleep(0.25)
pyperclip.copy('?')
time.sleep(0.25)
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.25)
element = self.find_search_button()
pyautogui.click(element.left + element.width / 2, element.top + element.height / 2)
time.sleep(1.0)
while self.scrolling:
element = self.find_search_button()
right_bottom = (element.left + element.width, element.top + element.height + 300)
pyautogui.scroll(-120)
pyautogui.click(right_bottom)
time.sleep(0.2)
search_hwnd = win32gui.FindWindow('Chrome_WidgetWin_0', '微信')
moments_hwnd = win32gui.FindWindow('SnsWnd', '朋友圈')
if search_hwnd and moments_hwnd:
# 调整位置朋友圈不要遮挡
width = GetSystemMetrics(0)
win32gui.SetWindowPos(moments_hwnd, None, 50, 50, 0, 0, win32con.SWP_NOSIZE)
win32gui.SetWindowPos(search_hwnd, None, 50, 50, 0, 0, win32con.SWP_NOSIZE)
except Exception:
traceback.print_exc()
def set_scrolling(self, scrolling: bool) -> None:
self.scrolling = scrolling
if not self.scrolling:
self.gui.working_note.pack_forget()
if self.scrolling and self.gui.working_note:
self.gui.working_note.pack()