Skip to content

Commit

Permalink
Merge branch 'refs/heads/upstream'
Browse files Browse the repository at this point in the history
# Conflicts:
#	run_page/garmin_sync_cn_global.py
#	run_page/synced_data_file_logger.py
  • Loading branch information
ben-29 committed Nov 13, 2024
2 parents 13952ba + afe2b25 commit 6d2f0c1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 76 deletions.
50 changes: 7 additions & 43 deletions run_page/garmin_sync_cn_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,13 @@

import argparse
import asyncio
import logging
import os
import sys
import time
import traceback
import zipfile
from io import BytesIO

import aiofiles
import cloudscraper
import garth
import httpx

from config import FIT_FOLDER, GPX_FOLDER, JSON_FILE, SQL_FILE, config
from garmin_device_adaptor import wrap_device_info
from garmin_sync import Garmin, get_downloaded_ids
from garmin_sync import (
download_new_activities,
gather_with_concurrency,
get_activities_name,
)
from synced_data_file_logger import (
load_synced_activity_list,
save_synced_activity_list,
load_fit_name_mapping,
save_fit_name_mapping,
)
from garmin_sync import download_new_activities, gather_with_concurrency
from utils import make_activities_file

if __name__ == "__main__":
Expand Down Expand Up @@ -62,7 +43,9 @@
# If the activity is manually imported with a GPX, the GPX file will be synced

# load synced activity list
synced_activity = load_synced_activity_list()
downloaded_fit = get_downloaded_ids(FIT_FOLDER)
downloaded_gpx = get_downloaded_ids(GPX_FOLDER)
downloaded_activity = list(set(downloaded_fit + downloaded_gpx))

folder = FIT_FOLDER
# make gpx or tcx dir
Expand All @@ -74,7 +57,7 @@
download_new_activities(
secret_string_cn,
auth_domain,
synced_activity,
downloaded_activity,
is_only_running,
folder,
"fit",
Expand All @@ -83,21 +66,6 @@
loop.run_until_complete(future)
new_ids = future.result()

# Step 2:
# activity name not included in fit file
# manually fetch activity name, save to file for later use
old_name_mapping = load_fit_name_mapping()
new_ids = new_ids - old_name_mapping.keys()
if new_ids:
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(
get_activities_name(secret_string_cn, auth_domain, new_ids)
)
loop.run_until_complete(future)
names_mapping = future.result()
names_mapping.update(old_name_mapping)
save_fit_name_mapping(names_mapping)

to_upload_files = []
for i in new_ids:
if os.path.exists(os.path.join(FIT_FOLDER, f"{i}.fit")):
Expand All @@ -119,11 +87,7 @@
)
loop.run_until_complete(future)

# Save synced activity list for speeding up
synced_activity.extend(new_ids)
save_synced_activity_list(synced_activity)

# Step 3:
# Step 2:
# Generate track from fit/gpx file
make_activities_file(SQL_FILE, GPX_FOLDER, JSON_FILE, file_suffix="gpx")
make_activities_file(SQL_FILE, FIT_FOLDER, JSON_FILE, file_suffix="fit")
4 changes: 1 addition & 3 deletions run_page/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def sync_from_data_dir(self, data_dir, file_suffix="gpx"):
synced_files = []

for t in tracks:
created = update_or_create_activity(
self.session, t.to_namedtuple(run_from=file_suffix)
)
created = update_or_create_activity(self.session, t.to_namedtuple())
if created:
sys.stdout.write("+")
else:
Expand Down
5 changes: 5 additions & 0 deletions run_page/gpxtrackposter/tracks_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ def color(
diff = length_range.diameter()
if diff == 0:
return color1
if (
self.poster.length_range.upper() / 1000
< self.poster.special_distance["special_distance2"]
):
return color1

return interpolate_color(color1, color2, (length - length_range.lower()) / diff)
31 changes: 1 addition & 30 deletions run_page/synced_data_file_logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from config import SYNCED_FILE, SYNCED_ACTIVITY_FILE, NAME_MAPPING_FILE
from config import SYNCED_FILE, SYNCED_ACTIVITY_FILE
import json


Expand Down Expand Up @@ -27,32 +27,3 @@ def load_synced_file_list():
pass

return []


def load_synced_activity_list():
if os.path.exists(SYNCED_ACTIVITY_FILE):
with open(SYNCED_ACTIVITY_FILE, "r") as f:
try:
return json.load(f)
except Exception as e:
print(f"json load {SYNCED_ACTIVITY_FILE} \nerror {e}")
pass

return []


def load_fit_name_mapping():
if os.path.exists(NAME_MAPPING_FILE):
with open(NAME_MAPPING_FILE, "r") as f:
try:
return json.load(f)
except Exception as e:
print(f"json load {NAME_MAPPING_FILE} \nerror {e}")
pass

return {}


def save_fit_name_mapping(name_mapping: dict):
with open(NAME_MAPPING_FILE, "w") as f:
json.dump(name_mapping, f)

0 comments on commit 6d2f0c1

Please sign in to comment.