def put_nightscout(ns_format, entity): upload = requests.put(NS_URL + 'api/v1/' + entity + '?api_secret=' + NS_SECRET, json=ns_format, headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) print("Nightscout put status:", upload.status_code, upload.text)
def last_uploaded_nightscout_activity(activityType): latest = requests.get(NS_URL + 'api/v1/activity?find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[activityType]=' + urllib.parse.quote(activityType) + '&ts=' + str(time.time()), headers={ 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) j = latest.json() if j and len(j) > 0: return j[0] return None
def upload_nightscout_activity(ns_format): for ns in ns_format: upload = requests.post( NS_URL + 'api/v1/activity?api_secret=' + NS_SECRET, json=ns, headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) print("Nightscout upload status:", upload.status_code, upload.text)
def upload_ns(ns_format): upload = requests.post(NS_URL + 'api/v1/treatments?api_secret=' + NS_SECRET, json=ns_format, headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) print("Nightscout upload status:", upload.status_code, upload.text) return upload
def get_last_nightscout(): # last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[enteredBy]='+urllib.parse.quote(NS_AUTHOR) ) last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[enteredBy]=' + urllib.parse.quote(NS_AUTHOR), headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) # print(NS_URL , 'api/v1/treatments?count=1&find[enteredBy]=',urllib.parse.quote(NS_AUTHOR)) if last.status_code == 200: js = last.json() if len(js) > 0: return arrow.get(js[0]['created_at']).datetime
def get_from_nightscout(cnt): #last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[eventType]='+urllib.parse.quote('Exercise'), headers={ #last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[enteredBy]='+urllib.parse.quote(NS_AUTHOR), headers={ last = requests.get(NS_URL + 'api/v1/entries?count=' + str(cnt) + '&find[type]=sgv&find[device]=' + urllib.parse.quote(NS_AUTHOR), headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) if last.status_code == 200: js = last.json() return reversed(js)
def get_last_nightscout(): #last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[eventType]='+urllib.parse.quote('Exercise'), headers={ last = requests.get(NS_URL + 'api/v1/treatments?count=1&find[enteredBy]=' + urllib.parse.quote(NS_AUTHOR), headers={ 'Accept': 'application/json', 'Content-Type': 'application/json', 'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest() }) if last.status_code == 200: js = last.json() if len(js) > 0: duration = 0 if 'duration' in js[0]: duration = js[0]['duration'] return arrow.get( js[0]['created_at']).datetime + timedelta(minutes=duration)