-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
313 lines (244 loc) · 10.7 KB
/
main.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import requests
from flask import Flask, render_template, request, jsonify
from combineImages import create_images
from generateSlideshow import buildSlideshow
from recap import butidRecap
from datetime import datetime
app = Flask(__name__, template_folder="templates")
# Acquire Phone Number from User
def send_code(phone):
print("> Entered phone number is ", phone)
# First Post to send out OTP session and code
url_send_code = "https://berealapi.fly.dev/login/send-code"
# IMPORTANT: Format must be +##########
payload = {"phone": phone}
print("-- Sending OTP Session Request --")
response = requests.post(url_send_code, json=payload)
otp_session = "n/a"
if response.status_code == 201:
print("> Request successful!")
print("Response:", response.json())
response_json = response.json()
if "data" in response_json and "otpSession" in response_json["data"]:
otp_session = response_json["data"]["otpSession"]["sessionInfo"]
print("OTP Session:", otp_session)
else:
print("No 'otpSession' found in the response.")
else:
print("Request failed with status code:", response.status_code)
print(response.json())
return otp_session
# Verify Session using otp_session code and user entered otp_code recieved from phone notification
def verify(otp_session, otp_code):
# print("please enter OTP code")
# otp_code = input()
print("> OTP: ", otp_code)
# Second POST request to verify base don user input
url_verify = "https://berealapi.fly.dev/login/verify"
payload_verify = {"code": otp_code, "otpSession": otp_session}
print("-- Sending Verify Request --")
print(payload_verify)
response_verify = requests.post(url_verify, json=payload_verify)
tokenObj = "n/a"
if response_verify.status_code == 201:
print("> Verification request successful!")
print("Response:", response_verify.json())
# Process the verification response if needed
response_json = response_verify.json()
if "data" in response_json and "token" in response_json["data"]:
tokenObj = response_json["data"]["token"]
print("tokenObj:", tokenObj)
else:
print("No 'tokenObj' found in the response.")
exit()
else:
print(
"> Verification request failed with status code:",
response_verify.status_code,
)
print(response_verify.json())
exit()
return tokenObj
# Fetch user memories. Skip to this stage if we already acquired reusable token
def get_memories(tokenObj, start_date_range, end_date_range):
url_mem_feed = "https://berealapi.fly.dev/friends/mem-feed"
headers = {"token": tokenObj}
# Create a folder named 'primary' if it doesn't exist
folder_name = "primary"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# Create a folder named 'secondary' if it doesn't exist
secondary_folder_name = "secondary"
if not os.path.exists(secondary_folder_name):
os.makedirs(secondary_folder_name)
print("-- Sending Get Memories Request --")
response_mem_feed = requests.get(url_mem_feed, headers=headers)
data_array = []
if response_mem_feed.status_code == 200:
print("> GET request successful!")
# Process the response from mem-feed endpoint
print("Response:", response_mem_feed.json())
print("we did it yay")
response_data = response_mem_feed.json().get("data", {})
data_array = response_data.get("data", [])
else:
print("GET request failed with status code:", response_mem_feed.status_code)
start_date_str = str(start_date_range)
end_date_str = str(end_date_range)
# Convert the input strings to datetime objects
start_date_object = datetime.strptime(start_date_str, "%Y-%m-%d")
end_date_object = datetime.strptime(end_date_str, "%Y-%m-%d")
# Iterate through the 'data' array and download images
for item in data_array:
image_url = item["mainPostPrimaryMedia"].get("url", "")
secondary_image_url = item["mainPostSecondaryMedia"].get("url", "")
date = item["memoryDay"]
date_object = datetime.strptime(date, "%Y-%m-%d")
if image_url and start_date_object <= date_object <= end_date_object:
# Extracting the image name from the URL
image_name = date + "_" + image_url.split("/")[-1]
# Downloading the image
image_path = os.path.join(folder_name, image_name)
with open(image_path, "wb") as img_file:
img_response = requests.get(image_url)
if img_response.status_code == 200:
img_file.write(img_response.content)
print(f"Downloaded {image_name} to {folder_name}")
else:
print(f"Failed to download {image_name}")
if secondary_image_url and start_date_object <= date_object <= end_date_object:
# Extracting the image name from the URL
image_name = date + "_" + secondary_image_url.split("/")[-1]
# Downloading the image
image_path = os.path.join(secondary_folder_name, image_name)
with open(image_path, "wb") as img_file:
img_response = requests.get(secondary_image_url)
if img_response.status_code == 200:
img_file.write(img_response.content)
print(f"Downloaded {image_name} to {secondary_folder_name}")
else:
print(f"Failed to download {image_name}")
return "complete"
# All images referenced in the 'primary' URLs should now be saved in the 'primary' folder
# 'secondary' URLS saved in 'secondary', etc.
# -------------------------------------------------------------------------------------------------------------------------
# Flask App Routing
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
phone_number = request.form["phone_number"]
otp_session = send_code(phone_number)
if otp_session != "n/a":
return render_template("verify.html", otp_session=otp_session)
return render_template(
"index.html",
message="Invalid phone number. Check formatting and Please try again.",
)
return render_template("index.html")
@app.route("/verify", methods=["POST"])
def verify_code():
if request.method == "POST":
user_code = request.form["verification_code"]
otp_session = request.form["otp_session"]
print("> verify_code otp_session: ", otp_session)
tokenObj = verify(otp_session, user_code)
if tokenObj != "n/a":
return render_template("process.html", tokenObj=tokenObj)
else:
return render_template("failure.html")
# return render_template('verify.html', tokenObj='n/a', message='Invalid verification code. Please try again.')
return render_template("verify.html")
@app.route("/process", methods=["POST"])
def process_data():
if request.method == "POST":
start_date_range = request.form["start_date_range"]
end_date_range = request.form["end_date_range"]
wav_file = request.files["wav_file"]
tokenObj = request.form["tokenObj"]
mode = request.form.get("mode")
print("> HTML Form Elements: ")
print("start_date_range ", str(start_date_range))
print("end_date_range ", str(end_date_range))
print("wav_file ", str(wav_file))
print("mode", str(mode))
# Call get_memories function
print("> donwloading music file locally: ")
try:
# Save the uploaded WAV file locally
upload_directory = os.getcwd()
print("saving file to ", upload_directory)
if not os.path.exists(upload_directory):
os.makedirs(upload_directory)
wav_file.save(os.path.join(upload_directory, "curr_song.wav"))
except Exception as e:
print(f"Error in processing data: {str(e)}")
result = " "
if not os.path.exists("primary") or not os.path.exists("secondary"):
print("> downloading images locally")
result = get_memories(tokenObj, start_date_range, end_date_range)
if result != "n/a":
# Execute the Python functions
create_images() # process images and apply effects
# do something with current page here
buildSlideshow(mode) # assemble files and load audio
# do something with current page here
return render_template("preview.html")
else:
return render_template("failure.html")
return render_template("process.html")
@app.route("/recap", methods=["POST"])
def recap():
if request.method == "POST":
start_date_range = "2023-01-01"
end_date_range = "2023-12-31"
tokenObj = request.form["tokenObj"]
# check if a folder called 'primary' exists and 'secondary' exists
result = " "
if not os.path.exists("primary") or not os.path.exists("secondary"):
print("> downloading images locally")
result = get_memories(tokenObj, start_date_range, end_date_range)
if result != "n/a":
# Execute the Python functions
create_images() # process images and apply effects
# do something with current page here
butidRecap() # assemble files and load audio
# do something with current page here
return render_template("preview.html")
else:
return render_template("failure.html")
return render_template("process.html")
# @app.route('/run-python-functions', methods=['POST'])
# def run_python_functions():
# try:
# # Execute the Python functions
# create_images() # process images and apply effects
# buildSlideshow() # assemble files and load audio
#
# return render_template('preview.html') # Success! redirect to preview page
#
# except Exception as e:
# # Return a JSON response indicating failure
# return render_template('failure.html', message=str(e)) # Failure! redirect to failure page
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/privacy")
def privacy():
return render_template("privacy.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/preview")
def preview():
return render_template("preview.html")
@app.route("/failure")
def failure():
return render_template("failure.html")
if __name__ == "__main__":
app.run(debug=True)
# otp_session = send_code()
# tokenObj = verify(otp_session)
# get_memories(tokenObj)
# create_images()
# buildSlideshow()