def from_API(self, access_token, start_dt, end_dt): available_datasources = query_data_sources(access_token) datasets = defaultdict(lambda: {}) data_types_synced = set() data_sources_synced = set() for data_type_name, data_stream_id in available_datasources: if data_type_name not in GOOGLEFIT_SYNCED_DATATYPES: continue monthly_dataset = {} for i, dt in enumerate(rrule(DAILY, dtstart=start_dt, until=end_dt)): print("Getting {} for {}".format(data_stream_id, start_of_day(dt))) res = query_data_stream(access_token, data_stream_id, start_of_day(dt), end_of_day(dt)) res = remove_empty_buckets(res) monthly_dataset[dt.strftime("%Y-%m-%d")] = res datasets[data_type_name][data_stream_id] = monthly_dataset data_types_synced.add(data_type_name) data_sources_synced.add((data_type_name, data_stream_id)) metadata = { 'month': start_dt.strftime("%Y-%m"), 'last_dt': end_dt.strftime("%Y-%m-%d %H:%M:%S"), 'data_source_pairs': list(data_sources_synced), 'data_types': list(data_types_synced) } return GoogleFitData(datasets, metadata)
def find_first_date_with_data(access_token, end_dt): start_dt = find_first_month_with_data(access_token, end_dt) for i, dt in enumerate(rrule(DAILY, dtstart=start_dt, until=end_dt)): res = query_data_stream(access_token, 'com.google.active_minutes', start_of_day(dt), end_of_day(dt), bucketing=12 * HOURLY, aggregate_name="dataTypeName") is_empty = is_empty_aggregate_result(res) if is_empty: print("No data for {}, will look at later dates".format(dt)) else: return start_of_day(dt) return None
def generate_monthly_ranges(first_date, last_date): first_month_date = first_date while (True): last_month_date = end_of_month(first_month_date) if last_month_date >= last_date: yield first_month_date, last_date return yield first_month_date, last_month_date # get the first date for the next month first_month_date = start_of_day(last_month_date + timedelta(days=1))
def get_last_synced_data(oh_access_token, gf_access_token, current_date): download_url = get_latest_googlefit_file_url(oh_access_token) if download_url: existing_data_json = download_to_json(download_url) last_monthly_gf_data = GoogleFitData.from_json(existing_data_json) start_date = start_of_day( last_monthly_gf_data.last_dt ) # start of day to account for partial data in the last day else: last_monthly_gf_data = None start_date = find_first_date_with_data(gf_access_token, current_date) return last_monthly_gf_data, start_date
def find_first_month_with_data(access_token, end_dt): start_dt = GOOGLEFIT_DEFAULT_START_DATE for i, dt in enumerate(rrule(MONTHLY, dtstart=start_dt, until=end_dt)): res = query_data_stream(access_token, 'com.google.active_minutes', start_of_day(dt), end_of_day(dt + timedelta(days=32)), bucketing=12 * HOURLY, aggregate_name="dataTypeName") is_empty = is_empty_aggregate_result(res) if is_empty: print("No data for {}, will look at later dates".format(dt)) continue else: return start_of_month(dt) return None