import_ requests
import_ json
import_ time
import_ base64
import_ urllib3
urllib3.disable_warnings()
picture = open('test4.png', 'rb') # read picture
picture_read = picture.read()
base64_picture = base64.b64encode(picture_read) # save picture as base 64
print(len((str(base64_picture)[2:]))) # cutting off the first two characters from byte encoding, you may not need to
linux_time = time.time()
linux_time_millisecund = [item for item in str(linux_time) if item != '.'] # formatting time according to the linux era
# Below you have two variables in the form of a dictionary, which are protocols for logging in and sending data, this is what I sent you
earlier and comes from the creed page (with minor changes)
# data in the login in addition to the password and e-mail are free, if you registered a cell phone it will match.
login = {'app_version': '1', 'device_model': 'zero', 'device_type': 'raspberry','system_version': 'raspbian',
'device_id': 'raspberry','password': 'hasło', "email": "email"}
# in detection the data in addition to frame content and timestamp are also arbitrary, remember to limit the linux time to 13 characters as on the server,
otherwise you will have problems with the date.
detection = {"detections": [{"frame_content": '%s'% (str(base64_picture)[2:]), "timestamp": int(''.join(linux_time_millisecund)[:-4]), "latitude": 51.708491,
"longitude": 19.476362, "altitude": 12, "accuracy": 1, "provider": 'google map', "width": 45, "height": 45, "id": 1}],
"device_id": "raspberry", "androidVersion": 'none', "device_model": "zero", "app_version": '1',
"system_version": 'raspbian', "device_type": 'raspberry'}
# Once I've created variables, I save them in the json format.
with open("login.json", "w") as login_file:
json.dump(login, login_file)
with open("login.json", "r") as read_login:
data_login = json.load(read_login)
with open("detection.json", "w") as detection_file:
json.dump(detection, detection_file)
with open("detection.json", "r") as read_detection:
data_detetion = json.load(read_detection)
#request login is to get the authentication token needed to send the image to the server
request_login = requests.post('https://api.credo.science/api/v2/user/login', verify=False, json=data_login)
print(request_login.status_code, request_login.reason)
token = request_login.json()['token']
#in request detection you are sending a json file and a token code, and that's all, the file is on the server
request_detection = requests.post('https://api.credo.science/api/v2/detection', verify=False,
json=data_detetion, headers={'Authorization': 'Token %s' % token})
print(request_detection.status_code, request_detection.reason)
print(request_detection.content)