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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#!/bin/python
from openpyxl import *
from datetime import timedelta, datetime, time
import sqlite3
import sys
# constant
DB_FILE = "../strengthy.db"
USER_ID = 1
LEGDAY_WID = 1
PUSHDAY_WID = 3
PULLDAY_WID = 4
def fix_name(name):
name = name.strip()
if name == "Rope Tricep Pulldown":
return "Cable Tricep Pushdown"
elif name == "Cable Row":
return "Chest Row"
elif name == "Dumbell Shrugs":
return "Dumbbell Shrugs"
elif name == "Roller":
return "Ab Roller"
elif name == "Lateral Raise":
return "Delt Raise"
else:
return name
def parse_workout(workout_id, rows):
# Iterate over exercises
sets = []
for row in rows:
# Only get rows that have an exercise
name = sheet.cell(row, 1).value
if not name:
continue
name = fix_name(name)
# Iterate over sets
for col in range(2, 7):
lbs = sheet.cell(row, col).value
reps = sheet.cell(row + 1, col).value
if (
lbs
and reps
and not isinstance(reps, time)
and not isinstance(lbs, time)
):
sets.append((name, float(lbs), int(reps)))
global date
workout = {"workout_id": workout_id, "sets": sets, "finished": date}
# Update date
date -= timedelta(days=1)
return workout
exercise_ids = {}
def insert_workout(cur, workout):
cur.execute(
"INSERT INTO workout_records(finished, user_id, workout_id) VALUES (?, ?, ?)",
(
workout["finished"],
USER_ID,
int(workout["workout_id"]),
),
)
workout_record_id = cur.lastrowid
for s in workout["sets"]:
# Get exercise id if not cached
if s[0] not in exercise_ids:
row = cur.execute(
"SELECT * FROM exercises WHERE name = :name", {"name": s[0]}
).fetchone()
# Invalid exercise
if not row:
print("Could not find id for", s[0])
continue
exercise_ids[s[0]] = int(row[0])
exercise_id = exercise_ids[s[0]]
cur.execute(
"INSERT INTO set_records(workout_record_id, lbs, reps, exercise_id) VALUES (?, ?, ?, ?)",
(workout_record_id, s[1], s[2], exercise_id),
)
# Load
print("Importing data...")
workbook = load_workbook(sys.argv[1])
# Get all "week sheets"
print("Parsing sheets...")
week_sheets = []
for sheet in workbook:
if sheet.title.startswith("Week "):
week_sheets.append(sheet)
# Parse all workouts
print("Parsing workouts...")
workouts = []
date = datetime.today()
date = date.replace(hour=15, minute=0, second=0)
for sheet in week_sheets:
print(f"Parsing {sheet.title}...")
workouts.append(parse_workout(LEGDAY_WID, range(2, 22)))
workouts.append(parse_workout(PUSHDAY_WID, range(25, 39)))
workouts.append(parse_workout(PULLDAY_WID, range(42, 56)))
workouts.append(parse_workout(LEGDAY_WID, range(59, 79)))
workouts.append(parse_workout(PUSHDAY_WID, range(82, 96)))
workouts.append(parse_workout(PULLDAY_WID, range(99, 113)))
# Skip sunday
date -= timedelta(days=1)
# Load database
print("Connecting to database...")
conn = sqlite3.connect(DB_FILE)
cur = conn.cursor()
# Insert workouts into the database
for workout in workouts:
insert_workout(cur, workout)
conn.commit()
conn.close()
|