forked from AivanF/ID-Detective-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_vk.py
191 lines (164 loc) · 5.73 KB
/
worker_vk.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
import os
from os.path import basename, join
import datetime, time
import requests, json
import numpy as np
from utils import *
from handler import load_person, load_people
from vk import VkAccess, f_get_user_photos, f_kwargs_vk
from faces import read_files, find_faces
# Bytes to NumPy array
# https://stackoverflow.com/q/50630045/5308802
# NumPy array from bytes
# https://stackoverflow.com/a/11760170/5308802
@multi_all
def person2dict(user_id, results=None, **kwargs):
def f_save_face(user_id, name, values, kwargs):
results.append((user_id, name, values))
load_person(user_id, **kwargs, f_get_user_photos=f_get_user_photos,
f_save_face=f_save_face, f_exists_user_id=None, f_commit=None)
TASK_STATUSES = {
0: 'task is 0k',
1: 'the worker was not initialised',
2: 'the range is negative',
3: 'completed this range',
4: 'the task is expired',
}
VK_API_ERRORS = ['no access to call', 'invalid session', 'Too many', 'server error']
def check_task_status(commands, b=None):
if commands['vk_task_id'] < 0:
return 1
elif commands['vk_from'] < 0:
return 2
elif commands['vk_from'] >= commands['vk_to']:
return 3
elif time.time() > commands['vk_expiration']:
return 4
else:
return 0
def process_vk_tasks(commands, params):
st = check_task_status(commands)
if st != 0:
reason = TASK_STATUSES.get(st, '[error]')
if 'nonew' in params and params['nonew'] == 'nonew':
commands['must_work'] = False
print('Completed the task and shutting down')
return
log_inf('Requesting Central because: {}'.format(reason), 1)
url = 'http://' + join(params['center'], 'Vk/GetTask/') + '?name=' + params['name']
try:
response = requests.get(url)
data = response.content.decode('utf-8')
if response.status_code == 200:
data = json.loads(data)
if 'sleep' in data:
commands['must_work'] = False
print('Server asked to deactivate.')
elif 'id' in data:
commands['vk_task_id'] = data['id']
commands['vk_from'] = data['down']
commands['vk_to'] = data['up']
commands['vk_expiration'] = data['expire']
commands['dones'] = 0
if 'comment' in data:
commands['vk_comment'] = data['comment']
else:
log_er('GetTask no id, data:<br>\n{}'.format(data), 1)
else:
log_er('GetTask bad code: {}, data:<br>\n{}'.format(response.status_code, data), 1)
except requests.exceptions.ConnectionError as ex:
incr(commands, 'Vk/GetTask-error')
if check_task_status(commands) != 0:
# incr(commands, 'Vk/GetTask-still-no')
return
if params['vk_tokens'] is None or len(params['vk_tokens']) == 0:
log_er('Vk token is None!', 1)
return
# Process subrange, save indices.
down = commands['vk_from']
up = commands['vk_to']
up = min(up, down + params['pack_size'])
print('Doing task #{} from {} to {}, the goal is {}'.format(commands['vk_task_id'], down, up, commands['vk_to']))
people = list(range(down, up))
results = []
pro = Progress(len(people), change=9, key='vk.proc')
load_people(people, person2dict, pro,
kwargs={'results':results},
cyclic_args={'vka':params['vk_tokens']},
kwargs_processors=[f_kwargs_vk],
threads_count=params.get('threads_count', None))
api_errors = dfindsum(pro.notes, VK_API_ERRORS)
# if len(results) > 0:
if api_errors < (up-down) / 3:
log_inf('- Worker Vk: got {} API errors.'.format(api_errors))
done = {
'vk_task_id': commands['vk_task_id'],
'vk_from': down,
'vk_to': up,
'data': results,
'notes': pro.notes
}
name = '{} {} {} {}-{}.dat'.format(params['name'], commands['vk_task_id'], str(commands['dones']).zfill(3), down, up)
path = join(params['done'], name)
save_pickle(path, done)
commands['vk_from'] = up
incr(commands, 'dones')
else:
# log_er('- Worker Vk Error: completely no faces extracted! Killing process.'.format())
log_er('- Worker Vk Error: too many API errors ({})! Killing process. Notes:\n{}'.format(api_errors, pro.notes))
commands['must_work'] = False
def try_upload(name, path, commands, params):
data = load_pickle(path)
if isinstance(data, dict):
if 'vk_task_id' in data:
# label = '- Found done file {}:'.format(name)
# label += '\n{}:{} #{}'.format(data['vk_from'], data['vk_to'], data['vk_task_id'])
# label += '\nRows: {}'.format(len(data['data']))
# print(label)
# Delete if success
# and update expiration date.
url = 'http://' + join(params['center'], 'Vk/SubmitTask/')
files = {name + '.dat': open(path,'rb')}
values = {'name': params['name']}
try:
response = requests.post(url, files=files, data=values)
data = response.content.decode('utf-8')
if response.status_code == 200:
data = json.loads(data)
if data['code'] == 1:
os.remove(path)
print('Loaded file {}'.format(name))
elif data['code'] == 3:
# This should never happen
os.remove(path)
temp = 'Deleted mal file {}'.format(name)
log_er(temp)
print(temp)
elif data['code'] == 0:
commands['must_work'] = False
print('Server asked to deactivate.')
else:
print('Got data: {}'.format(data))
return data['code']
else:
log_er('SubmitTask bad code: {}, data:<br>\n{}'.format(response.status_code, data), 1)
return response.status_code
except requests.exceptions.ConnectionError as ex:
incr(commands, 'Vk/SubmitTask-error')
else:
return -1
# data_old = data[0]
# data_new = np.fromstring(data_old.tobytes())
# print('Dif:')
# print(np.subtract(data_new, data_old).sum())
def process_vk_submit(commands, params):
# print('- process_submit:')
# print(datetime.datetime.now())
completed = find_filenames(params['done'], '.dat')
if len(completed) == 0:
return
names = list(completed.keys())
names.sort()
for name in names:
if name.split(' ')[0] == params['name']:
try_upload(name, completed[name], commands, params)