Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from pydantic import BaseModel
|
4 |
-
from typing import List
|
5 |
import datetime
|
6 |
import json
|
|
|
7 |
|
8 |
-
app = FastAPI(
|
9 |
|
10 |
# Enable CORS
|
11 |
app.add_middleware(
|
@@ -15,33 +16,80 @@ app.add_middleware(
|
|
15 |
allow_headers=["*"],
|
16 |
)
|
17 |
|
18 |
-
class
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
type: str
|
23 |
|
24 |
-
class
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
#
|
31 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
32 |
-
filename = f"
|
33 |
|
|
|
34 |
with open(filename, "w") as f:
|
35 |
-
json.dump(
|
|
|
|
|
|
|
|
|
36 |
|
37 |
return {
|
38 |
"status": "success",
|
39 |
-
"
|
40 |
"filename": filename
|
41 |
}
|
42 |
except Exception as e:
|
43 |
raise HTTPException(status_code=500, detail=str(e))
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
@app.get("/")
|
46 |
-
async def
|
47 |
-
return {"
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Request, Query
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from pydantic import BaseModel
|
4 |
+
from typing import List, Optional
|
5 |
import datetime
|
6 |
import json
|
7 |
+
import os
|
8 |
|
9 |
+
app = FastAPI()
|
10 |
|
11 |
# Enable CORS
|
12 |
app.add_middleware(
|
|
|
16 |
allow_headers=["*"],
|
17 |
)
|
18 |
|
19 |
+
class SMSMessage(BaseModel):
|
20 |
+
sender: str
|
21 |
+
text: str
|
22 |
+
timestamp: int
|
|
|
23 |
|
24 |
+
class BackupRequest(BaseModel):
|
25 |
+
phone: str
|
26 |
+
messages: List[SMSMessage]
|
27 |
|
28 |
+
# Storage directory
|
29 |
+
DATA_DIR = "sms_data"
|
30 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
31 |
+
|
32 |
+
@app.post("/backup")
|
33 |
+
async def backup_sms(request: BackupRequest):
|
34 |
try:
|
35 |
+
# Create filename with phone number and timestamp
|
36 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
37 |
+
filename = f"{DATA_DIR}/{request.phone}_{timestamp}.json"
|
38 |
|
39 |
+
# Save messages to file
|
40 |
with open(filename, "w") as f:
|
41 |
+
json.dump({
|
42 |
+
"phone": request.phone,
|
43 |
+
"timestamp": timestamp,
|
44 |
+
"messages": [msg.dict() for msg in request.messages]
|
45 |
+
}, f)
|
46 |
|
47 |
return {
|
48 |
"status": "success",
|
49 |
+
"saved_count": len(request.messages),
|
50 |
"filename": filename
|
51 |
}
|
52 |
except Exception as e:
|
53 |
raise HTTPException(status_code=500, detail=str(e))
|
54 |
|
55 |
+
@app.get("/retrieve")
|
56 |
+
async def retrieve_sms(
|
57 |
+
phone: str = Query(..., description="Phone number to retrieve messages for"),
|
58 |
+
limit: Optional[int] = Query(50, description="Maximum number of messages to retrieve")
|
59 |
+
):
|
60 |
+
try:
|
61 |
+
# Find all files for this phone number
|
62 |
+
matching_files = []
|
63 |
+
for filename in os.listdir(DATA_DIR):
|
64 |
+
if filename.startswith(f"{phone}_"):
|
65 |
+
matching_files.append(filename)
|
66 |
+
|
67 |
+
if not matching_files:
|
68 |
+
raise HTTPException(status_code=404, detail="No messages found for this phone number")
|
69 |
+
|
70 |
+
# Sort by newest first
|
71 |
+
matching_files.sort(reverse=True)
|
72 |
+
|
73 |
+
# Read all messages
|
74 |
+
all_messages = []
|
75 |
+
for filename in matching_files:
|
76 |
+
with open(f"{DATA_DIR}/{filename}", "r") as f:
|
77 |
+
data = json.load(f)
|
78 |
+
all_messages.extend(data["messages"])
|
79 |
+
|
80 |
+
# Apply limit and sort by timestamp (newest first)
|
81 |
+
all_messages.sort(key=lambda x: x["timestamp"], reverse=True)
|
82 |
+
limited_messages = all_messages[:limit] if limit else all_messages
|
83 |
+
|
84 |
+
return {
|
85 |
+
"status": "success",
|
86 |
+
"phone": phone,
|
87 |
+
"count": len(limited_messages),
|
88 |
+
"messages": limited_messages
|
89 |
+
}
|
90 |
+
except Exception as e:
|
91 |
+
raise HTTPException(status_code=500, detail=str(e))
|
92 |
+
|
93 |
@app.get("/")
|
94 |
+
async def health_check():
|
95 |
+
return {"status": "healthy", "timestamp": datetime.datetime.now().isoformat()}
|