-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
365 lines (299 loc) · 13.2 KB
/
Copy pathmain.py
File metadata and controls
365 lines (299 loc) · 13.2 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
import os,time
try:
import threading, subprocess, base64, cv2, random, requests
import numpy as np
except:
os.system("pip install --force-reinstall --no-cache opencv-python")
os.system("pip install numpy")
os.system("pip install requests")
import threading, subprocess, base64, cv2, random, hashlib, sys, requests
import numpy as np
from xml.dom.minidom import parse
try:
import uiautomator2 as u2
except:
os.system("pip install uiautomator2")
import uiautomator2 as u2
from uiautomator2 import Direction
def delay(delay_time: float):
time.sleep(delay_time)
def bypass_slide(devices):
pipe = subprocess.Popen(f'adb -s {devices} exec-out screencap -p',
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
#image_bytes = pipe.stdout.read().replace(b'\r\n', b'\n')
image_bytes = pipe.stdout.read()
image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
# img = image[430:765, 102:648] # cắt chỗ có captcha # cut zone captcha
img = image[360:580, 100:440]
# img = image[400:1505, 80:1248]
#cv2.imshow("a", img)
#cv2.waitKey(0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img3 = cv2.Canny(gray, 200, 200, L2gradient=True)
kernel = np.ones([23,23]) # Tạo kernel
kernel[2:,2:] = -0.1
im = cv2.filter2D(img3/255, -1, kernel)
im1 = im[:,:125]
y1,x1 = np.argmax(im1)//im1.shape[1], np.argmax(im1)%im1.shape[1] # Tìm vị trí 1 chính xác
im2 = im[:,125:]
y2,x2 = np.argmax(im2)//im2.shape[1], np.argmax(im2)%im2.shape[1] + 125 # Tìm vị trí 1 chính xác
# cv2.rectangle(img, (x1,y1), (x1+50, y1+50), 255, 2)
# cv2.rectangle(img, (x2,y2), (x2+50, y2+50), 255, 2)
# plt.imshow(img)
# plt.show()
return x2-x1
class Auto:
def __init__(self, device_id):
self.device = u2.connect(device_id)
# Kiểm tra app có mở hay chưa
def check_is_opened(self):
if ("com.zhiliaoapp.musically" == self.device.app_current()['package']):
return True
return False
# Mở app
def open_app(self):
self.device.app_start(package_name="com.zhiliaoapp.musically", wait=True)
# Đóng app
def close_app(self):
self.device.app_stop("com.ss.android.ugc.trill")
# Lướt video
def swipe_video(self, video: int):
i = 1
while i <= video:
# self.device.settings['wait_timeout'] = 10.0
delay(10.0)
self.device.swipe_ext('up', scale=random.uniform(0.8, 1.0))
i += 1
# Thích video
def like_video(self):
if self.device(resourceId="com.ss.android.ugc.trill:id/deu").exists:
self.device(resourceId="com.ss.android.ugc.trill:id/deu").click()
# Bình luận video
def comment_video(self):
if self.device(resourceId="com.ss.android.ugc.trill:id/cf4").exists:
self.device(resourceId="com.ss.android.ugc.trill:id/cf4").click()
# Lướt bình luận
def swipe_comments(self, time: int):
i = 1
while i <= time:
self.device.swipe_ext(Direction.FORWARD, scale=0.8)
time.sleep(5.0)
i += 1
# Tìm kiếm chủ đề theo từ khóa
def find_key_word(self, text: str):
# self.device.xpath('//*[@resource-id="com.ss.android.ugc.trill:id/mi7"]/android.widget.ImageView[2]').click()
self.device.click(0.941, 0.058)
for char in text:
time.sleep(1.5)
self.device.send_keys(char)
time.sleep(random.uniform(0.08, 0.3))
time.sleep(5.0)
self.device.click(0.907, 0.049)
# Trở về
def back(self):
self.device.press('back')
# Tiếp tục tìm kiếm và lướt video
def keep_find_key_word(self, text: str):
for char in text:
time.sleep(1.0)
self.device.send_keys(char)
time.sleep(random.uniform(0.08, 0.3))
time.sleep(5.0)
self.device.click(0.907, 0.049)
time.sleep(3.0)
self.device.swipe_ext(Direction.FORWARD, scale=random.uniform(0.8, 1.0))
self.device.swipe_ext(Direction.FORWARD, scale=random.uniform(0.8, 1.0))
# Xem live
def watch_live(self):
# self.device.xpath('//*[@resource-id="com.ss.android.ugc.trill:id/mi7"]/android.widget.ImageView[1]').click()
time.sleep(10.0)
self.device.swipe_ext(Direction.FORWARD)
time.sleep(random.randint(5, 15))
self.device.swipe_ext(Direction.FORWARD)
time.sleep(random.randint(5, 20))
# Bình luận live
self.device(resourceId="com.ss.android.ugc.trill:id/dx8").click()
time.sleep(2.0)
for char in "Nice bro":
self.device._send_keys_with_ime(char)
time.sleep(random.uniform(0.08, 0.3))
self.device.press('enter')
# Vào xem giỏ hàng
def click_store(self, watch: int):
self.device(text="Cửa hàng").click()
time.sleep(9.0)
while self.device.xpath('//*[@resource-id="com.ss.android.ugc.trill:id/fhp"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/com.lynx.component.svg.UISvg[1]').exists:
self.device.xpath('//*[@resource-id="com.ss.android.ugc.trill:id/fhp"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/com.lynx.component.svg.UISvg[1]').click()
i = 1
while i <= watch:
self.device.swipe_ext(Direction.FORWARD, scale=random.uniform(0.7, 1.0))
time.sleep(random.randint(2, 5))
i += 1
# Vào trang cá nhân
def click_profile(self):
# self.device(text="Hồ sơ").click()
# time.sleep(5.0)
# self.device.xpath('//*[@resource-id="com.ss.android.ugc.trill:id/kh_"]/android.widget.ImageView[2]').click()
# time.sleep(1.0)
# self.device(resourceId="com.ss.android.ugc.trill:id/uy", text="Cài đặt và quyền riêng tư").click()
# self.device.swipe(500, 1500, 500, 500, duration=1)
# self.device(text="Ngôn ngữ").click()
# self.device(text="Ngôn ngữ ứng dụng").click()
# self.device.swipe_ext('up', scale=0.9)
# self.device(resourceId="com.ss.android.ugc.trill:id/r0s", text="Tiếng Việt").click()
self.device(resourceId="com.ss.android.ugc.trill:id/kh_").click()
# Đóng tất cả app chạy ngầm
def close_recent_apps(self):
self.device.press('recent')
if self.device(resourceId="com.sec.android.app.launcher:id/clear_all_button").exists:
self.device(resourceId="com.sec.android.app.launcher:id/clear_all_button").click()
time.sleep(2.0)
self.device.press('home')
# Mở app check proxy
def open_proxy_app(self):
self.device.xpath('//*[@content-desc="Show My IP Address"]/android.widget.ImageView[1]').click()
# time.sleep(1.0)
# Trở về home
def return_home(self):
self.device.press('home')
# Check pop-up window
def check_window(self):
if self.device(text="Allow access to phone data?").exists:
return True
return False
# click OK
def click(self):
self.device(resourceId="android:id/button1").click()
# Kiểm tra có xuất hiện bảng bạn bè?
def check_friends(self):
if self.device(resourceId="com.ss.android.ugc.trill:id/lyc", text="Follow bạn bè của bạn").exists or self.device(resourceId="com.zhiliaoapp.musically:id/lyc", text="Follow bạn bè của bạn").exists:
self.device.click(0.794, 0.207)
# Kiểm tra app đang chạy
def apps_running(self):
print(self.device.app_current())
def get_devices():
devices = subprocess.check_output("adb devices")
p = str(devices).replace("b'List of devices attached","").replace('\\r\\n',"").replace(" ","").replace("'","").replace('b*daemonnotrunning.startingitnowonport5037**daemonstartedsuccessfully*Listofdevicesattached',"")
if int(len(p)) > 0:
listDevices = p.split("\\tdevice")
listDevices.pop()
return listDevices
else:
return
devices_list = get_devices()
thread_count = len(devices_list)
class starts(threading.Thread):
def __init__(self, device):
super().__init__()
self.device = device
def run(self):
try:
d = Auto(self.device)
d3 = ADB(self.device)
if not d.device.info:
print(f"Thiết bị {self.device} không sẵn sàng.")
return
# Xóa proxy
# d3.remProxy()
# d.open_proxy_app()
# while d.check_window():
# d.click()
# delay(10.0)
# if d.check_is_opened() == False:
# d.open_app()
# delay(9.0)
# d.check_friends()
# delay(10.0)
# d.swipe_video(53)
except Exception as e:
print(f"Lỗi khi chạy trên thiết bị {self.device}: {e}")
def main(m):
run = starts(devices_list[m])
run.start() # Sửa lại từ run.run() thành run.start()
class ADB:
def __init__(self, handle):
self.handle = handle
def screen_capture(self):
#os.system(f'adb -s {self.handle} exec-out screencap -p > {name}.png')
pipe = subprocess.Popen(f'adb -s {self.handle} exec-out screencap -p',
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, shell=True)
#image_bytes = pipe.stdout.read().replace(b'\r\n', b'\n')
image_bytes = pipe.stdout.read()
image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
return image
def swipe(self, x1, y1, x2, y2):
subprocess.call(f"adb -s {self.handle} shell input touchscreen swipe {x1} {y1} {x2} {y2} 1000", stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
def find(self,img='',threshold=0.99):
img = cv2.imread(img) #sys.path[0]+"/"+img)
img2 = self.screen_capture()
result = cv2.matchTemplate(img,img2,cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= threshold)
retVal = list(zip(*loc[::-1]))
#image = cv2.rectangle(img2, retVal[0],(retVal[0][0]+img.shape[0],retVal[0][1]+img.shape[1]), (0,250,0), 2)
#cv2.imshow("test",image)
#cv2.waitKey(0)
#cv2.destroyWindow("test")
return retVal
def slideCaptcha(self,x,y):
# adb.excuteAdb(sr, "adb shell screencap -p /sdcard/cap.png")
# adb.excuteAdb(sr, f"adb pull /sdcard/cap.png {sr}/captcha.png")
captcha = bypass_slide(self.handle)
self.swipe(round(x), round(y), int(x)+int(captcha), round(y))
return True
# Restart wifi
def restart_wifi(self):
subprocess.run(f'adb -s {self.handle} shell svc wifi disable && adb -s {self.handle} shell svc wifi enable', shell=True, check=True)
print(f'{self.handle} restart wifi succeed!')
def chage_rotation(self):
subprocess.call(f'adb -s {self.handle} shell settings put system user_rotation 1')
print(f'Device {self.handle} change rotation succeed')
# Đổi proxy
def changeProxy(self, ip):
"""
Input Proxy Http IP:PORT
Thêm Proxy Http IP:PORT
"""
subprocess.call(f'adb -s {self.handle} shell settings put global http_proxy {ip}', stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
# Xóa proxy
def remProxy(self):
"""
Input Proxy Http IP:PORT
Thêm Proxy Http IP:PORT
"""
subprocess.call(f'adb -s {self.handle} shell settings put global http_proxy :0', stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
print(f'{self.handle} remove proxy succeed')
# Kiểm tra proxy
def get_proxy(self):
proxy = subprocess.check_output(f"adb -s {self.handle} shell settings get global http_proxy", shell=True).decode().strip()
print(f'{self.handle} có proxy: {proxy if proxy else "Không có proxy"}')
def assign_proxies(proxy_file):
"""
Đọc danh sách proxy từ tệp và gán proxy cho từng thiết bị.
"""
if not devices_list:
print("Không có thiết bị nào được kết nối.")
return
try:
with open(proxy_file, "r") as f:
proxies = [line.strip() for line in f.readlines() if line.strip()]
if len(proxies) < len(devices_list):
print("Cảnh báo: Số lượng proxy ít hơn số lượng thiết bị. Một số thiết bị sẽ không có proxy.")
for i, device in enumerate(devices_list):
if i < len(proxies):
proxy = proxies[i]
adb = ADB(device)
adb.changeProxy(proxy)
print(f"Đã gán proxy {proxy} cho thiết bị {device}")
else:
print(f"Không đủ proxy cho thiết bị {device}, bỏ qua.")
except Exception as e:
print(f"Lỗi khi gán proxy: {e}")
if __name__ == "__main__":
# Đọc proxy từ file và gán cho từng thiết bị trước khi chạy
assign_proxies("proxy.txt")
for m in range(thread_count):
main(m)