import csv import arrow import os import collections import json import re import matplotlib ref_time = arrow.get() data_dir = '/Users/fluffy/Library/Mobile Documents/iCloud~com~ifunography~HealthExport/Documents/' start_time, _ = ref_time.shift(days=-30).span('day') _, end_time = ref_time.span('day', bounds='[]') metrics = collections.defaultdict(list) def filter(date): if date >= start_time and date < end_time: return date return None def scan_dir(path): for file in os.scandir(path): if file.is_dir(): scan_dir(file.path) elif file.is_file(): scan_file(file) def scan_file(file): match = re.match(r'HealthAutoExport-([0-9\-]+)\.json', file.name) if not match: return file_date = arrow.get(match[1]) if filter(file_date): file_data = json.load(open(file.path,'r')) scan_metrics(file_data['data']['metrics']) def get_value(item): if 'qty' in item: return item['qty'] elif 'Avg' in item: return item['Avg'] elif 'asleep' in item: print(item) return None else: raise KeyError(f"unknown format; keys: {item.keys()}") def scan_metrics(data): for metric in data: if len(data): sink = metrics[metric['name']] for item in metric['data']: if when := filter(arrow.get(item['date'], 'YYYY-MM-DD HH:mm:ss Z')): sink.append((when, item)) scan_dir(data_dir) metrics = {k:sorted(v,key=lambda i:i[0]) for k,v in metrics.items() if len(v)} for key,vals in metrics.items(): print(key,len(vals))