-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (123 loc) · 4.07 KB
/
Copy pathapp.py
File metadata and controls
154 lines (123 loc) · 4.07 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
from flask import Flask, render_template, request, url_for, jsonify
import json
import string, random
import time
import os
app = Flask(__name__)
DATA_FILE = "data.json"
FIVE_DAYS = 5 * 24 * 60 * 60 # 5 days
def clean_old_data(data):
current = time.time()
return {
k: v for k, v in data.items()
if current - v.get("created_at", current) < FIVE_DAYS
}
# Ensure data.json exists and is valid
if not os.path.exists(DATA_FILE):
with open(DATA_FILE, "w") as f:
json.dump({}, f)
else:
try:
with open(DATA_FILE, "r") as f:
json.load(f)
except json.JSONDecodeError:
with open(DATA_FILE, "w") as f:
json.dump({}, f)
def generate_unique_id(length=6):
# """Generate random unique ID"""
chars = string.ascii_letters + string.digits
return ''.join(random.choice(chars) for _ in range(length))
@app.route("/")
def home():
return render_template("form.html")
@app.route("/submit", methods=["POST"])
def submit():
your_name = request.form.get("your_name")
crush_name = request.form.get("crush_name")
your_paragraph = request.form.get("your_paragraph")
unique_id = generate_unique_id()
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except:
data = {}
data = clean_old_data(data)
data[unique_id] = {
"your_name": your_name,
"crush_name": crush_name,
"your_paragraph": your_paragraph,
"response": None,
"created_at": time.time()
}
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
love_link = url_for("love_page", unique_id=unique_id, _external=True)
status_link = url_for("check_response", unique_id=unique_id, _external=True)
return jsonify({
"love_link": love_link,
"status_link": status_link
})
@app.route("/love/<unique_id>")
def love_page(unique_id):
# Load existing data safely
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except json.JSONDecodeError:
data = {}
if unique_id not in data:
return "Invalid link!"
entry = data[unique_id]
your_name = entry["your_name"]
crush_name = entry["crush_name"]
your_paragraph = entry["your_paragraph"]
return render_template("love.html", message=your_paragraph, your_name=your_name, crush_name=crush_name, unique_id=unique_id)
# Route to store Yes/No response from crush
@app.route("/response/<unique_id>/<answer>")
def save_response(unique_id, answer):
# Load existing data
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except json.JSONDecodeError:
data = {}
if unique_id in data:
data[unique_id]["response"] = answer # store Yes or No
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
return jsonify({"status": "success"})
else:
return jsonify({"status": "error", "message": "Invalid link"})
# Route to check response (sender can use)
@app.route("/status/<unique_id>")
def check_response(unique_id):
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except json.JSONDecodeError:
data = {}
data = clean_old_data(data)
# 🔥 YAHI PE (save back)
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
if unique_id not in data:
return render_template(
"status.html",
status="invalid",
message="Invalid link ❌"
)
response = data[unique_id].get("response")
if response:
return render_template(
"status.html",
status="answered",
message=f"They selected {response.upper()}!"
)
else:
return render_template(
"status.html",
status="waiting",
message="Your crush has not responded yet ⏳"
)
if __name__ == "__main__":
app.run()