Skip to content

Commit e61e021

Browse files
Add other fields from GPS data (#37)
* Renaming docker-compose to compose: Thats the new nomenclature. Signed-off-by: Dipan Ghosh <5004441+dipanghosh@users.noreply.github.com> * Modified script to include additonal data with GPS. Testing. Signed-off-by: Dipan Ghosh <5004441+dipanghosh@users.noreply.github.com> * changed long to lon * update readme to refect compose.yml * Removed excessive comments --------- Signed-off-by: Dipan Ghosh <5004441+dipanghosh@users.noreply.github.com> Co-authored-by: Arpan Ghosh <26424944+arpanghosh8453@users.noreply.github.com>
1 parent a24260e commit e61e021

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Fitbit_Fetch.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,22 +553,50 @@ def get_tcx_data(tcx_url, ActivityID):
553553
else:
554554
root = ET.fromstring(response.text)
555555
namespace = {"ns": "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"}
556-
for trkpt in root.findall(".//ns:Trackpoint", namespace):
557-
time = trkpt.find("ns:Time", namespace)
556+
trackpoints = root.findall(".//ns:Trackpoint", namespace)
557+
prev_time = None
558+
prev_distance = None
559+
560+
for i, trkpt in enumerate(trackpoints):
561+
time_elem = trkpt.find("ns:Time", namespace)
558562
lat = trkpt.find(".//ns:LatitudeDegrees", namespace)
559563
lon = trkpt.find(".//ns:LongitudeDegrees", namespace)
560-
561-
if time is not None and lat is not None:
564+
altitude = trkpt.find("ns:AltitudeMeters", namespace)
565+
distance = trkpt.find("ns:DistanceMeters", namespace)
566+
heart_rate = trkpt.find(".//ns:HeartRateBpm/ns:Value", namespace)
567+
568+
if time_elem is not None and lat is not None:
569+
current_time = datetime.fromisoformat(time_elem.text.strip("Z"))
570+
fields = {
571+
"lat": float(lat.text),
572+
"lon": float(lon.text)
573+
}
574+
if altitude is not None:
575+
fields["altitude"] = float(altitude.text)
576+
if distance is not None:
577+
fields["distance"] = float(distance.text)
578+
current_distance = float(distance.text)
579+
else:
580+
current_distance = None
581+
if heart_rate is not None:
582+
fields["heart_rate"] = int(heart_rate.text)
583+
if i > 0 and prev_time is not None and prev_distance is not None and current_distance is not None:
584+
time_diff = (current_time - prev_time).total_seconds()
585+
distance_diff = current_distance - prev_distance
586+
if time_diff > 0:
587+
speed_mps = distance_diff / time_diff
588+
speed_kph = speed_mps * 3.6
589+
fields["speed_kph"] = speed_kph
590+
prev_time = current_time
591+
prev_distance = current_distance
592+
562593
collected_records.append({
563594
"measurement": "GPS",
564595
"tags": {
565596
"ActivityID": ActivityID
566597
},
567-
"time": datetime.fromisoformat(time.text.strip("Z")).astimezone(pytz.utc).isoformat(),
568-
"fields": {
569-
"lat": float(lat.text),
570-
"long": float(lon.text)
571-
}
598+
"time": datetime.fromisoformat(time_elem.text.strip("Z")).astimezone(pytz.utc).isoformat(),
599+
"fields": fields
572600
})
573601

574602
# Fetches latest activities from record ( upto last 50 )

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ You can use the Fitbit_Fetch_Autostart.service template to set up an auto-starti
6464

6565
Follow this [guide](https://dev.fitbit.com/build/reference/web-api/developer-guide/getting-started/) to create an application. This will give you a client ID, client secret, and a refresh token ( end step after following OAuth setup )
6666

67-
Initial setup : Create a folder named fitbit-fetch-data, cd into the folder, create a docker-compose.yml file with the below compose example ( Change the enviornment variables accordingly )
67+
Initial setup : Create a folder named fitbit-fetch-data, cd into the folder, create a compose.yml file with the below compose example ( Change the enviornment variables accordingly )
6868

6969
Create folders named logs and tokens inside and make sure to chown them for uid 1000 ( otherwise you may get read/write permission denied errors )
7070

@@ -151,7 +151,7 @@ The process is quite simple. you need to add an ENV variable and rerun the conta
151151

152152
- In the docker compose file, add a new ENV variable `AUTO_DATE_RANGE=False` under the `environment` section along with other variables. This variable switches the mode to bulk update instead of regular daily update
153153

154-
- Assuming you are already in the directory where the `docker-compose.yml` file is, run `docker compose run --rm fitbit-fetch-data` - this will run this container in _"remove container automatically after finish"_ mode which is useful for one time running like this. This will also attach the container to the shell as interactive mode, so don't close the shell until the bulk update is complete.
154+
- Assuming you are already in the directory where the `compose.yml` file is, run `docker compose run --rm fitbit-fetch-data` - this will run this container in _"remove container automatically after finish"_ mode which is useful for one time running like this. This will also attach the container to the shell as interactive mode, so don't close the shell until the bulk update is complete.
155155

156156
- After initialization, you will be requested to input the start and end dates in YYYY-MM-DD format. the format is very important so please enter the dates like this `2024-03-13`. Start date must be earlier than end date. The script should work for any given range, but if you encounter an error during the bulk update with large date range, please break the date range into one year chunks (maybe a few days less than one year just to be safe), and run it for each one year chunk one after another. I personally did not encounter any issue with longer date ranges, but this is just a heads up.
157157

File renamed without changes.

0 commit comments

Comments
 (0)