Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,100 @@ import httpx
|
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
-
app = FastAPI()
|
| 7 |
|
| 8 |
LOGIN_URL = "https://www.ivasms.com/login"
|
| 9 |
DASHBOARD_URL = "https://www.ivasms.com/portal"
|
| 10 |
SMS_URL = "https://www.ivasms.com/portal/sms/received/getsms"
|
| 11 |
|
|
|
|
| 12 |
client = httpx.Client(
|
| 13 |
follow_redirects=True,
|
| 14 |
headers={
|
| 15 |
-
"User-Agent": "Mozilla/5.0",
|
|
|
|
| 16 |
"Accept-Language": "en-US,en;q=0.9",
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
session_ready = False
|
| 21 |
csrf_token = None
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def login(email: str, password: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
global session_ready, csrf_token
|
| 26 |
|
|
|
|
| 27 |
r = client.get(LOGIN_URL)
|
| 28 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
|
|
|
| 31 |
data = {
|
| 32 |
"email": email,
|
| 33 |
"username": email,
|
| 34 |
"password": password,
|
| 35 |
-
"_token":
|
| 36 |
}
|
| 37 |
|
| 38 |
res = client.post(LOGIN_URL, data=data)
|
| 39 |
|
| 40 |
if "login" in str(res.url):
|
|
|
|
| 41 |
raise HTTPException(401, "Login failed")
|
| 42 |
|
|
|
|
| 43 |
dash = client.get(DASHBOARD_URL)
|
| 44 |
-
|
| 45 |
-
meta =
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
csrf_token = meta["content"]
|
| 48 |
session_ready = True
|
| 49 |
|
| 50 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
@app.get("/status")
|
| 53 |
def status():
|
| 54 |
-
return {
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
@app.get("/sms")
|
| 57 |
def get_sms():
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
raise HTTPException(401, "Not logged in")
|
| 60 |
|
| 61 |
today = datetime.utcnow()
|
|
@@ -68,11 +109,19 @@ def get_sms():
|
|
| 68 |
}
|
| 69 |
|
| 70 |
r = client.post(SMS_URL, data=payload)
|
| 71 |
-
return {"raw_html": r.text}
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def logout():
|
| 75 |
-
global session_ready
|
| 76 |
client.cookies.clear()
|
| 77 |
session_ready = False
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
+
app = FastAPI(title="iVasMS API (GET only)")
|
| 7 |
|
| 8 |
LOGIN_URL = "https://www.ivasms.com/login"
|
| 9 |
DASHBOARD_URL = "https://www.ivasms.com/portal"
|
| 10 |
SMS_URL = "https://www.ivasms.com/portal/sms/received/getsms"
|
| 11 |
|
| 12 |
+
# Global session (simple & legal)
|
| 13 |
client = httpx.Client(
|
| 14 |
follow_redirects=True,
|
| 15 |
headers={
|
| 16 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
|
| 17 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
| 18 |
"Accept-Language": "en-US,en;q=0.9",
|
| 19 |
+
"Connection": "keep-alive",
|
| 20 |
+
},
|
| 21 |
+
timeout=30.0
|
| 22 |
)
|
| 23 |
|
| 24 |
session_ready = False
|
| 25 |
csrf_token = None
|
| 26 |
|
| 27 |
+
|
| 28 |
+
@app.get("/")
|
| 29 |
+
def root():
|
| 30 |
+
return {
|
| 31 |
+
"service": "iVasMS API",
|
| 32 |
+
"login": "/login?email=EMAIL&password=PASSWORD",
|
| 33 |
+
"status": "/status",
|
| 34 |
+
"sms": "/sms",
|
| 35 |
+
"logout": "/logout"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.get("/login")
|
| 40 |
def login(email: str, password: str):
|
| 41 |
+
"""
|
| 42 |
+
LOGIN via GET
|
| 43 |
+
Example:
|
| 44 |
+
/login?email=xxx@gmail.com&password=xxxxx
|
| 45 |
+
"""
|
| 46 |
global session_ready, csrf_token
|
| 47 |
|
| 48 |
+
# Step 1: open login page (get CSRF)
|
| 49 |
r = client.get(LOGIN_URL)
|
| 50 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 51 |
+
token_input = soup.find("input", {"name": "_token"})
|
| 52 |
+
if not token_input:
|
| 53 |
+
raise HTTPException(500, "CSRF token not found")
|
| 54 |
|
| 55 |
+
# Step 2: submit login (email + username for safety)
|
| 56 |
data = {
|
| 57 |
"email": email,
|
| 58 |
"username": email,
|
| 59 |
"password": password,
|
| 60 |
+
"_token": token_input["value"]
|
| 61 |
}
|
| 62 |
|
| 63 |
res = client.post(LOGIN_URL, data=data)
|
| 64 |
|
| 65 |
if "login" in str(res.url):
|
| 66 |
+
session_ready = False
|
| 67 |
raise HTTPException(401, "Login failed")
|
| 68 |
|
| 69 |
+
# Step 3: get dashboard CSRF
|
| 70 |
dash = client.get(DASHBOARD_URL)
|
| 71 |
+
dash_soup = BeautifulSoup(dash.text, "html.parser")
|
| 72 |
+
meta = dash_soup.find("meta", {"name": "csrf-token"})
|
| 73 |
+
if not meta:
|
| 74 |
+
session_ready = False
|
| 75 |
+
raise HTTPException(500, "Dashboard CSRF not found")
|
| 76 |
|
| 77 |
csrf_token = meta["content"]
|
| 78 |
session_ready = True
|
| 79 |
|
| 80 |
+
return {
|
| 81 |
+
"status": "ok",
|
| 82 |
+
"logged_in": True
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
|
| 86 |
@app.get("/status")
|
| 87 |
def status():
|
| 88 |
+
return {
|
| 89 |
+
"logged_in": session_ready
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
|
| 93 |
@app.get("/sms")
|
| 94 |
def get_sms():
|
| 95 |
+
"""
|
| 96 |
+
Ambil SMS (raw HTML)
|
| 97 |
+
Harus login dulu
|
| 98 |
+
"""
|
| 99 |
+
if not session_ready or not csrf_token:
|
| 100 |
raise HTTPException(401, "Not logged in")
|
| 101 |
|
| 102 |
today = datetime.utcnow()
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
r = client.post(SMS_URL, data=payload)
|
|
|
|
| 112 |
|
| 113 |
+
return {
|
| 114 |
+
"success": True,
|
| 115 |
+
"raw_html": r.text
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
@app.get("/logout")
|
| 120 |
def logout():
|
| 121 |
+
global session_ready, csrf_token
|
| 122 |
client.cookies.clear()
|
| 123 |
session_ready = False
|
| 124 |
+
csrf_token = None
|
| 125 |
+
return {
|
| 126 |
+
"status": "logged_out"
|
| 127 |
+
}
|