Update app.py
Browse files
app.py
CHANGED
|
@@ -100,98 +100,72 @@ class DownloadResponse(BaseModel):
|
|
| 100 |
|
| 101 |
|
| 102 |
def wait_for_file_to_complete(file_path, initial_size, max_wait_time=30):
|
| 103 |
-
"""
|
| 104 |
-
Wait until the file size stops increasing for a set amount of time,
|
| 105 |
-
indicating the download has completed.
|
| 106 |
-
"""
|
| 107 |
wait_time = 0
|
| 108 |
while True:
|
| 109 |
current_size = os.path.getsize(file_path)
|
| 110 |
if current_size == initial_size:
|
| 111 |
wait_time += 1
|
| 112 |
if wait_time >= max_wait_time:
|
| 113 |
-
# File size hasn't changed for max_wait_time seconds
|
| 114 |
break
|
| 115 |
else:
|
| 116 |
-
# Reset wait time if the file is still growing
|
| 117 |
wait_time = 0
|
| 118 |
initial_size = current_size
|
| 119 |
-
time.sleep(1)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
@app.post("/download", response_model=DownloadResponse)
|
| 125 |
async def download_file(request: DownloadRequest):
|
| 126 |
try:
|
| 127 |
-
# Create a unique subdirectory for this download
|
| 128 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 129 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
| 130 |
os.makedirs(download_subdir, exist_ok=True)
|
| 131 |
-
|
| 132 |
-
# Log the current working directory and download directory
|
| 133 |
-
logger.info(f"Current working directory: {os.getcwd()}")
|
| 134 |
-
logger.info(f"Download directory: {download_subdir}")
|
| 135 |
-
|
| 136 |
-
# Create cookies file from environment variable
|
| 137 |
-
cookie_path = os.path.join(download_subdir, "cookies.txt")
|
| 138 |
-
logger.info(f"Creating cookies file at: {cookie_path}")
|
| 139 |
-
env_to_cookies_from_env(cookie_path)
|
| 140 |
-
|
| 141 |
-
# Change to download directory
|
| 142 |
-
original_dir = os.getcwd()
|
| 143 |
-
os.chdir(download_subdir)
|
| 144 |
-
|
| 145 |
# Log the command being executed
|
| 146 |
cmd = ["gamdl", request.url]
|
| 147 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 148 |
|
| 149 |
-
# Run gamdl command
|
| 150 |
process = subprocess.run(
|
| 151 |
cmd,
|
| 152 |
capture_output=True,
|
| 153 |
text=True
|
| 154 |
)
|
| 155 |
-
|
| 156 |
# Log the command output
|
| 157 |
logger.info(f"Command stdout: {process.stdout}")
|
| 158 |
logger.info(f"Command stderr: {process.stderr}")
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
# Get the downloaded filename
|
| 164 |
-
files = [f for f in os.listdir() if f != "cookies.txt"]
|
| 165 |
-
logger.info(f"Files in download directory: {files}")
|
| 166 |
-
|
| 167 |
if not files:
|
| 168 |
raise Exception("No files found in download directory after download attempt")
|
| 169 |
-
|
| 170 |
downloaded_file = files[0]
|
| 171 |
logger.info(f"Downloaded file: {downloaded_file}")
|
| 172 |
-
|
| 173 |
# Ensure the file is fully written (wait for download to complete)
|
| 174 |
-
initial_size = os.path.getsize(downloaded_file)
|
| 175 |
-
wait_for_file_to_complete(downloaded_file, initial_size)
|
| 176 |
-
|
| 177 |
-
#
|
| 178 |
-
file_size = os.path.getsize(downloaded_file)
|
| 179 |
-
|
|
|
|
| 180 |
# Generate the download URL and URL encode the filename
|
| 181 |
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
| 182 |
-
encoded_filename = quote(downloaded_file)
|
| 183 |
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
| 184 |
logger.info(f"Generated download URL: {download_url}")
|
| 185 |
-
|
| 186 |
-
# Move back to original directory
|
| 187 |
-
os.chdir(original_dir)
|
| 188 |
-
|
| 189 |
return DownloadResponse(
|
| 190 |
success=True,
|
| 191 |
message="File downloaded successfully",
|
| 192 |
filename=downloaded_file,
|
| 193 |
download_url=download_url,
|
| 194 |
-
file_size=file_size
|
| 195 |
)
|
| 196 |
|
| 197 |
except subprocess.CalledProcessError as e:
|
|
@@ -206,10 +180,6 @@ async def download_file(request: DownloadRequest):
|
|
| 206 |
status_code=500,
|
| 207 |
detail=f"Error: {str(e)}"
|
| 208 |
)
|
| 209 |
-
finally:
|
| 210 |
-
# Always try to return to the original directory
|
| 211 |
-
if 'original_dir' in locals():
|
| 212 |
-
os.chdir(original_dir)
|
| 213 |
|
| 214 |
|
| 215 |
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
def wait_for_file_to_complete(file_path, initial_size, max_wait_time=30):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
wait_time = 0
|
| 104 |
while True:
|
| 105 |
current_size = os.path.getsize(file_path)
|
| 106 |
if current_size == initial_size:
|
| 107 |
wait_time += 1
|
| 108 |
if wait_time >= max_wait_time:
|
|
|
|
| 109 |
break
|
| 110 |
else:
|
|
|
|
| 111 |
wait_time = 0
|
| 112 |
initial_size = current_size
|
| 113 |
+
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
@app.post("/download", response_model=DownloadResponse)
|
| 116 |
async def download_file(request: DownloadRequest):
|
| 117 |
try:
|
|
|
|
| 118 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 119 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
| 120 |
os.makedirs(download_subdir, exist_ok=True)
|
| 121 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# Log the command being executed
|
| 123 |
cmd = ["gamdl", request.url]
|
| 124 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
| 125 |
|
| 126 |
+
# Run gamdl command
|
| 127 |
process = subprocess.run(
|
| 128 |
cmd,
|
| 129 |
capture_output=True,
|
| 130 |
text=True
|
| 131 |
)
|
| 132 |
+
|
| 133 |
# Log the command output
|
| 134 |
logger.info(f"Command stdout: {process.stdout}")
|
| 135 |
logger.info(f"Command stderr: {process.stderr}")
|
| 136 |
+
logger.info(f"Return code: {process.returncode}")
|
| 137 |
|
| 138 |
+
if process.returncode != 0:
|
| 139 |
+
raise Exception(f"Failed to download file: {process.stderr}")
|
| 140 |
+
|
| 141 |
# Get the downloaded filename
|
| 142 |
+
files = [f for f in os.listdir(download_subdir) if f != "cookies.txt"]
|
|
|
|
|
|
|
| 143 |
if not files:
|
| 144 |
raise Exception("No files found in download directory after download attempt")
|
| 145 |
+
|
| 146 |
downloaded_file = files[0]
|
| 147 |
logger.info(f"Downloaded file: {downloaded_file}")
|
| 148 |
+
|
| 149 |
# Ensure the file is fully written (wait for download to complete)
|
| 150 |
+
initial_size = os.path.getsize(os.path.join(download_subdir, downloaded_file))
|
| 151 |
+
wait_for_file_to_complete(os.path.join(download_subdir, downloaded_file), initial_size)
|
| 152 |
+
|
| 153 |
+
# Verify the final file size
|
| 154 |
+
file_size = os.path.getsize(os.path.join(download_subdir, downloaded_file))
|
| 155 |
+
logger.info(f"Final file size: {file_size} bytes")
|
| 156 |
+
|
| 157 |
# Generate the download URL and URL encode the filename
|
| 158 |
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
| 159 |
+
encoded_filename = quote(downloaded_file)
|
| 160 |
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
| 161 |
logger.info(f"Generated download URL: {download_url}")
|
| 162 |
+
|
|
|
|
|
|
|
|
|
|
| 163 |
return DownloadResponse(
|
| 164 |
success=True,
|
| 165 |
message="File downloaded successfully",
|
| 166 |
filename=downloaded_file,
|
| 167 |
download_url=download_url,
|
| 168 |
+
file_size=file_size
|
| 169 |
)
|
| 170 |
|
| 171 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 180 |
status_code=500,
|
| 181 |
detail=f"Error: {str(e)}"
|
| 182 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
|