Skip to content
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

Save synced filenams and skip sync next times #454

Merged
Merged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"license": "MIT",
"private": true,
"scripts": {
"data:clean": "rm scripts/data.db GPX_OUT/* activities/* src/static/activities.json",
"data:clean": "rm scripts/data.db {GPX,TCX,FIT}_OUT/* activities/* src/static/activities.json",
"data:download:garmin": "python3 scripts/garmin_sync.py",
"data:analysis": "python3 scripts/gen_svg.py --from-db --type github --output assets/github.svg",
"build": "gatsby clean && gatsby build",
Expand Down
1 change: 1 addition & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
SQL_FILE = os.path.join(parent, "scripts", "data.db")
JSON_FILE = os.path.join(parent, "src", "static", "activities.json")
SYNCED_FILE_NAME = "imported.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's convenient to set to var to abs path with the name SYNCED_FILE, just like the line above. Then you can use it anywhere with ease.


# TODO: Move into nike_sync
BASE_URL = "https://api.nike.com/sport/v3/me"
Expand Down
12 changes: 12 additions & 0 deletions scripts/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from .db import Activity, init_db, update_or_create_activity


sys.path.append("..")
agassiyzh marked this conversation as resolved.
Show resolved Hide resolved
import utils
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from utils import save_synced_data_file_list



IGNORE_BEFORE_SAVING = os.getenv("IGNORE_BEFORE_SAVING", False)


Expand Down Expand Up @@ -78,25 +82,33 @@ def sync_from_data_dir(self, data_dir, file_suffix="gpx"):
if not tracks:
print("No tracks found.")
return

synced_files = []

for t in tracks:
created = update_or_create_activity(self.session, t.to_namedtuple())
if created:
sys.stdout.write("+")
synced_files.extend(t.file_names)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the record already synced before this patch, would the file_name be saved in SYNCED_FILE?

else:
sys.stdout.write(".")
sys.stdout.flush()

utils.save_synced_data_file_list(data_dir, synced_files)

self.session.commit()

def sync_from_app(self, app_tracks):
if not app_tracks:
print("No tracks found.")
return
print("Syncing tracks '+' means new track '.' means update tracks")
synced_files = []
for t in app_tracks:
created = update_or_create_activity(self.session, t)
if created:
sys.stdout.write("+")
synced_files.extend(t.file_names)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

else:
sys.stdout.write(".")
sys.stdout.flush()
Expand Down
5 changes: 5 additions & 0 deletions scripts/gpxtrackposter/track_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from .track import Track
from .year_range import YearRange

import utils
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -167,12 +169,15 @@ def _load_data_tracks(file_names, load_func=load_gpx_file):

@staticmethod
def _list_data_files(data_dir, file_suffix):
synced_files = utils.load_synced_file_list(data_dir)
data_dir = os.path.abspath(data_dir)
if not os.path.isdir(data_dir):
raise ParameterError(f"Not a directory: {data_dir}")
for name in os.listdir(data_dir):
if name.startswith("."):
continue
if name in synced_files:
continue
path_name = os.path.join(data_dir, name)
if name.endswith(f".{file_suffix}") and os.path.isfile(path_name):
yield path_name
36 changes: 36 additions & 0 deletions scripts/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import time
from datetime import datetime
from config import SYNCED_FILE_NAME
agassiyzh marked this conversation as resolved.
Show resolved Hide resolved

import pytz
import os
agassiyzh marked this conversation as resolved.
Show resolved Hide resolved

try:
from rich import print
Expand Down Expand Up @@ -104,3 +106,37 @@ def upload_file_to_strava(client, file_name, data_type):
print(
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}."
)


def save_synced_data_file_list(data_dir: str, file_list: list):
data_dir = os.path.abspath(data_dir)
synced_json_file = os.path.join(data_dir, SYNCED_FILE_NAME)

old_list = []
if os.path.exists(synced_json_file):
with open(synced_json_file, "r") as f:
try:
old_list = json.load(f)
except Exception as e:
print(f"json load {synced_json_file} \nerror {e}")
pass

with open(synced_json_file, "w") as f:
file_list.extend(old_list)

json.dump(file_list, f)


def load_synced_file_list(data_dir: str):
data_dir = os.path.abspath(data_dir)
synced_json_file = os.path.join(data_dir, SYNCED_FILE_NAME)

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

return []