-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutomatedControllingAndroid.py
executable file
·332 lines (311 loc) · 13.8 KB
/
AutomatedControllingAndroid.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
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
import uiautomator2 as u2
import time
import glob
import os
import re
import sqlite3
from aip import AipOcr
class OCR_detect:
# construct funtion of the ocr
def __init__(self, imgpath, sample_size):
'''
:param imgpath: the folder path of screenshots
:param sample_size: the number of screenshots
'''
self.imgpath = imgpath
self.sample_size = sample_size
self.config = {
'appId': '',# Your id
'apiKey': '',# Your api key
'secretKey': ''# Your secret key
}
self.client = AipOcr(**self.config)
def get_file_content(self, img):
with open(img, 'rb') as f:
return f.read()
def img_to_str(self, imgpath):
'''
:param imgpath: picture path
:return: the result of scan a picture
'''
img = self.get_file_content(imgpath)
res = self.client.basicGeneral(img)
if 'words_result' in res:
words = '\n'.join(w['words'] for w in res['words_result'])
# print(words)
return words
def traverse_file(self, rootdir):
'''
:param rootdir: traverse a folder and get files
:return: None
'''
ls = os.listdir(rootdir)
for i in range(0, len(ls)):
path = os.path.join(rootdir, ls[i])
if os.path.isfile(path) and (path.endswith('.jpg') or path.endswith('.png')):
os.remove(path)
def getname_title_region(self, string):
'''
This funtion a used to process the screen's first block and get the
users' name, title and their region
:param string:
:return:
'''
try:
name = ''
tmpstr = string.strip('\n')
# print(type(tmpstr))
# block = tmpstr.partition('猜您认识')[2].partition('忽略')[0]
if tmpstr.find('<猜您认识') >=0:
block = tmpstr.partition('<猜您认识')[2].partition('忽略')[0]
elif tmpstr.find('猜您认识') >=0:
block = tmpstr.partition('猜您认识')[2].partition('忽略')[0]
res = block.split('\n')
title = ''
# print(res)
name = res[1]
region = ''
for item in res[2:]:
if item.find('·') >= 0:
region = item.partition('·')[0]
else:
# if title == 'Not fill':
# title = ''
title = title + item.strip('\n')
if region.find('…') >= 0:
region = region[:region.find('…')]
if title.find('…') >= 0:
title = title[:title.find('…')]
return name, title, region
except Exception as e:
print(e)
def get_experiece(self, string):
ans = string.strip('\n')
exper = ans.partition('最新经历')[2].partition('教育经历')[0]
try:
workpattern = r'其他\d+个职位'
t = re.search(workpattern, exper)
exper = ans.partition('最新经历')[2].partition(t.group())[0]
except AttributeError as e:
print('')
res = exper.split('\n')
res = [x.strip() for x in res if x.strip() != '']
ls = []
for i in range(len(res)):
if res[i].find('查看完整档案') >= 0 or (res[i].find('且') >= 0 and res[i].split('且')[1] == ''):
continue
elif res[i].find('且') >= 0 and res[i].split('且')[1] != '':
ls.append(res[i].split('且')[1].strip(' '))
else:
ls.append(res[i].strip(' '))
return ls
def get_edu(self, string):
ans = string.strip('\n')
edu = ans.partition('教育经历')[2].partition('查看完整档案')[0]
try:
schpattern = r'其他\d+个学校'
s = re.search(schpattern, edu)
edu = ans.partition('教育经历')[2].partition(s.group())[0]
except AttributeError as e:
print('')
res = edu.split('\n')
res = [x.strip() for x in res if x.strip() != '']
ls = []
for i in range(len(res)):
if res[i].find('查看完整档案') >= 0 or (res[i].find('且') >= 0 and res[i].split('且')[1] == '') or (
res[i].find('回') >= 0 and res[i].split('回')[1] == ''):
continue
elif res[i].find('且') >= 0 and res[i].split('且')[1] != '':
ls.append(res[i].split('且')[1].split("\'")[0])
elif res[i].find('回') >= 0 and res[i].split('回')[1] != '':
ls.append(res[i].split('回')[1].split("\'")[0])
else:
ls.append(res[i].split("\'")[0])
return ls
def run(self):
conn = sqlite3.connect('persons.db')
# create table
try:
cursor = conn.cursor()
cursor.execute('create table if not exists person (id varchar(5) primary key, '
'name varchar(20), headline varchar(50), region varchar(10))')
cursor.execute('create table if not exists experience (id varchar(5) primary key, '
'title varchar(50), org varchar(50), p_id varchar(5),foreign key (p_id) '
'references person(id))')
cursor.execute('create table if not exists education (id varchar(5) primary key, school varchar(50), '
'major varchar(50), p_id varchar(5),foreign key (p_id) references person(id))')
except Exception as e:
print(e)
finally:
try:
cursor.execute('delete from person')
cursor.execute('delete from experience')
cursor.execute('delete from education')
except Exception as e:
print(e)
finally:
cursor.close()
conn.commit()
conn.close()
# insert records
eduid = 0
experid = 0
for i in range(self.sample_size):
try:
tmp = self.img_to_str(self.imgpath+'\\%s.jpg'%i)
conn = sqlite3.connect('persons.db')
edu = self.get_edu(tmp)
exper = self.get_experiece(tmp)
name, title, region = self.getname_title_region(tmp)
print(name + ',' + title + ',' + region)
print(exper)
print(edu)
ID = str(i)
person_query = 'insert into person values (\'%s\',\'%s\',\'%s\',\'%s\')' % (ID, name, title, region)
try:
cursor = conn.cursor()
cursor.execute(person_query)
print('insert name, title, region, ok')
major = ''
school = ''
exper_title = ''
org = ''
if len(edu) == 0:
edu_query = 'insert into education values(\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(eduid), '', '', ID)
cursor.execute(edu_query)
eduid += 1
elif len(edu) == 1:
edu_query = 'insert into education values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(eduid), edu[0], '', ID)
cursor.execute(edu_query)
eduid += 1
elif len(edu) % 2 == 0 and len(edu) != 0:
cnt = 0
for idx in range(len(edu)):
if idx % 2 == 0:
school = edu[idx]
cnt += 1
else:
major = edu[idx]
cnt +=1
if cnt == 2:
edu_query = 'insert into education values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(eduid), school, major, ID)
cursor.execute(edu_query)
eduid += 1
cnt = 0
elif len(edu) % 2 == 1 and len(edu) != 0:
cnt = 0
for idx in range(len(edu)):
if idx % 2 == 0:
school = edu[idx]
cnt += 1
else:
major = edu[idx]
cnt += 1
if cnt == 2:
edu_query = 'insert into education values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(eduid), school, major, ID)
cursor.execute(edu_query)
eduid += 1
break
print('insert edu ok')
if len(exper) == 0:
exper_query = 'insert into experience values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(experid), '', '', ID)
cursor.execute(exper_query)
experid += 1
elif len(exper) == 1:
exper_query = 'insert into experience values (\'%s\',\'%s\',\'%s\',\'%s\')' % (str(experid),
exper[0], '', ID)
cursor.execute(exper_query)
experid += 1
elif len(exper)>6:
cnt = 0
for idx in range(6):
if idx % 2 ==0:
exper_title = exper[idx]
cnt += 1
else:
org = exper[idx]
cnt += 1
if cnt == 2:
exper_query = 'insert into experience values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(experid), exper_title, org, ID)
cursor.execute(exper_query)
experid += 1
cnt = 0
elif len(exper) % 2 == 0 and len(exper) != 0 and len(exper)<=6:
cnt = 0
for idx in range(len(exper)):
if idx % 2 == 0:
exper_title = exper[idx]
cnt += 1
else:
org = exper[idx]
cnt += 1
if cnt == 2:
exper_query = 'insert into experience values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(experid), exper_title, org, ID)
cursor.execute(exper_query)
experid += 1
cnt = 0
elif len(exper) % 2 == 1 and len(exper) != 0 and len(exper)<=6:
cnt = 0
for idx in range(len(exper)):
if idx % 2 == 0:
exper_title = exper[idx]
cnt += 1
else:
org = exper[idx]
cnt += 1
if cnt == 2:
exper_query = 'insert into experience values (\'%s\',\'%s\',\'%s\',\'%s\')' % (
str(experid), exper_title, org, ID)
cursor.execute(exper_query)
experid += 1
break
print('insert exper ok')
except Exception as e:
eduid += 1
experid += 1
print(e)
finally:
cursor.close()
conn.commit()
conn.close()
except Exception as e:
print(e)
self.traverse_file(self.imgpath)
if __name__ == '__main__':
d = u2.connect('c8db0f7f') # alias for u2.connect_usb('123456789F')
print(d.info)
while True:
if d.toast.get_message(5.0):
times = int(d.toast.get_message(5.0).split(' ')[1])
break
time.sleep(2)
d.app_start("com.linkedin.android")
time.sleep(4)
d(text='人脉', className='android.widget.TextView').click()
time.sleep(2)
d(resourceId='com.linkedin.android:id/mynetwork_pymk_name', className='android.widget.TextView').click()
for i in range(times):
time.sleep(1)
d(scrollable=True).scroll.toEnd()
time.sleep(0.5)
d.screenshot('screenshot/' + str(i) + '.jpg')
d(scrollable=True).scroll.horiz.forward()
time.sleep(1)
d.app_start("uk.ac.lancs.myapplication")
time.sleep(1)
d.toast.show("Profiles collected successfully!", 2.0)
time.sleep(2)
d.toast.show("Now start scanning screenshots!", 2.0)
files = glob.glob(os.getcwd() + '\\screenshot\\*.jpg')
ocr = OCR_detect(os.getcwd() + '\\screenshot', times)
ocr.run()
d.push("persons.db", "/sdcard/Download/")
d.press("back")
d.toast.show("Now you can query information collected from LinkedIn!", 3.0)