-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_activities.py
More file actions
81 lines (64 loc) · 2.47 KB
/
Copy pathfilter_activities.py
File metadata and controls
81 lines (64 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Filter raw FIT files into a clean upload set.
Garmin's GDPR export bundles every uploaded file: workout activities
mixed with monitoring data (sleep, HR, steps), weight-scale readings,
and other non-activity records. This script reads the file_id header
of each FIT and keeps only those with type='activity'.
Usage:
.venv/bin/python filter_activities.py <staging_dir> <out_dir>
"""
import argparse
import os
import shutil
from collections import Counter
from pathlib import Path
from fitparse import FitFile
def file_type(path: Path) -> str:
try:
ff = FitFile(str(path))
for msg in ff.get_messages("file_id"):
return str(msg.get_value("type"))
except Exception as e:
return f"__error__:{type(e).__name__}"
return "__no_file_id__"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("staging", help="Directory of raw FIT files from Garmin export")
ap.add_argument("out", help="Output directory for activity-only FIT files")
ap.add_argument("--copy", action="store_true",
help="Copy files instead of symlinking (default: relative symlink)")
args = ap.parse_args()
staging = Path(args.staging).expanduser()
out = Path(args.out).expanduser()
out.mkdir(parents=True, exist_ok=True)
fits = sorted(p for p in staging.iterdir() if p.suffix.lower() == ".fit")
print(f"Scanning {len(fits)} files in {staging}...")
types = Counter()
activities = []
for i, p in enumerate(fits, 1):
t = file_type(p)
types[t] += 1
if t == "activity":
activities.append(p)
if i % 1000 == 0:
print(f" [{i}/{len(fits)}]")
print("\nFile type breakdown:")
for t, n in types.most_common():
print(f" {t:25s} {n}")
error_count = sum(n for t, n in types.items() if t.startswith("__error__"))
if error_count:
print(f"\nNote: {error_count} files could not be parsed and were skipped.")
print(f"\nKeeping {len(activities)} activity files. Linking into {out}...")
for src in activities:
dst = out / src.name
if dst.exists():
continue
if args.copy:
shutil.copy2(src, dst)
else:
# Relative symlink so moving the staging tree doesn't break links.
rel = Path(os.path.relpath(src.resolve(), dst.parent.resolve()))
dst.symlink_to(rel)
print(f"Done. {len(activities)} files ready in {out}")
if __name__ == "__main__":
main()