Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -189,4 +189,40 @@ def download_json():
|
|
| 189 |
def debug():
|
| 190 |
return jsonify({
|
| 191 |
"db_exists": os.path.exists(DATABASE),
|
| 192 |
-
"csv_exists": os.path.exists(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
def debug():
|
| 190 |
return jsonify({
|
| 191 |
"db_exists": os.path.exists(DATABASE),
|
| 192 |
+
"csv_exists": os.path.exists(EXPORT_CSV),
|
| 193 |
+
"json_exists": os.path.exists(EXPORT_JSON),
|
| 194 |
+
})
|
| 195 |
+
|
| 196 |
+
# Helpers
|
| 197 |
+
def generate_otp():
|
| 198 |
+
return str(random.randint(100000, 999999))
|
| 199 |
+
|
| 200 |
+
def export_logs():
|
| 201 |
+
with sqlite3.connect(DATABASE) as conn:
|
| 202 |
+
c = conn.cursor()
|
| 203 |
+
c.execute("SELECT otp, generated_at, expires_at, used_at, ip_address, status FROM otps ORDER BY id DESC")
|
| 204 |
+
rows = c.fetchall()
|
| 205 |
+
|
| 206 |
+
# Save CSV
|
| 207 |
+
with open(EXPORT_CSV, "w", newline="") as f:
|
| 208 |
+
writer = csv.writer(f)
|
| 209 |
+
writer.writerow(["otp", "generated_at", "expires_at", "used_at", "ip_address", "status"])
|
| 210 |
+
writer.writerows(rows)
|
| 211 |
+
|
| 212 |
+
# Save JSON
|
| 213 |
+
json_data = [
|
| 214 |
+
{
|
| 215 |
+
"otp": row[0],
|
| 216 |
+
"generated_at": row[1],
|
| 217 |
+
"expires_at": row[2],
|
| 218 |
+
"used_at": row[3],
|
| 219 |
+
"ip_address": row[4],
|
| 220 |
+
"status": row[5]
|
| 221 |
+
}
|
| 222 |
+
for row in rows
|
| 223 |
+
]
|
| 224 |
+
with open(EXPORT_JSON, "w") as f:
|
| 225 |
+
json.dump(json_data, f, indent=2)
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
app.run(host="0.0.0.0", port=7860)
|