Athspi commited on
Commit
e775e54
·
verified ·
1 Parent(s): 51b503d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(title="SMS Backup API")
9
+
10
+ # Enable CORS
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ class SmsItem(BaseModel):
19
+ address: str
20
+ body: str
21
+ date: int
22
+ type: str
23
+
24
+ class SmsRequest(BaseModel):
25
+ sms_list: List[SmsItem]
26
+
27
+ @app.post("/save-sms")
28
+ async def save_sms(request: SmsRequest):
29
+ try:
30
+ # Save to JSON file (in production, use a database)
31
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
32
+ filename = f"sms_backup_{timestamp}.json"
33
+
34
+ with open(filename, "w") as f:
35
+ json.dump([sms.dict() for sms in request.sms_list], f)
36
+
37
+ return {
38
+ "status": "success",
39
+ "message": f"{len(request.sms_list)} SMS saved",
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 read_root():
47
+ return {"message": "SMS Backup API is running"}