Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from datetime import datetime
|
|
| 10 |
import tempfile
|
| 11 |
from pathlib import Path
|
| 12 |
from dotenv import load_dotenv
|
|
|
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -71,18 +72,28 @@ os.makedirs(DOWNLOADS_DIR, exist_ok=True)
|
|
| 71 |
# Mount the downloads directory
|
| 72 |
app.mount("/files", StaticFiles(directory=DOWNLOADS_DIR), name="files")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
class DownloadRequest(BaseModel):
|
| 75 |
url: str
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
class DownloadResponse(BaseModel):
|
| 78 |
success: bool
|
| 79 |
message: str
|
| 80 |
-
|
| 81 |
-
download_url: str = None
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# [Previous cookie handling code remains the same...]
|
| 86 |
|
| 87 |
@app.post("/download", response_model=DownloadResponse)
|
| 88 |
async def download_file(request: DownloadRequest):
|
|
@@ -124,40 +135,49 @@ async def download_file(request: DownloadRequest):
|
|
| 124 |
for root, dirs, files in os.walk('.'):
|
| 125 |
for file in files:
|
| 126 |
if file != "cookies.txt":
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
logger.info(f"All files found: {all_files}")
|
| 130 |
|
| 131 |
if not all_files:
|
| 132 |
raise Exception("No files found after download attempt")
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
downloaded_file = new_path
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# Move back to original directory
|
| 154 |
os.chdir(original_dir)
|
| 155 |
|
| 156 |
return DownloadResponse(
|
| 157 |
success=True,
|
| 158 |
-
message="
|
| 159 |
-
|
| 160 |
-
download_url=download_url
|
| 161 |
)
|
| 162 |
|
| 163 |
except subprocess.CalledProcessError as e:
|
|
@@ -176,6 +196,10 @@ async def download_file(request: DownloadRequest):
|
|
| 176 |
if 'original_dir' in locals():
|
| 177 |
os.chdir(original_dir)
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
@app.get("/test")
|
| 180 |
async def test():
|
| 181 |
"""Test endpoint to verify setup"""
|
|
|
|
| 10 |
import tempfile
|
| 11 |
from pathlib import Path
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
+
from urllib.parse import quote
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 72 |
# Mount the downloads directory
|
| 73 |
app.mount("/files", StaticFiles(directory=DOWNLOADS_DIR), name="files")
|
| 74 |
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# [Previous cookie handling code remains the same...]
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# [Previous cookie handling code remains the same...]
|
| 84 |
+
|
| 85 |
class DownloadRequest(BaseModel):
|
| 86 |
url: str
|
| 87 |
|
| 88 |
+
class FileInfo(BaseModel):
|
| 89 |
+
filename: str
|
| 90 |
+
download_url: str
|
| 91 |
+
file_type: str
|
| 92 |
+
|
| 93 |
class DownloadResponse(BaseModel):
|
| 94 |
success: bool
|
| 95 |
message: str
|
| 96 |
+
files: List[FileInfo]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
@app.post("/download", response_model=DownloadResponse)
|
| 99 |
async def download_file(request: DownloadRequest):
|
|
|
|
| 135 |
for root, dirs, files in os.walk('.'):
|
| 136 |
for file in files:
|
| 137 |
if file != "cookies.txt":
|
| 138 |
+
file_path = os.path.join(root, file)
|
| 139 |
+
all_files.append(file_path)
|
| 140 |
|
| 141 |
logger.info(f"All files found: {all_files}")
|
| 142 |
|
| 143 |
if not all_files:
|
| 144 |
raise Exception("No files found after download attempt")
|
| 145 |
|
| 146 |
+
# Process all downloaded files
|
| 147 |
+
downloaded_files = []
|
| 148 |
+
space_url = os.getenv("SPACE_URL", "http://localhost:7860")
|
| 149 |
+
|
| 150 |
+
for file_path in all_files:
|
| 151 |
+
# Get the filename without the directory structure
|
| 152 |
+
filename = os.path.basename(file_path)
|
| 153 |
|
| 154 |
+
# Move file to download directory root if it's in a subdirectory
|
| 155 |
+
if os.path.dirname(file_path):
|
| 156 |
+
new_path = os.path.join(download_subdir, filename)
|
| 157 |
+
shutil.move(file_path, new_path)
|
|
|
|
| 158 |
|
| 159 |
+
# Get file extension
|
| 160 |
+
file_type = os.path.splitext(filename)[1].lstrip('.')
|
| 161 |
+
|
| 162 |
+
# Generate download URL
|
| 163 |
+
encoded_filename = quote(filename)
|
| 164 |
+
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
| 165 |
+
|
| 166 |
+
downloaded_files.append(FileInfo(
|
| 167 |
+
filename=filename,
|
| 168 |
+
download_url=download_url,
|
| 169 |
+
file_type=file_type
|
| 170 |
+
))
|
| 171 |
+
|
| 172 |
+
logger.info(f"Processed file: {filename} -> {download_url}")
|
| 173 |
|
| 174 |
# Move back to original directory
|
| 175 |
os.chdir(original_dir)
|
| 176 |
|
| 177 |
return DownloadResponse(
|
| 178 |
success=True,
|
| 179 |
+
message=f"Successfully downloaded {len(downloaded_files)} files",
|
| 180 |
+
files=downloaded_files
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 196 |
if 'original_dir' in locals():
|
| 197 |
os.chdir(original_dir)
|
| 198 |
|
| 199 |
+
@app.get("/")
|
| 200 |
+
async def root():
|
| 201 |
+
return {"message": "Welcome to GAMDL API. Visit /docs for API documentation."}
|
| 202 |
+
|
| 203 |
@app.get("/test")
|
| 204 |
async def test():
|
| 205 |
"""Test endpoint to verify setup"""
|