Spaces:
Paused
Paused
Enhance error handling in bypass_shinigami function and update API response structure
Browse files- app/main.py +6 -2
- app/utils.py +26 -6
app/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, Query
|
| 2 |
from app.utils import bypass_shinigami
|
| 3 |
|
| 4 |
app = FastAPI()
|
|
@@ -9,5 +9,9 @@ async def root():
|
|
| 9 |
|
| 10 |
@app.get("/bypass")
|
| 11 |
async def bypass(url: str = Query(..., description="URL Shinigami yang mau dibypass")):
|
|
|
|
|
|
|
| 12 |
result = bypass_shinigami(url)
|
| 13 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query, HTTPException
|
| 2 |
from app.utils import bypass_shinigami
|
| 3 |
|
| 4 |
app = FastAPI()
|
|
|
|
| 9 |
|
| 10 |
@app.get("/bypass")
|
| 11 |
async def bypass(url: str = Query(..., description="URL Shinigami yang mau dibypass")):
|
| 12 |
+
if not url.startswith("http"):
|
| 13 |
+
raise HTTPException(status_code=400, detail="URL tidak valid")
|
| 14 |
result = bypass_shinigami(url)
|
| 15 |
+
if result["status"] == "error":
|
| 16 |
+
raise HTTPException(status_code=502, detail=result["message"])
|
| 17 |
+
return result
|
app/utils.py
CHANGED
|
@@ -3,12 +3,23 @@ from selenium import webdriver
|
|
| 3 |
from selenium.webdriver.chrome.options import Options
|
| 4 |
from selenium.webdriver.common.by import By
|
| 5 |
import time
|
|
|
|
| 6 |
|
| 7 |
# Force all cache to go to /tmp/.cache
|
| 8 |
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
|
| 9 |
os.environ["XDG_CONFIG_HOME"] = "/tmp/.config"
|
| 10 |
|
| 11 |
-
def bypass_shinigami(url: str) ->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
chrome_options = Options()
|
| 13 |
chrome_options.add_argument("--headless")
|
| 14 |
chrome_options.add_argument("--disable-gpu")
|
|
@@ -23,12 +34,21 @@ def bypass_shinigami(url: str) -> str:
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
driver.get(url)
|
| 26 |
-
time.sleep(5)
|
| 27 |
-
|
| 28 |
-
# Ambil link pertama dari tombol (misal tombol lanjut/link keluar)
|
| 29 |
link = driver.find_element(By.TAG_NAME, "a").get_attribute("href")
|
| 30 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
finally:
|
| 34 |
driver.quit()
|
|
|
|
| 3 |
from selenium.webdriver.chrome.options import Options
|
| 4 |
from selenium.webdriver.common.by import By
|
| 5 |
import time
|
| 6 |
+
import requests
|
| 7 |
|
| 8 |
# Force all cache to go to /tmp/.cache
|
| 9 |
os.environ["XDG_CACHE_HOME"] = "/tmp/.cache"
|
| 10 |
os.environ["XDG_CONFIG_HOME"] = "/tmp/.config"
|
| 11 |
|
| 12 |
+
def bypass_shinigami(url: str) -> dict:
|
| 13 |
+
# Cek status code dulu
|
| 14 |
+
try:
|
| 15 |
+
resp = requests.get(url, timeout=10)
|
| 16 |
+
status_code = resp.status_code
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return {"status": "error", "message": f"Gagal akses url: {e}", "code": None, "bypassed_url": None}
|
| 19 |
+
|
| 20 |
+
if status_code != 200:
|
| 21 |
+
return {"status": "error", "message": f"Status code: {status_code}", "code": status_code, "bypassed_url": None}
|
| 22 |
+
|
| 23 |
chrome_options = Options()
|
| 24 |
chrome_options.add_argument("--headless")
|
| 25 |
chrome_options.add_argument("--disable-gpu")
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
driver.get(url)
|
| 37 |
+
time.sleep(5)
|
| 38 |
+
# Ambil link pertama yang ditemukan
|
|
|
|
| 39 |
link = driver.find_element(By.TAG_NAME, "a").get_attribute("href")
|
| 40 |
+
return {
|
| 41 |
+
"status": "success",
|
| 42 |
+
"message": "OK",
|
| 43 |
+
"code": status_code,
|
| 44 |
+
"bypassed_url": link or None
|
| 45 |
+
}
|
| 46 |
except Exception as e:
|
| 47 |
+
return {
|
| 48 |
+
"status": "error",
|
| 49 |
+
"message": f"Error: {e}",
|
| 50 |
+
"code": status_code,
|
| 51 |
+
"bypassed_url": None
|
| 52 |
+
}
|
| 53 |
finally:
|
| 54 |
driver.quit()
|