tecuts commited on
Commit
bc70293
·
verified ·
1 Parent(s): 073fa28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -31
app.py CHANGED
@@ -11,7 +11,7 @@ import tempfile
11
  from pathlib import Path
12
  from dotenv import load_dotenv
13
  from urllib.parse import quote
14
- import time
15
 
16
  # Set up logging
17
  logging.basicConfig(level=logging.INFO)
@@ -68,15 +68,9 @@ app = FastAPI(
68
 
69
 
70
 
71
-
72
-
73
-
74
-
75
  # Create downloads directory if it doesn't exist
76
  DOWNLOADS_DIR = "downloads"
77
- TEMP_DIR = "temp"
78
  os.makedirs(DOWNLOADS_DIR, exist_ok=True)
79
- os.makedirs(TEMP_DIR, exist_ok=True)
80
 
81
  # Mount the downloads directory with cache control headers
82
  class CacheControlStaticFiles(StaticFiles):
@@ -106,23 +100,23 @@ class DownloadResponse(BaseModel):
106
  @app.post("/download", response_model=DownloadResponse)
107
  async def download_file(request: DownloadRequest):
108
  try:
109
- # Create a unique subdirectory for this download in the temporary directory
110
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
111
- temp_subdir = os.path.join(TEMP_DIR, timestamp)
112
- os.makedirs(temp_subdir, exist_ok=True)
113
 
114
  # Log the current working directory and download directory
115
  logger.info(f"Current working directory: {os.getcwd()}")
116
- logger.info(f"Temporary download directory: {temp_subdir}")
117
 
118
  # Create cookies file from environment variable
119
- cookie_path = os.path.join(temp_subdir, "cookies.txt")
120
  logger.info(f"Creating cookies file at: {cookie_path}")
121
  env_to_cookies_from_env(cookie_path)
122
 
123
- # Change to temporary download directory
124
  original_dir = os.getcwd()
125
- os.chdir(temp_subdir)
126
 
127
  # Log the command being executed
128
  cmd = ["gamdl", request.url]
@@ -144,30 +138,18 @@ async def download_file(request: DownloadRequest):
144
 
145
  # Get the downloaded filename
146
  files = [f for f in os.listdir() if f != "cookies.txt"]
147
- logger.info(f"Files in temporary download directory: {files}")
148
 
149
  if not files:
150
- raise Exception("No files found in temporary 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
156
- time.sleep(1) # Sleep for 1 second
157
 
158
  # Get the file size
159
  file_size = os.path.getsize(downloaded_file)
160
-
161
- # Check for potential issues in file handling
162
- if process.stderr:
163
- raise Exception(f"Error in downloaded file: {process.stderr}")
164
-
165
- # Move the completed download to the final directory
166
- final_download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
167
- os.makedirs(final_download_subdir, exist_ok=True)
168
- shutil.move(downloaded_file, final_download_subdir)
169
- downloaded_file_path = os.path.join(final_download_subdir, downloaded_file)
170
- logger.info(f"Moved downloaded file to: {downloaded_file_path}")
171
 
172
  # Generate the download URL and URL encode the filename
173
  space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
@@ -176,7 +158,7 @@ async def download_file(request: DownloadRequest):
176
  logger.info(f"Generated download URL: {download_url}")
177
 
178
  # Move back to original directory
179
- os.chdir(original_dir)
180
 
181
  return DownloadResponse(
182
  success=True,
 
11
  from pathlib import Path
12
  from dotenv import load_dotenv
13
  from urllib.parse import quote
14
+ import time # Import for sleep to ensure file is fully written
15
 
16
  # Set up logging
17
  logging.basicConfig(level=logging.INFO)
 
68
 
69
 
70
 
 
 
 
 
71
  # Create downloads directory if it doesn't exist
72
  DOWNLOADS_DIR = "downloads"
 
73
  os.makedirs(DOWNLOADS_DIR, exist_ok=True)
 
74
 
75
  # Mount the downloads directory with cache control headers
76
  class CacheControlStaticFiles(StaticFiles):
 
100
  @app.post("/download", response_model=DownloadResponse)
101
  async def download_file(request: DownloadRequest):
102
  try:
103
+ # Create a unique subdirectory for this download
104
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
105
+ download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
106
+ os.makedirs(download_subdir, exist_ok=True)
107
 
108
  # Log the current working directory and download directory
109
  logger.info(f"Current working directory: {os.getcwd()}")
110
+ logger.info(f"Download directory: {download_subdir}")
111
 
112
  # Create cookies file from environment variable
113
+ cookie_path = os.path.join(download_subdir, "cookies.txt")
114
  logger.info(f"Creating cookies file at: {cookie_path}")
115
  env_to_cookies_from_env(cookie_path)
116
 
117
+ # Change to download directory
118
  original_dir = os.getcwd()
119
+ os.chdir(download_subdir)
120
 
121
  # Log the command being executed
122
  cmd = ["gamdl", request.url]
 
138
 
139
  # Get the downloaded filename
140
  files = [f for f in os.listdir() if f != "cookies.txt"]
141
+ logger.info(f"Files in download directory: {files}")
142
 
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
+ # Ensure the file is fully written
149
+ time.sleep(2) # Sleep for 2 second
 
150
 
151
  # Get the file size
152
  file_size = os.path.getsize(downloaded_file)
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  # Generate the download URL and URL encode the filename
155
  space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
 
158
  logger.info(f"Generated download URL: {download_url}")
159
 
160
  # Move back to original directory
161
+
162
 
163
  return DownloadResponse(
164
  success=True,