Update app.py
Browse files
app.py
CHANGED
|
@@ -80,6 +80,10 @@ class DownloadResponse(BaseModel):
|
|
| 80 |
filename: str = None
|
| 81 |
download_url: str = None
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
@app.post("/download", response_model=DownloadResponse)
|
| 84 |
async def download_file(request: DownloadRequest):
|
| 85 |
try:
|
|
@@ -88,7 +92,6 @@ async def download_file(request: DownloadRequest):
|
|
| 88 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
| 89 |
os.makedirs(download_subdir, exist_ok=True)
|
| 90 |
|
| 91 |
-
# Log the current working directory and download directory
|
| 92 |
logger.info(f"Current working directory: {os.getcwd()}")
|
| 93 |
logger.info(f"Download directory: {download_subdir}")
|
| 94 |
|
|
@@ -101,37 +104,50 @@ async def download_file(request: DownloadRequest):
|
|
| 101 |
original_dir = os.getcwd()
|
| 102 |
os.chdir(download_subdir)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
cmd = ["gamdl", request.url]
|
| 106 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 107 |
|
| 108 |
-
# Run gamdl command with more detailed output
|
| 109 |
process = subprocess.run(
|
| 110 |
cmd,
|
| 111 |
capture_output=True,
|
| 112 |
text=True
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# Log the command output
|
| 116 |
logger.info(f"Command stdout: {process.stdout}")
|
| 117 |
logger.info(f"Command stderr: {process.stderr}")
|
| 118 |
|
| 119 |
-
# Check if the command was successful
|
| 120 |
process.check_returncode()
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
logger.info(f"Downloaded file: {downloaded_file}")
|
| 131 |
|
| 132 |
-
# Generate the download URL
|
| 133 |
-
space_url = os.getenv("SPACE_URL", "http://
|
| 134 |
-
|
|
|
|
| 135 |
logger.info(f"Generated download URL: {download_url}")
|
| 136 |
|
| 137 |
# Move back to original directory
|
|
@@ -157,7 +173,6 @@ async def download_file(request: DownloadRequest):
|
|
| 157 |
detail=f"Error: {str(e)}"
|
| 158 |
)
|
| 159 |
finally:
|
| 160 |
-
# Always try to return to the original directory
|
| 161 |
if 'original_dir' in locals():
|
| 162 |
os.chdir(original_dir)
|
| 163 |
|
|
|
|
| 80 |
filename: str = None
|
| 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):
|
| 89 |
try:
|
|
|
|
| 92 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
| 93 |
os.makedirs(download_subdir, exist_ok=True)
|
| 94 |
|
|
|
|
| 95 |
logger.info(f"Current working directory: {os.getcwd()}")
|
| 96 |
logger.info(f"Download directory: {download_subdir}")
|
| 97 |
|
|
|
|
| 104 |
original_dir = os.getcwd()
|
| 105 |
os.chdir(download_subdir)
|
| 106 |
|
| 107 |
+
# Run gamdl command
|
| 108 |
cmd = ["gamdl", request.url]
|
| 109 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 110 |
|
|
|
|
| 111 |
process = subprocess.run(
|
| 112 |
cmd,
|
| 113 |
capture_output=True,
|
| 114 |
text=True
|
| 115 |
)
|
| 116 |
|
|
|
|
| 117 |
logger.info(f"Command stdout: {process.stdout}")
|
| 118 |
logger.info(f"Command stderr: {process.stderr}")
|
| 119 |
|
|
|
|
| 120 |
process.check_returncode()
|
| 121 |
|
| 122 |
+
# Find all files recursively in the download directory
|
| 123 |
+
all_files = []
|
| 124 |
+
for root, dirs, files in os.walk('.'):
|
| 125 |
+
for file in files:
|
| 126 |
+
if file != "cookies.txt":
|
| 127 |
+
all_files.append(os.path.join(root, file))
|
| 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 |
+
# Get the first downloaded file (usually there's only one)
|
| 135 |
+
downloaded_file = all_files[0]
|
| 136 |
+
if downloaded_file.startswith('./'):
|
| 137 |
+
downloaded_file = downloaded_file[2:] # Remove leading ./
|
| 138 |
+
|
| 139 |
+
# Move the file to the download directory root if it's in a subdirectory
|
| 140 |
+
if os.path.dirname(downloaded_file):
|
| 141 |
+
new_path = os.path.basename(downloaded_file)
|
| 142 |
+
shutil.move(downloaded_file, new_path)
|
| 143 |
+
downloaded_file = new_path
|
| 144 |
|
| 145 |
+
logger.info(f"Final downloaded file: {downloaded_file}")
|
|
|
|
| 146 |
|
| 147 |
+
# Generate the download URL with proper URL encoding
|
| 148 |
+
space_url = os.getenv("SPACE_URL", "http://tecuts-testing.hf.space")
|
| 149 |
+
encoded_filename = quote(downloaded_file)
|
| 150 |
+
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
| 151 |
logger.info(f"Generated download URL: {download_url}")
|
| 152 |
|
| 153 |
# Move back to original directory
|
|
|
|
| 173 |
detail=f"Error: {str(e)}"
|
| 174 |
)
|
| 175 |
finally:
|
|
|
|
| 176 |
if 'original_dir' in locals():
|
| 177 |
os.chdir(original_dir)
|
| 178 |
|