Настроена повторная отправка фото при неудачной отправке

This commit is contained in:
Noretsa 2020-06-21 15:48:22 +03:00
parent 9e11dc7d26
commit ab72d01c5b

View File

@ -32,9 +32,12 @@ def send_image(session, headers, url, photo):
# print(files) # print(files)
img = session.post('https://knd.mosreg.ru//api/v1/executions/'+str(url)+'/images', headers=headers, files=files) img = session.post('https://knd.mosreg.ru//api/v1/executions/'+str(url)+'/images', headers=headers, files=files)
if img.status_code == 200: if img.status_code == 200:
print(f"Фото {photo} успешно отправлено")
return img.json() return img.json()
else: else:
print(f"Не удалось загрузить фото на сервер по заданию {url}, статус ошибки {img.status_code}! Завершите его вручную!") print(f"Не удалось загрузить фото на сервер по заданию {url}, статус ошибки {img.status_code}! Повторная попытка!")
second_img = send_image(session, headers, url, photo)
return second_img
def complete_task(session, headers, task, address): def complete_task(session, headers, task, address):
@ -67,7 +70,13 @@ def complete_task(session, headers, task, address):
for chain in data: for chain in data:
for item in chain['questions']: for item in chain['questions']:
if len(data) == len(photos): if len(data) == len(photos):
img = send_image(session, headers, task['id'], photos.pop(0)) photo_from_photos = photos.pop(0)
img = send_image(session, headers, task['id'], photo_from_photos)#photos.pop(0))
count_send_photo = 0
while img['id'] is None:
count_send_photo += 1
print("Попытка № " + str(count_send_photo) + "отправить фото" + str(photo_from_photos))
img = send_image(session, headers, task['id'], photo_from_photos)#photos.pop(0))
first = {"id": 18850, "answer": [img['id']], 'answer_status': None, 'start_time': int(time.time())} first = {"id": 18850, "answer": [img['id']], 'answer_status': None, 'start_time': int(time.time())}
time.sleep(2) time.sleep(2)
first.update({'end_time': int(time.time())}) first.update({'end_time': int(time.time())})
@ -77,7 +86,14 @@ def complete_task(session, headers, task, address):
item['question_components'].append(first) item['question_components'].append(first)
item['question_components'].append(second) item['question_components'].append(second)
else: else:
img = send_image(session, headers, task['id'], photos.pop(0)) #img = send_image(session, headers, task['id'], photos.pop(0))
photo_from_photos = photos.pop(0)
img = send_image(session, headers, task['id'], photo_from_photos)#photos.pop(0))
count_send_photo = 0
while img['id'] is None:
count_send_photo += 1
print("Попытка № " + str(count_send_photo) + " отправить фото" + str(photo_from_photos))
img = send_image(session, headers, task['id'], photo_from_photos)#photos.pop(0))
first = {"id": 18856, "answer": 77, 'answer_status': None, 'start_time': int(time.time())} first = {"id": 18856, "answer": 77, 'answer_status': None, 'start_time': int(time.time())}
time.sleep(2) time.sleep(2)
first.update({'end_time': int(time.time())}) first.update({'end_time': int(time.time())})