-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
279 lines (234 loc) · 9.71 KB
/
Copy pathuser.py
File metadata and controls
279 lines (234 loc) · 9.71 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import os
import re
import threading
from datetime import datetime
import pandas as pd
import psycopg2
class UserManager:
_instance = None
user_id = None
combined_data = {}
user_profile_file = "user_profile.json"
user_preferences = {}
exercise_data_file = 'exercise_data.json'
dataset_file = 'workout_plans2.csv'
df = pd.read_csv(dataset_file)
exercise_data = {}
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = UserManager()
return cls._instance
def set_user_id(self, user_id):
self.user_id = user_id
def get_user_id(self):
return self.user_id
def set_user_profile(self, profile_data):
self.combined_data.update(profile_data)
def get_user_profile(self):
return self.combined_data
def process_height(self):
profile_data = self.get_user_profile()
height = profile_data['Height (cm)']
start_index = height.find('(')
end_index = height.find('c', start_index)
number_str = height[start_index + 1:end_index]
height_value = int(number_str)
profile_data['Height (cm)'] = height_value
def extract_height_cm(self, height_str):
match = re.search(r"(\d+)cm", height_str)
return int(match.group(1)) if match else None
def add_user_groups(self):
profile_data = self.get_user_profile()
age = profile_data['Age']
weight = profile_data['Weight (lbs)']
if age >= 18 and age <= 30:
profile_data['Age Group'] = 1
elif age >= 31 and age <= 40:
profile_data['Age Group'] = 2
elif age >= 41 and age <= 50:
profile_data['Age Group'] = 3
else:
profile_data['Age Group'] = 4
if weight >= 80 and weight <= 101:
profile_data['Weight Group'] = 1
elif weight >= 100 and weight <= 121:
profile_data['Weight Group'] = 2
elif weight >= 120 and weight <= 141:
profile_data['Weight Group'] = 3
elif weight >= 140 and weight <= 161:
profile_data['Weight Group'] = 4
elif weight >= 160 and weight <= 181:
profile_data['Weight Group'] = 5
elif weight >= 180 and weight <= 201:
profile_data['Weight Group'] = 6
elif weight >= 201 and weight <= 220:
profile_data['Weight Group'] = 7
else:
profile_data['Weight Group'] = 8
def update_user_profile(self):
pass
def set_user_goals(self, goal, weight, days, split):
self.combined_data["Goal"] = goal
self.combined_data["Weight Loss (lbs)"] = weight
self.combined_data["Frequency days/week"] = days
self.combined_data["Workout Type"] = split
def add_weight_loss_scale(self):
profile_data = self.get_user_profile()
weight_str = profile_data.get('Weight Loss (lbs)', '').strip()
if weight_str.isdigit():
weight = int(weight_str)
if weight == 0:
self.combined_data['Weight Loss Scale'] = 0
elif weight <= 15:
self.combined_data['Weight Loss Scale'] = 1
elif weight <= 25:
self.combined_data['Weight Loss Scale'] = 2
elif weight <= 35:
self.combined_data['Weight Loss Scale'] = 3
elif weight <= 45:
self.combined_data['Weight Loss Scale'] = 4
else:
self.combined_data['Weight Loss Scale'] = 5
else:
print("Invalid or missing weight input for weight loss scale calculation.")
def process_frequency(self):
profile_data = self.get_user_profile()
frequency = profile_data['Frequency days/week']
parts = frequency.split()
for part in parts:
if part.isdigit():
frequency_value = int(part)
profile_data['Frequency days/week'] = frequency_value
return profile_data
def set_user_preferences(self, preferences):
self.user_preferences = preferences
def process_user_preferences(self):
preference_data = self.get_user_preferences()
frequency = preference_data['Frequency days/week']
parts = frequency.split()
for part in parts:
if part.isdigit():
frequency_value = int(part)
preference_data['Frequency days/week'] = frequency_value
return preference_data
# return int(part)
def get_user_preferences(self):
return self.user_preferences
def get_user_goals(self):
return self.user_goals
def set_user_start_date(self, start_date):
with open("user_data.json", "w") as f:
json.dump({"user_start_date": start_date.strftime("%Y-%m-%d")}, f)
def get_user_start_date(self):
try:
with open("user_data.json", "r") as f:
data = json.load(f)
return datetime.strptime(data["user_start_date"], "%Y-%m-%d")
except (FileNotFoundError, json.JSONDecodeError, KeyError):
return None
# SAVE USER WORKOUT AFTER SETTING GOALS
def set_user_rec_workout(self, workout):
try:
with open("user_data.json", "r+") as f:
data = json.load(f)
data["user_workout"] = workout
f.seek(0)
json.dump(data, f)
f.truncate()
except FileNotFoundError:
print("User data file not found.")
def get_user_rec_workout(self):
try:
with open("user_data.json", "r") as f:
data = json.load(f)
return data.get("user_workout")
except (FileNotFoundError, json.JSONDecodeError, KeyError):
return None
def save_user_profile(self):
with open(self.user_profile_file, "w") as f:
json.dump(self.combined_data, f)
def load_user_profile(self):
file = self.user_profile_file
try:
with open(file, "r") as f:
self.combined_data = json.load(f)
except FileNotFoundError:
self.combined_data = {}
def extract_exercises(self):
workout_plan_name = self.get_user_rec_workout()
filtered_df = self.df[self.df['Name'] == workout_plan_name]
exercise_data = {}
if not filtered_df.empty:
columns = filtered_df.columns[filtered_df.columns != 'Name']
for col_name in columns:
exercises_list = []
if col_name.startswith('Day'):
for exercise_info in filtered_df[col_name]:
if exercise_info.strip() == "Rest":
exercises_list.append({"Rest": {"Sets": None, "Reps": None}})
else:
exercises = re.findall(r'\[(.*?)\]', exercise_info)
# print(exercises)
for exercise in exercises:
match = re.match(r'(.*?), Sets: (\d+), Reps: (\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*)',
exercise)
# match = re.match(r'(.*?), Sets: (\d+), Reps: (\d+-\d+)', exercise)
if match:
exercise_name = match.group(1).strip()
sets = match.group(2)
reps = match.group(3)
exercises_list.append({exercise_name: {"Sets": sets, "Reps": reps}})
exercise_data[col_name] = exercises_list
with open('exercise_data.json', 'w') as json_file:
json.dump(exercise_data, json_file, indent=4)
print(exercise_data)
def get_exercise_data(self):
# self.file_created_event.wait()
try:
with open("exercise_data.json", "r") as f:
exercise_data = json.load(f)
return exercise_data
# except (FileNotFoundError, json.JSONDecodeError, KeyError):
# return None
except FileNotFoundError:
print("FileNotFoundError: 'exercise_data.json' not found")
return None
except json.JSONDecodeError:
print("JSONDecodeError: Failed to decode 'exercise_data.json'")
return None
except KeyError:
print("KeyError: Unexpected structure in 'exercise_data.json'")
return None
# def temp_exercise_data(self):
# exercise_data = self.get_exercise_data()
# self.exercise_data = exercise_data
# return self.exercise_data
def save_profile_to_db(self, user_id, age, gender, height, weight, experience):
# Define connection parameters
rds_host = 'refer to email'
rds_port = 'refer to email'
rds_dbname = 'refer to email'
rds_user = 'refer to email'
rds_password = 'refer to email'
conn_string = f"host={rds_host} port={rds_port} dbname={rds_dbname} user={rds_user} password={rds_password}"
height_cm = self.extract_height_cm(height)
conn = None
try:
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO profile (userid, age, gender, height, currentweight, explvl)
VALUES (%s, %s, %s, %s, %s, %s)
""",
(user_id, age, gender, height_cm, weight, experience)
)
conn.commit()
cursor.close()
except Exception as e:
print("Error saving profile to database:", e)
finally:
if conn is not None:
conn.close()