Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,13 @@ from pydantic import BaseModel
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
import shutil
|
|
|
8 |
from datetime import datetime
|
9 |
|
|
|
|
|
|
|
|
|
10 |
app = FastAPI(
|
11 |
title="GAMDL API",
|
12 |
description="API for downloading Google Drive files using gamdl",
|
@@ -37,29 +42,46 @@ async def download_file(request: DownloadRequest):
|
|
37 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
38 |
os.makedirs(download_subdir, exist_ok=True)
|
39 |
|
|
|
|
|
|
|
|
|
40 |
# Change to download directory
|
41 |
original_dir = os.getcwd()
|
42 |
os.chdir(download_subdir)
|
43 |
|
44 |
-
#
|
|
|
|
|
|
|
|
|
45 |
process = subprocess.run(
|
46 |
-
|
47 |
capture_output=True,
|
48 |
-
text=True
|
49 |
-
check=True
|
50 |
)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Get the downloaded filename
|
53 |
files = os.listdir()
|
|
|
|
|
54 |
if not files:
|
55 |
-
raise Exception("No
|
56 |
|
57 |
downloaded_file = files[0]
|
|
|
58 |
|
59 |
# Generate the download URL
|
60 |
-
# Using the Space's domain - you'll need to replace this with your actual Space URL
|
61 |
space_url = os.getenv("SPACE_URL", "http://localhost:7860")
|
62 |
download_url = f"{space_url}/files/{timestamp}/{downloaded_file}"
|
|
|
63 |
|
64 |
# Move back to original directory
|
65 |
os.chdir(original_dir)
|
@@ -72,33 +94,38 @@ async def download_file(request: DownloadRequest):
|
|
72 |
)
|
73 |
|
74 |
except subprocess.CalledProcessError as e:
|
|
|
75 |
raise HTTPException(
|
76 |
status_code=400,
|
77 |
-
detail=f"Failed to download: {e.stderr}"
|
78 |
)
|
79 |
except Exception as e:
|
|
|
80 |
raise HTTPException(
|
81 |
status_code=500,
|
82 |
detail=f"Error: {str(e)}"
|
83 |
)
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
# Add basic homepage with API documentation link
|
86 |
@app.get("/")
|
87 |
async def root():
|
88 |
return {"message": "Welcome to GAMDL API. Visit /docs for API documentation."}
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
"""Remove files older than 24 hours"""
|
94 |
try:
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
except Exception as e:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
import shutil
|
8 |
+
import logging
|
9 |
from datetime import datetime
|
10 |
|
11 |
+
# Set up logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
app = FastAPI(
|
16 |
title="GAMDL API",
|
17 |
description="API for downloading Google Drive files using gamdl",
|
|
|
42 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
43 |
os.makedirs(download_subdir, exist_ok=True)
|
44 |
|
45 |
+
# Log the current working directory and download directory
|
46 |
+
logger.info(f"Current working directory: {os.getcwd()}")
|
47 |
+
logger.info(f"Download directory: {download_subdir}")
|
48 |
+
|
49 |
# Change to download directory
|
50 |
original_dir = os.getcwd()
|
51 |
os.chdir(download_subdir)
|
52 |
|
53 |
+
# Log the command being executed
|
54 |
+
cmd = ["gamdl", request.url]
|
55 |
+
logger.info(f"Executing command: {' '.join(cmd)}")
|
56 |
+
|
57 |
+
# Run gamdl command with more detailed output
|
58 |
process = subprocess.run(
|
59 |
+
cmd,
|
60 |
capture_output=True,
|
61 |
+
text=True
|
|
|
62 |
)
|
63 |
|
64 |
+
# Log the command output
|
65 |
+
logger.info(f"Command stdout: {process.stdout}")
|
66 |
+
logger.info(f"Command stderr: {process.stderr}")
|
67 |
+
|
68 |
+
# Check if the command was successful
|
69 |
+
process.check_returncode()
|
70 |
+
|
71 |
# Get the downloaded filename
|
72 |
files = os.listdir()
|
73 |
+
logger.info(f"Files in download directory: {files}")
|
74 |
+
|
75 |
if not files:
|
76 |
+
raise Exception("No files found in download directory after download attempt")
|
77 |
|
78 |
downloaded_file = files[0]
|
79 |
+
logger.info(f"Downloaded file: {downloaded_file}")
|
80 |
|
81 |
# Generate the download URL
|
|
|
82 |
space_url = os.getenv("SPACE_URL", "http://localhost:7860")
|
83 |
download_url = f"{space_url}/files/{timestamp}/{downloaded_file}"
|
84 |
+
logger.info(f"Generated download URL: {download_url}")
|
85 |
|
86 |
# Move back to original directory
|
87 |
os.chdir(original_dir)
|
|
|
94 |
)
|
95 |
|
96 |
except subprocess.CalledProcessError as e:
|
97 |
+
logger.error(f"Download process failed: stdout={e.stdout}, stderr={e.stderr}")
|
98 |
raise HTTPException(
|
99 |
status_code=400,
|
100 |
+
detail=f"Failed to download: {e.stderr or e.stdout or str(e)}"
|
101 |
)
|
102 |
except Exception as e:
|
103 |
+
logger.error(f"Unexpected error: {str(e)}", exc_info=True)
|
104 |
raise HTTPException(
|
105 |
status_code=500,
|
106 |
detail=f"Error: {str(e)}"
|
107 |
)
|
108 |
+
finally:
|
109 |
+
# Always try to return to the original directory
|
110 |
+
if 'original_dir' in locals():
|
111 |
+
os.chdir(original_dir)
|
112 |
|
|
|
113 |
@app.get("/")
|
114 |
async def root():
|
115 |
return {"message": "Welcome to GAMDL API. Visit /docs for API documentation."}
|
116 |
|
117 |
+
@app.get("/test")
|
118 |
+
async def test():
|
119 |
+
"""Test endpoint to verify gamdl installation"""
|
|
|
120 |
try:
|
121 |
+
process = subprocess.run(["gamdl", "--version"], capture_output=True, text=True)
|
122 |
+
return {
|
123 |
+
"gamdl_version": process.stdout.strip(),
|
124 |
+
"installed": True,
|
125 |
+
"error": process.stderr if process.stderr else None
|
126 |
+
}
|
127 |
except Exception as e:
|
128 |
+
return {
|
129 |
+
"installed": False,
|
130 |
+
"error": str(e)
|
131 |
+
}
|