def store_activites(): with application.app_context(): fitbit_secret_token = config.get('config', 'fitbit_secret_token') stats_query_string = f'https://api.fitbit.com/1/user/-/activities/date/{time_helper.get_local_date()}.json' secret_header = { 'Authorization': 'Bearer {}'.format(fitbit_secret_token) } response = requests.get(stats_query_string, headers=secret_header) weight_query_string = f'https://api.fitbit.com/1/user/-/body/log/weight/goal.json' weight_response = requests.get(weight_query_string, headers=secret_header) sleep_goal_query_string = f'https://api.fitbit.com/1/user/-/sleep/goal.json' sleep_goal_response = requests.get(sleep_goal_query_string, headers=secret_header) sleep_query_string = f'https://api.fitbit.com/1.2/user/-/sleep/list.json?beforeDate={time_helper.get_local_date()}&sort=desc&offset=0&limit=1' sleep_response = requests.get(sleep_query_string, headers=secret_header) #data = response.json() json_data = json.loads(response.text) weight_json_data = json.loads(weight_response.text) sleep_goal_json_data = json.loads(sleep_goal_response.text) sleep_json_data = json.loads(sleep_response.text) json_data['date'] = time_helper.get_local_date() json_data['startWeight'] = weight_json_data['goal']['startWeight'] json_data['weight'] = weight_json_data['goal']['weight'] json_data['sleepGoalMinutes'] = sleep_goal_json_data['goal'][ 'minDuration'] json_data['sleepMinutes'] = sleep_json_data['sleep'][0]['timeInBed'] json_data['sleepMinutesAwake'] = sleep_json_data['sleep'][0][ 'minutesAwake'] fitbit_stats_db.update({'date': f'{json_data["date"]}'}, json_data, upsert=True) result = Stats.stats_from_dict(json_data) activity_query_string = f'https://api.fitbit.com/1/user/-/activities/list.json?afterDate={time_helper.get_local_date()}&sort=desc&offset=0&limit=1' secret_header = { 'Authorization': 'Bearer {}'.format(fitbit_secret_token) } response = requests.get(activity_query_string, headers=secret_header) json_data = json.loads(response.text) json_data['date'] = time_helper.get_local_date() if fitbit_activity_db.find({'date': f'{json_data["date"]}'}).count(): fitbit_activity_db.update({'date': f'{json_data["date"]}'}, json_data) else: fitbit_activity_db.insert(json_data) result = Activity.activity_from_dict(json_data) print(result)
def get(self): data = [] for step_record in mfp_db.find({}, {"date": 1}).sort('date', -1): del step_record["_id"] data.append(int(step_record["date"].replace('-', ''))) current = int(time_helper.get_local_date().replace('-', '')) while current != data[0]: data.pop(0) data.pop(0) counter = 1 for date_entry in data: if current - date_entry == 1: counter = counter + 1 current = date_entry else: break json_returned = {} json_returned["streak"] = counter return jsonify(json_returned)
def create_monthly_budgets(application, db_client): with application.app_context(): current_month = time_helper.get_local_date_month() if db_client.find({'month': current_month}).count() == 0: necessities_budget_json = {} necessities_budget_json["name"] = "Necessities" necessities_budget_json["budget"] = 1600 necessities_budget_json["current"] = 0 necessities_budget_json["month"] = current_month necessities_budget_json["transactions"] = [] rent = {} rent["name"] = "Rent" rent["amount"] = 678 rent["date"] = time_helper.get_local_date() necessities_budget_json["transactions"].append(rent) luxury_budget_json = {} luxury_budget_json["name"] = "Luxury" luxury_budget_json["budget"] = 750 luxury_budget_json["current"] = 0 luxury_budget_json["month"] = current_month luxury_budget_json["transactions"] = [] cinema = {} cinema["name"] = "Cinema" cinema["amount"] = 13.5 cinema["date"] = time_helper.get_local_date() luxury_budget_json["transactions"].append(cinema) savings_budget_json = {} savings_budget_json["name"] = "Savings" savings_budget_json["budget"] = 500 savings_budget_json["current"] = 0 savings_budget_json["month"] = current_month savings_budget_json["transactions"] = [] db_client.insert(necessities_budget_json) db_client.insert(luxury_budget_json) db_client.insert(savings_budget_json)
def get(self): data = [] records = [] for mfp_record in mfp_db.find({}, { "date": 1, "calories": 1 }).sort('date', -1): del mfp_record["_id"] data.append(int(mfp_record["date"].replace('-', ''))) records.append(mfp_record) current = int(time_helper.get_local_date().replace('-', '')) while current != data[0]: data.pop(0) records.pop(0) return jsonify(records)
def store_mfp(application, db_client, user, password): with application.app_context(): client = myfitnesspal.Client(user, password) numbers = time_helper.get_local_date_numbers() day = client.get_date(numbers[0], numbers[1], numbers[2]) daily_stats = day.totals data = json.loads(json.dumps(daily_stats)) weight = client.get_measurements( 'Weight', datetime.date(numbers[0], numbers[1], numbers[2])) value = list(weight.items())[0][-1] data["weight"] = json.dumps(value) data["date"] = time_helper.get_local_date() breakfast = day.meals[0] lunch = day.meals[1] dinner = day.meals[2] snacks = day.meals[3] totals = breakfast.totals entries = breakfast.entries breafast_json = {} breafast_json["totals"] = totals breafast_json["entries"] = [] for entry in entries: breafast_json["entries"].append(entry.get_as_dict()) data["breakfast"] = breafast_json totals = lunch.totals entries = lunch.entries lunch_json = {} lunch_json["totals"] = totals lunch_json["entries"] = [] for entry in entries: lunch_json["entries"].append(entry.get_as_dict()) data["lunch"] = lunch_json totals = dinner.totals entries = dinner.entries dinner_json = {} dinner_json["totals"] = totals dinner_json["entries"] = [] for entry in entries: dinner_json["entries"].append(entry.get_as_dict()) data["dinner"] = dinner_json totals = snacks.totals entries = snacks.entries snacks_json = {} snacks_json["totals"] = totals snacks_json["entries"] = [] for entry in entries: snacks_json["entries"].append(entry.get_as_dict()) data["snacks"] = snacks_json db_client.update({'date': f'{data["date"]}'}, data, upsert=True)