Update app.py
Browse files
app.py
CHANGED
@@ -11,8 +11,6 @@ import tempfile
|
|
11 |
from pathlib import Path
|
12 |
from dotenv import load_dotenv
|
13 |
from urllib.parse import quote
|
14 |
-
from pydantic import BaseModel
|
15 |
-
import time # Import for sleep to ensure file is fully written
|
16 |
|
17 |
# Set up logging
|
18 |
logging.basicConfig(level=logging.INFO)
|
@@ -98,73 +96,75 @@ class DownloadResponse(BaseModel):
|
|
98 |
file_size: int = None # Include file size in the response
|
99 |
|
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 "100%" in line:
|
108 |
-
return True
|
109 |
-
return False
|
110 |
-
|
111 |
@app.post("/download", response_model=DownloadResponse)
|
112 |
async def download_file(request: DownloadRequest):
|
113 |
try:
|
|
|
114 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
115 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
116 |
os.makedirs(download_subdir, exist_ok=True)
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
# Log the command being executed
|
119 |
-
cmd = ["
|
120 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
121 |
|
122 |
-
# Run gamdl command
|
123 |
-
process = subprocess.
|
124 |
cmd,
|
125 |
-
|
126 |
-
stderr=subprocess.PIPE,
|
127 |
text=True
|
128 |
)
|
129 |
|
130 |
-
#
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}")
|
170 |
raise HTTPException(
|
@@ -177,6 +177,10 @@ async def download_file(request: DownloadRequest):
|
|
177 |
status_code=500,
|
178 |
detail=f"Error: {str(e)}"
|
179 |
)
|
|
|
|
|
|
|
|
|
180 |
|
181 |
|
182 |
|
@@ -189,7 +193,7 @@ async def test():
|
|
189 |
env_to_cookies_from_env(temp_cookie)
|
190 |
|
191 |
# Test gamdl installation
|
192 |
-
process = subprocess.run(["
|
193 |
|
194 |
return {
|
195 |
"gamdl_version": process.stdout.strip(),
|
|
|
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)
|
|
|
96 |
file_size: int = None # Include file size in the response
|
97 |
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
@app.post("/download", response_model=DownloadResponse)
|
100 |
async def download_file(request: DownloadRequest):
|
101 |
try:
|
102 |
+
# Create a unique subdirectory for this download
|
103 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
104 |
download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
105 |
os.makedirs(download_subdir, exist_ok=True)
|
106 |
+
|
107 |
+
# Log the current working directory and download directory
|
108 |
+
logger.info(f"Current working directory: {os.getcwd()}")
|
109 |
+
logger.info(f"Download directory: {download_subdir}")
|
110 |
+
|
111 |
+
# Create cookies file from environment variable
|
112 |
+
cookie_path = os.path.join(download_subdir, "cookies.txt")
|
113 |
+
logger.info(f"Creating cookies file at: {cookie_path}")
|
114 |
+
env_to_cookies_from_env(cookie_path)
|
115 |
+
|
116 |
+
# Change to download directory
|
117 |
+
original_dir = os.getcwd()
|
118 |
+
os.chdir(download_subdir)
|
119 |
+
|
120 |
# Log the command being executed
|
121 |
+
cmd = ["votify", request.url]
|
122 |
logger.info(f"Executing command: {' '.join(cmd)}")
|
123 |
|
124 |
+
# Run gamdl command with more detailed output
|
125 |
+
process = subprocess.run(
|
126 |
cmd,
|
127 |
+
capture_output=True,
|
|
|
128 |
text=True
|
129 |
)
|
130 |
|
131 |
+
# Log the command output
|
132 |
+
logger.info(f"Command stdout: {process.stdout}")
|
133 |
+
logger.info(f"Command stderr: {process.stderr}")
|
134 |
+
|
135 |
+
# Check if the command was successful
|
136 |
+
process.check_returncode()
|
137 |
+
|
138 |
+
# Get the downloaded filename
|
139 |
+
files = [f for f in os.listdir() if f != "cookies.txt"]
|
140 |
+
logger.info(f"Files in download directory: {files}")
|
141 |
+
|
142 |
+
if not files:
|
143 |
+
raise Exception("No files found in download directory after download attempt")
|
144 |
+
|
145 |
+
downloaded_file = files[0]
|
146 |
+
logger.info(f"Downloaded file: {downloaded_file}")
|
147 |
|
148 |
+
# Get the file size
|
149 |
+
file_size = os.path.getsize(downloaded_file)
|
150 |
+
|
151 |
+
# Generate the download URL and URL encode the filename
|
152 |
+
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
153 |
+
encoded_filename = quote(downloaded_file) # URL encode the filename
|
154 |
+
download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
|
155 |
+
logger.info(f"Generated download URL: {download_url}")
|
156 |
+
|
157 |
+
# Move back to original directory
|
158 |
+
|
159 |
+
|
160 |
+
return DownloadResponse(
|
161 |
+
success=True,
|
162 |
+
message="File downloaded successfully",
|
163 |
+
filename=downloaded_file,
|
164 |
+
download_url=download_url,
|
165 |
+
file_size=file_size # Include the file size in the response
|
166 |
+
)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
except subprocess.CalledProcessError as e:
|
169 |
logger.error(f"Download process failed: stdout={e.stdout}, stderr={e.stderr}")
|
170 |
raise HTTPException(
|
|
|
177 |
status_code=500,
|
178 |
detail=f"Error: {str(e)}"
|
179 |
)
|
180 |
+
finally:
|
181 |
+
# Always try to return to the original directory
|
182 |
+
if 'original_dir' in locals():
|
183 |
+
os.chdir(original_dir)
|
184 |
|
185 |
|
186 |
|
|
|
193 |
env_to_cookies_from_env(temp_cookie)
|
194 |
|
195 |
# Test gamdl installation
|
196 |
+
process = subprocess.run(["votify", "--version"], capture_output=True, text=True)
|
197 |
|
198 |
return {
|
199 |
"gamdl_version": process.stdout.strip(),
|