tecuts commited on
Commit
5a90bee
·
verified ·
1 Parent(s): 2e5512b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -60
app.py CHANGED
@@ -100,21 +100,13 @@ class DownloadResponse(BaseModel):
100
 
101
 
102
 
103
-
104
- # Function to wait until the file is fully downloaded
105
- def wait_for_file_to_complete(file_path, initial_size, max_wait_time=60):
106
- wait_time = 0
107
- while True:
108
- current_size = os.path.getsize(file_path)
109
- if current_size == initial_size:
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.run(
132
  cmd,
133
- capture_output=True,
 
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
- if process.returncode != 0:
143
- raise Exception(f"Failed to download file: {process.stderr}")
 
144
 
145
- # Check all files in the current directory
146
- logger.info(f"All files in current directory: {os.listdir(download_subdir)}")
147
-
148
- files = [f for f in os.listdir(download_subdir) if f != "cookies.txt"]
149
- if not files:
150
- raise Exception("No files found in download directory after download attempt")
151
-
152
- downloaded_file = files[0]
153
- logger.info(f"Downloaded file: {downloaded_file}")
154
-
155
- # Ensure the file is fully written (wait for download to complete)
156
- initial_size = os.path.getsize(os.path.join(download_subdir, downloaded_file))
157
- wait_for_file_to_complete(os.path.join(download_subdir, downloaded_file), initial_size)
158
-
159
- # Verify the final file size
160
- file_size = os.path.getsize(os.path.join(download_subdir, downloaded_file))
161
- logger.info(f"Final file size: {file_size} bytes")
162
-
163
- # If the size is still incorrect, raise an error
164
- if file_size < 7_000_000: # Assuming 7 MB is the minimum expected file size
165
- raise Exception(f"File seems incomplete. Final size: {file_size} bytes.")
166
-
167
- # Generate the download URL and URL encode the filename
168
- space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
169
- encoded_filename = quote(downloaded_file)
170
- download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
171
- logger.info(f"Generated download URL: {download_url}")
172
-
173
- return DownloadResponse(
174
- success=True,
175
- message="File downloaded successfully",
176
- filename=downloaded_file,
177
- download_url=download_url,
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"""