-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ui.py
93 lines (78 loc) · 2.71 KB
/
test_ui.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
import os, glob, json, re, time
DIR = "."
os.environ["ANDROID_HOME"] = DIR
print("ANDROID_HOME", os.environ["ANDROID_HOME"])
from uiautomator import device, Selector
import xml.etree.ElementTree as ET
info = device.info
print(json.dumps(info, indent=2, ensure_ascii=False))
def find_id(node, id):
n = node.find(f".//*[@resource-id='com.ismaker.android.simsimi:id/{id}']")
if n != None and "text" in n.attrib:
return n.attrib["text"]
return None
def click_uix():
while True:
root = ET.fromstring(device.dump())
profile = root.findall(".//*[@resource-id='com.ismaker.android.simsimi:id/layout_profile_main']")
if profile:
device.press.back()
continue
nodes = root.findall(".//*[@resource-id='com.ismaker.android.simsimi:id/item_search_result_title']")
if not nodes:
break
for i, node in enumerate(nodes[:1]):
bounds = list(map(int, re.findall("\d+", node.attrib["bounds"])))
x = (bounds[2]+bounds[0])//2
y = (bounds[3]+bounds[1])//2
device.click(x, y)
def parse_uix(uix):
root = ET.fromstring(uix)
nodes = root.findall(".//*[@resource-id='com.ismaker.android.simsimi:id/item_search_result_right_layout']")
data = []
for node in nodes:
question_1 = find_id(node, "item_search_result_dialog_question_1")
answer = find_id(node, "item_search_result_dialog_answer")
question_2 = find_id(node, "item_search_result_dialog_question_2")
user = find_id(node, "item_search_result_nickname")
loc = find_id(node, "item_search_result_flag")
time = find_id(node, "item_search_result_time")
bounds = list(map(int, re.findall("\d+", node.attrib["bounds"])))
if bounds[-1] >= info["displayHeight"]: continue
if not (question_1 and answer and question_2 and user): continue
texts = [question_1, answer, question_2]
data.append({
"texts": texts,
"user": user,
"loc": loc,
"time": time,
"bounds": bounds
})
return data
f = open("crawl_yeu.jsonl", "a", encoding="utf8")
data = []
pre_box = 0
i = 0
while True:
click_uix()
page = parse_uix(device.dump())
if not page:
continue
box = page[-1]
ey = box["bounds"][-1]
if box == pre_box:
break
pre_box = page[-1]
data += page
for d in page:
i += 1
d.pop("bounds")
f.write(json.dumps(d, ensure_ascii=False)+"\n")
print(i, d["texts"])
#d.drag(sx, sy, ex, ey)
result = device.drag(450, ey - 25, 450, 370)
print(result)
if not result:
break
time.sleep(0.01)
f.close()