|
| 1 | +import os |
| 2 | +import xml.etree.ElementTree as ET |
| 3 | +from collections import defaultdict, namedtuple |
| 4 | + |
| 5 | +import pendulum |
| 6 | + |
| 7 | +from github_poster.loader.base_loader import BaseLoader |
| 8 | + |
| 9 | +# func is a lambda that converts the "value" attribute of the record to a numeric value. |
| 10 | +RecordMetadata = namedtuple("RecordMetadata", ["type", "unit", "track_color", "func"]) |
| 11 | + |
| 12 | + |
| 13 | +SUPPORTED_HEALTH_RECORD_TYPES = { |
| 14 | + "move": RecordMetadata( |
| 15 | + "HKQuantityTypeIdentifierActiveEnergyBurned", |
| 16 | + "kCal", |
| 17 | + "#ED619C", |
| 18 | + lambda x: float(x), |
| 19 | + ), |
| 20 | + "exercise": RecordMetadata( |
| 21 | + "HKQuantityTypeIdentifierAppleExerciseTime", "mins", "#D7FD37", lambda x: int(x) |
| 22 | + ), |
| 23 | + "stand": RecordMetadata( |
| 24 | + "HKCategoryTypeIdentifierAppleStandHour", |
| 25 | + "hours", |
| 26 | + "#62F90B", |
| 27 | + lambda x: 1 if "HKCategoryValueAppleStandHourStood" else 0, |
| 28 | + ), |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class AppleHealthLoader(BaseLoader): |
| 33 | + def __init__(self, from_year, to_year, _type, **kwargs): |
| 34 | + super().__init__(from_year, to_year, _type) |
| 35 | + self.number_by_date_dict = defaultdict(int) |
| 36 | + self.apple_health_export_file = kwargs.get("apple_health_export_file") |
| 37 | + self.apple_health_record_type = kwargs.get("apple_health_record_type") |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def add_loader_arguments(cls, parser, optional): |
| 41 | + parser.add_argument( |
| 42 | + "--apple_health_export_file", |
| 43 | + dest="apple_health_export_file", |
| 44 | + type=str, |
| 45 | + default=os.path.join("IN_FOLDER", "apple_health_export", "export.xml"), |
| 46 | + help="Apple Health export file path", |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--apple_health_record_type", |
| 50 | + dest="apple_health_record_type", |
| 51 | + choices=SUPPORTED_HEALTH_RECORD_TYPES.keys(), |
| 52 | + default="move", |
| 53 | + help="Apple Health Record Type", |
| 54 | + ) |
| 55 | + |
| 56 | + def make_track_dict(self): |
| 57 | + record_metadata = SUPPORTED_HEALTH_RECORD_TYPES[self.apple_health_record_type] |
| 58 | + self.__class__.unit = record_metadata.unit |
| 59 | + self.__class__.track_color = record_metadata.track_color |
| 60 | + |
| 61 | + in_target_section = False |
| 62 | + for _, elem in ET.iterparse(self.apple_health_export_file, events=["end"]): |
| 63 | + if elem.tag != "Record": |
| 64 | + continue |
| 65 | + |
| 66 | + if elem.attrib["type"] == record_metadata.type: |
| 67 | + in_target_section = True |
| 68 | + create_date = pendulum.from_format( |
| 69 | + elem.attrib["creationDate"], "YYYY-MM-DD HH:mm:ss ZZ" |
| 70 | + ) |
| 71 | + if ( |
| 72 | + create_date.year >= self.from_year |
| 73 | + and create_date.year <= self.to_year |
| 74 | + ): |
| 75 | + self.number_by_date_dict[ |
| 76 | + create_date.to_date_string() |
| 77 | + ] += record_metadata.func(elem.attrib["value"]) |
| 78 | + elif in_target_section: |
| 79 | + break |
| 80 | + |
| 81 | + elem.clear() |
| 82 | + |
| 83 | + self.number_by_date_dict = { |
| 84 | + k: int(v) for k, v in self.number_by_date_dict.items() |
| 85 | + } |
| 86 | + self.number_list = list(self.number_by_date_dict.values()) |
| 87 | + |
| 88 | + def get_all_track_data(self): |
| 89 | + self.make_track_dict() |
| 90 | + self.make_special_number() |
| 91 | + return self.number_by_date_dict, self.year_list |
0 commit comments