knd_klinteplo/classes/desinfection.py
2022-01-19 18:38:00 +03:00

75 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import classes.create_photo as cr_photo
import os
import time
def assign_task(session, headers, task, address):
url = 'https://knd.mosreg.ru//api/v1/executions/'+str(task['id'])+'/assign'
task_assign = session.put(url, headers=headers)
if task_assign.status_code == 200:
print(f'Получили задание по адресу: {address}')
complete_task(session, headers, task, address)
else:
print(f"Не смогли получить задание {address} {task['id']}!")
def complete_task(session, headers, task, address):
photo_path = os.path.join(cr_photo.create_photo(address, headers['uid']), 'graph.jpg')
id_task = task['id']
# questions = get_questions(id_task, session, headers)
question_components = []
yes_no = {"id": 23384, "position": 0, "answer": 77, "start_time": int(time.time())}
time.sleep(2)
yes_no.update({"end_time": int(time.time())})
question_components.append(yes_no)
if not os.path.exists(photo_path):
print(f"Нет фотографии графика по адресу {address}")
else:
img = send_image(session, headers, task['id'], photo_path)#photos.pop(0))
image_graph = {"id": 23385, "position": 1, "answer": [img['id']], "start_time": int(time.time())}
time.sleep(2)
image_graph.update({"end_time": int(time.time())})
question_components.append(image_graph)
questions = [{"id": 7956, "question_components": question_components, "position": 0}]
question_chains = [{"id": 7396, "questions": questions}]
ans = {"question_chains": question_chains}
session_t = session
session_t.headers.update(
{
'referer': 'https://knd.mosreg.ru/executions'+str(task['id'])+'/solve',
'x-platform': 'android',
'accept': '*/*'
}
)
complete = session_t.post('https://knd.mosreg.ru//api/v1/executions/'+str(task['id'])+'/answers', headers=headers, json=ans)
if complete.status_code == 200:
print(f'Задание по адресу {address} выполнено успешно!\n\n')
else:
print(f'Задание по адресу {address} не выполненно! Статус ошибки {complete.status_code}\n\n')
def send_image(session, headers, url, photo):
"""Отправляем фото на сервер. Сервер должен вернуть id и url."""
files = {
'image': (photo[-5:], open(photo, 'rb')),
}
# print(files)
img = session.post('https://knd.mosreg.ru//api/v1/executions/'+str(url)+'/images', headers=headers, files=files)
if img.status_code == 200:
print(f"Фото {photo} успешно отправлено")
return img.json()
else:
print(f"Не удалось загрузить фото на сервер по заданию {url}, статус ошибки {img.status_code}! Повторная попытка!")
second_img = send_image(session, headers, url, photo)
return second_img
# def get_questions(id_task, session, headers):
# url = 'https://knd.mosreg.ru//api/v1/executions/'+str(id_task)+'/questions'
# questions = session.get(url, headers=headers)
# json_quest = questions.json()
# chain = json_quest['question_chains'][0].get('questions')
# print(chain[0].get('question_components'))
# return json_quest['selected_chains']