Update app.py
Browse files
app.py
CHANGED
|
@@ -100,21 +100,13 @@ class DownloadResponse(BaseModel):
|
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
wait_time += 1
|
| 111 |
-
if wait_time >= max_wait_time:
|
| 112 |
-
logger.info(f"Timeout reached waiting for file to complete: {file_path}")
|
| 113 |
-
break
|
| 114 |
-
else:
|
| 115 |
-
wait_time = 0
|
| 116 |
-
initial_size = current_size
|
| 117 |
-
time.sleep(2) # Wait 2 seconds before checking again
|
| 118 |
|
| 119 |
@app.post("/download", response_model=DownloadResponse)
|
| 120 |
async def download_file(request: DownloadRequest):
|
|
@@ -127,56 +119,51 @@ async def download_file(request: DownloadRequest):
|
|
| 127 |
cmd = ["gamdl", "--output-path", download_subdir, request.url]
|
| 128 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 129 |
|
| 130 |
-
# Run gamdl command
|
| 131 |
-
process = subprocess.
|
| 132 |
cmd,
|
| 133 |
-
|
|
|
|
| 134 |
text=True
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
# Log the command output
|
| 138 |
-
logger.info(f"Gamdl stdout: {process.stdout}")
|
| 139 |
-
logger.info(f"Gamdl stderr: {process.stderr}")
|
| 140 |
-
logger.info(f"Gamdl returncode: {process.returncode}")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
file_size=file_size
|
| 179 |
-
)
|
| 180 |
|
| 181 |
except subprocess.CalledProcessError as e:
|
| 182 |
logger.error(f"Download process failed: stdout={e.stdout}, stderr={e.stderr}")
|
|
@@ -192,6 +179,7 @@ async def download_file(request: DownloadRequest):
|
|
| 192 |
)
|
| 193 |
|
| 194 |
|
|
|
|
| 195 |
@app.get("/test")
|
| 196 |
async def test():
|
| 197 |
"""Test endpoint to verify setup"""
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
+
# Function to monitor gamdl output for "Done" message
|
| 104 |
+
def wait_for_done_message(process):
|
| 105 |
+
for line in process.stdout:
|
| 106 |
+
logger.info(f"Gamdl stdout: {line.strip()}")
|
| 107 |
+
if "Done" in line:
|
| 108 |
+
return True
|
| 109 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
@app.post("/download", response_model=DownloadResponse)
|
| 112 |
async def download_file(request: DownloadRequest):
|
|
|
|
| 119 |
cmd = ["gamdl", "--output-path", download_subdir, request.url]
|
| 120 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 121 |
|
| 122 |
+
# Run gamdl command and capture stdout
|
| 123 |
+
process = subprocess.Popen(
|
| 124 |
cmd,
|
| 125 |
+
stdout=subprocess.PIPE,
|
| 126 |
+
stderr=subprocess.PIPE,
|
| 127 |
text=True
|
| 128 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Wait for the "Done" message from the gamdl command output
|
| 131 |
+
if wait_for_done_message(process):
|
| 132 |
+
logger.info("Download completed successfully.")
|
| 133 |
|
| 134 |
+
# Check all files in the download directory
|
| 135 |
+
logger.info(f"All files in current directory: {os.listdir(download_subdir)}")
|
| 136 |
+
|
| 137 |
+
files = [f for f in os.listdir(download_subdir) if f != "cookies.txt"]
|
| 138 |
+
if not files:
|
| 139 |
+
raise Exception("No files found in download directory after download attempt")
|
| 140 |
+
|
| 141 |
+
downloaded_file = files[0]
|
| 142 |
+
logger.info(f"Downloaded file: {downloaded_file}")
|
| 143 |
+
|
| 144 |
+
# Verify the final file size
|
| 145 |
+
file_size = os.path.getsize(os.path.join(download_subdir, downloaded_file))
|
| 146 |
+
logger.info(f"Final file size: {file_size} bytes")
|
| 147 |
+
|
| 148 |
+
# If the size is still incorrect, raise an error
|
| 149 |
+
if file_size < 5_000_000: # Assuming 7 MB is the minimum expected file size
|
| 150 |
+
raise Exception(f"File seems incomplete. Final size: {file_size} bytes.")
|
| 151 |
+
|
| 152 |
+
# Generate the download URL and URL encode the filename
|
| 153 |
+
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
| 154 |
+
encoded_filename = quote(downloaded_file)
|
| 155 |
+
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
| 156 |
+
logger.info(f"Generated download URL: {download_url}")
|
| 157 |
+
|
| 158 |
+
return DownloadResponse(
|
| 159 |
+
success=True,
|
| 160 |
+
message="File downloaded successfully",
|
| 161 |
+
filename=downloaded_file,
|
| 162 |
+
download_url=download_url,
|
| 163 |
+
file_size=file_size
|
| 164 |
+
)
|
| 165 |
+
else:
|
| 166 |
+
raise Exception("Gamdl did not complete successfully. No 'Done' message found in output.")
|
|
|
|
|
|
|
| 167 |
|
| 168 |
except subprocess.CalledProcessError as e:
|
| 169 |
logger.error(f"Download process failed: stdout={e.stdout}, stderr={e.stderr}")
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
|
| 182 |
+
|
| 183 |
@app.get("/test")
|
| 184 |
async def test():
|
| 185 |
"""Test endpoint to verify setup"""
|