|
4 | 4 | import sqlite_utils |
5 | 5 | from .utils import convert_xml_to_sqlite |
6 | 6 |
|
7 | | -EXPORT_XML = "apple_health_export/export.xml" |
8 | | - |
9 | 7 |
|
10 | 8 | @click.command() |
11 | 9 | @click.argument( |
@@ -33,14 +31,22 @@ def cli(export_zip, db_path, silent, xml): |
33 | 31 | raise click.ClickException( |
34 | 32 | "File is not a zip file. Use --xml if you are running against an XML file." |
35 | 33 | ) |
36 | | - # Ensure export.xml is in there |
| 34 | + # Ensure something.xml with <!DOCTYPE HealthData or <HealthData is there |
37 | 35 | filenames = {zi.filename for zi in zf.filelist} |
38 | | - if EXPORT_XML not in filenames: |
39 | | - raise click.ClickException( |
40 | | - "Zip file does not contain {}".format(EXPORT_XML) |
41 | | - ) |
42 | | - fp = zf.open(EXPORT_XML) |
43 | | - file_length = zf.getinfo("apple_health_export/export.xml").file_size |
| 36 | + export_xml_path = None |
| 37 | + for filename in filenames: |
| 38 | + if filename.count("/") == 1 and filename.endswith(".xml"): |
| 39 | + firstbytes = zf.open(filename).read(1024) |
| 40 | + if ( |
| 41 | + b"<!DOCTYPE HealthData" in firstbytes |
| 42 | + or b"<HealthData " in firstbytes |
| 43 | + ): |
| 44 | + export_xml_path = filename |
| 45 | + break |
| 46 | + if export_xml_path is None: |
| 47 | + raise click.ClickException("Zip file does not contain valid export.xml") |
| 48 | + fp = zf.open(export_xml_path) |
| 49 | + file_length = zf.getinfo(export_xml_path).file_size |
44 | 50 | db = sqlite_utils.Database(db_path) |
45 | 51 | if silent: |
46 | 52 | convert_xml_to_sqlite(fp, db, zipfile=zf) |
|
0 commit comments