Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import subprocess
|
5 |
+
import os
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
app = FastAPI(
|
9 |
+
title="GAMDL API",
|
10 |
+
description="API for downloading Google Drive files using gamdl",
|
11 |
+
version="1.0.0"
|
12 |
+
)
|
13 |
+
|
14 |
+
class DownloadRequest(BaseModel):
|
15 |
+
url: str
|
16 |
+
|
17 |
+
class DownloadResponse(BaseModel):
|
18 |
+
success: bool
|
19 |
+
message: str
|
20 |
+
filename: str = None
|
21 |
+
|
22 |
+
@app.post("/download", response_model=DownloadResponse)
|
23 |
+
async def download_file(request: DownloadRequest):
|
24 |
+
try:
|
25 |
+
# Create a temporary directory for downloads
|
26 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
27 |
+
# Change to temp directory
|
28 |
+
original_dir = os.getcwd()
|
29 |
+
os.chdir(temp_dir)
|
30 |
+
|
31 |
+
# Run gamdl command
|
32 |
+
process = subprocess.run(
|
33 |
+
["gamdl", request.url],
|
34 |
+
capture_output=True,
|
35 |
+
text=True,
|
36 |
+
check=True
|
37 |
+
)
|
38 |
+
|
39 |
+
# Get the downloaded filename (last file in temp directory)
|
40 |
+
files = os.listdir()
|
41 |
+
if not files:
|
42 |
+
raise Exception("No file was downloaded")
|
43 |
+
|
44 |
+
downloaded_file = files[0]
|
45 |
+
|
46 |
+
# Move back to original directory
|
47 |
+
os.chdir(original_dir)
|
48 |
+
|
49 |
+
return DownloadResponse(
|
50 |
+
success=True,
|
51 |
+
message="File downloaded successfully",
|
52 |
+
filename=downloaded_file
|
53 |
+
)
|
54 |
+
|
55 |
+
except subprocess.CalledProcessError as e:
|
56 |
+
raise HTTPException(
|
57 |
+
status_code=400,
|
58 |
+
detail=f"Failed to download: {e.stderr}"
|
59 |
+
)
|
60 |
+
except Exception as e:
|
61 |
+
raise HTTPException(
|
62 |
+
status_code=500,
|
63 |
+
detail=f"Error: {str(e)}"
|
64 |
+
)
|
65 |
+
|
66 |
+
# Add basic homepage with API documentation link
|
67 |
+
@app.get("/")
|
68 |
+
async def root():
|
69 |
+
return {"message": " Visit /docs for API documentation."}
|