-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Apple Health #47
Open
oefe
wants to merge
3
commits into
luka1199:master
Choose a base branch
from
oefe:apple-health
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
import ijson | ||
import json | ||
import os | ||
from progressbar import ProgressBar, Bar, ETA, Percentage | ||
import posixpath | ||
from progressbar import ProgressBar, NullBar, Bar, ETA, Percentage, Variable | ||
from utils import * | ||
import webbrowser | ||
from xml.etree import ElementTree | ||
|
@@ -97,7 +98,30 @@ def loadKMLData(self, file_name, date_range): | |
self.updateCoord(coords) | ||
pb.update(i) | ||
|
||
def loadZIPData(self, file_name, date_range): | ||
def loadGPXData(self, file_name, date_range, make_progress_bar=ProgressBar): | ||
"""Loads location data from the given GPX file. | ||
|
||
Arguments: | ||
file_name {string or file} -- The name of the GPX file | ||
(or an open file-like object) with the GPX data. | ||
date_range {tuple} -- A tuple containing the min-date and max-date. | ||
e.g.: (None, None), (None, '2019-01-01'), ('2017-02-11'), ('2019-01-01') | ||
make_progress_bar -- progress bar factory (specify NullBar to suppress progress bar) | ||
""" | ||
xmldoc = minidom.parse(file_name) | ||
gxtrack = xmldoc.getElementsByTagName("trkpt") | ||
w = [Bar(), Percentage(), " ", ETA()] | ||
|
||
with make_progress_bar(max_value=len(gxtrack), widgets=w) as pb: | ||
for trkpt in pb(gxtrack): | ||
lat = trkpt.getAttribute("lat") | ||
lon = trkpt.getAttribute("lon") | ||
coords = (round(float(lat), 6), round(float(lon), 6)) | ||
date = trkpt.getElementsByTagName("time")[0].firstChild.data | ||
if dateInRange(date[:10], date_range): | ||
self.updateCoord(coords) | ||
|
||
def loadGoogleTakeOutZIPData(self, file_name, date_range): | ||
""" | ||
Load Google location data from a "takeout-*.zip" file. | ||
""" | ||
|
@@ -133,6 +157,40 @@ def loadZIPData(self, file_name, date_range): | |
raise ValueError("unsupported extension for {!r}: only .json and .kml supported" | ||
.format(file_name)) | ||
|
||
def loadAppleHealthData(self, file_name, date_range): | ||
""" | ||
Load location data from an Apple Health Export file. | ||
""" | ||
with zipfile.ZipFile(file_name) as zip_file: | ||
namelist = zip_file.namelist() | ||
all_routes = fnmatch.filter(namelist, "apple_health_export/workout-routes/*.gpx") | ||
filtered_routes = [name for name in all_routes if dateInRange(posixpath.basename(name)[6:16], date_range)] | ||
|
||
w = [Bar(), Percentage(), " ", Variable('name', width=30), " ", ETA()] | ||
with ProgressBar(max_value=len(filtered_routes), widgets=w) as pb: | ||
for i, name in enumerate(filtered_routes): | ||
pb.update(i, name=posixpath.basename(name)) | ||
with zip_file.open(name) as read_file: | ||
self.loadGPXData(read_file, date_range, make_progress_bar=NullBar) | ||
|
||
def loadZIPData(self, file_name, date_range): | ||
""" | ||
Load location data from a zip file. | ||
|
||
Supported zip files: | ||
- Google Takekout | ||
- Apple Health export | ||
""" | ||
with zipfile.ZipFile(file_name) as zip_file: | ||
namelist = zip_file.namelist() | ||
if fnmatch.filter(namelist, "Takeout/*.html"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like that the files are detected by their names. Can't we detect them with |
||
self.loadGoogleTakeOutZIPData(file_name, date_range) | ||
elif fnmatch.filter(namelist, "apple_health_export/Export.xml"): | ||
self.loadAppleHealthData(file_name, date_range) | ||
else: | ||
raise ValueError("unsupported ZIP file {!r}: only Google Takeout and Apple Health supported" | ||
.format(file_name)) | ||
|
||
def updateCoord(self, coords): | ||
self.coordinates[coords] += 1 | ||
if self.coordinates[coords] > self.max_magnitude: | ||
|
@@ -184,6 +242,8 @@ def run(self, data_files, output_file, date_range, stream_data, tiles): | |
self.loadJSONData(json_file, date_range) | ||
elif data_file.endswith(".kml"): | ||
self.loadKMLData(data_file, date_range) | ||
elif data_file.endswith(".gpx"): | ||
self.loadGPXData(data_file, date_range) | ||
else: | ||
raise NotImplementedError( | ||
"Unsupported file extension for {!r}".format(data_file)) | ||
|
@@ -203,9 +263,12 @@ def run(self, data_files, output_file, date_range, stream_data, tiles): | |
parser = ArgumentParser(formatter_class=RawTextHelpFormatter) | ||
parser.add_argument( | ||
"files", metavar="file", type=str, nargs='+', help="Any of the following files:\n" | ||
"1. Your location history JSON file from Google Takeout\n" | ||
"2. Your location history KML file from Google Takeout\n" | ||
"3. The takeout-*.zip raw download from Google Takeout \nthat contains either of the above files") | ||
"- Your location history JSON file from Google Takeout\n" | ||
"- Your location history KML file from Google Takeout\n" | ||
"- The takeout-*.zip raw download from Google Takeout \n that contains either of the above files\n" | ||
"- A GPX file containing GPS tracks\n" | ||
"- An Export.zip file from Apple Health" | ||
) | ||
parser.add_argument("-o", "--output", dest="output", metavar="", type=str, required=False, | ||
help="Path of heatmap HTML output file.", default="heatmap.html") | ||
parser.add_argument("--min-date", dest="min_date", metavar="YYYY-MM-DD", type=str, required=False, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this path shouldn't be hard coded if it changes in the future.