Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from playwright.async_api import async_playwright
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="SaveFrom Extractor API")
|
| 7 |
+
|
| 8 |
+
class DownloadRequest(BaseModel):
|
| 9 |
+
url: str
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.get("/")
|
| 13 |
+
def root():
|
| 14 |
+
return {"status": "ok"}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.post("/download")
|
| 18 |
+
async def extract(req: DownloadRequest):
|
| 19 |
+
target = f"https://en.savefrom.net/1/{req.url}"
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
async with async_playwright() as p:
|
| 23 |
+
browser = await p.chromium.launch(
|
| 24 |
+
headless=True,
|
| 25 |
+
args=[
|
| 26 |
+
"--no-sandbox",
|
| 27 |
+
"--disable-dev-shm-usage",
|
| 28 |
+
"--disable-blink-features=AutomationControlled",
|
| 29 |
+
],
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
context = await browser.new_context(
|
| 33 |
+
user_agent=(
|
| 34 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 35 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 36 |
+
"Chrome/120.0.0.0 Safari/537.36"
|
| 37 |
+
),
|
| 38 |
+
viewport={"width": 1280, "height": 720},
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
page = await context.new_page()
|
| 42 |
+
await page.goto(target, wait_until="networkidle", timeout=60_000)
|
| 43 |
+
|
| 44 |
+
# Give SaveFrom time to populate links
|
| 45 |
+
await page.wait_for_timeout(4000)
|
| 46 |
+
|
| 47 |
+
links = await page.evaluate("""
|
| 48 |
+
() => {
|
| 49 |
+
const results = [];
|
| 50 |
+
document.querySelectorAll("a").forEach(a => {
|
| 51 |
+
const href = a.href || "";
|
| 52 |
+
if (href.includes(".mp4") || href.includes(".mp3")) {
|
| 53 |
+
results.push({
|
| 54 |
+
url: href,
|
| 55 |
+
text: a.innerText || null
|
| 56 |
+
});
|
| 57 |
+
}
|
| 58 |
+
});
|
| 59 |
+
return results;
|
| 60 |
+
}
|
| 61 |
+
""")
|
| 62 |
+
|
| 63 |
+
await browser.close()
|
| 64 |
+
|
| 65 |
+
if not links:
|
| 66 |
+
raise HTTPException(
|
| 67 |
+
status_code=404,
|
| 68 |
+
detail="No downloadable links found"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return {
|
| 72 |
+
"source": "neon dl",
|
| 73 |
+
"original_url": req.url,
|
| 74 |
+
"count": len(links),
|
| 75 |
+
"links": links
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
raise HTTPException(
|
| 80 |
+
status_code=500,
|
| 81 |
+
detail=f"Extractor error: {str(e)}"
|
| 82 |
+
)
|