def on_post(self, req, resp): document = read_json(req.stream, req.content_length) r = init_redis() r.sadd("members", document["name"]) r.hmset(document["name"], dict((k, v) for k, v in document.items() if k not in "name")) resp.status = falcon.HTTP_200 resp.body = "Success"
def main(): r = init_redis() while 1: for member in r.smembers("members"): person = r.hgetall(member) if person["device"] == "fitbit": creds = eval(person["credentials"]) authd_client = fitbit.Fitbit(**creds) response = authd_client.activities() r.hmset(time.strftime("%d%m%Y") + member, {"steps": response["summary"]["steps"]}) r.hmset(time.strftime("%d%m%Y") + member, {"caloriesBMR": response["summary"]["caloriesBMR"]}) r.hmset( time.strftime("%d%m%Y") + member, {"distances": response["summary"]["distances"][0]["distance"]} ) if person["device"] == "jawbone-up": http_url = "https://jawbone.com/nudge/api/v.1.1/users/@me/moves" headers = eval(person["credentials"]) headers["Accept-Header"] = "application/json" response = requests.get(http_url, headers=headers) resp_body = json.loads(response.content) # import pdb # pdb.set_trace() r.hmset( time.strftime("%d%m%Y") + member, { "steps": resp_body["data"]["items"][0]["details"]["steps"], "caloriesBMR": resp_body["data"]["items"][0]["details"]["bmr"], "distances": 1.6 * resp_body["data"]["items"][0]["details"]["km"], }, ) time.sleep(1800)
def on_get(self, req, resp): r = init_redis() resp_return = [] for member in r.smembers("members"): details = r.hgetall(time.strftime("%d%m%Y") + member) resp_return.append({"user": member, "data": details}) sorted_resp = sorted(resp_return, key=lambda k: k["data"]["steps"], reverse=True) resp.status = falcon.HTTP_200 resp.set_header("Access-Control-Allow-Origin", "*") resp.body = json.dumps(sorted_resp)