aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-01-18 16:20:33 +0100
committersalmonik <raisersoft@proton.me>2026-01-18 16:20:33 +0100
commitcb625506be95cec4d4a402119eec0c79699142b9 (patch)
tree7d5a60f7754d6e753a9c4f853c2fbccd9222973b
parentjarvis (diff)
downloadsenator-sperma-cb625506be95cec4d4a402119eec0c79699142b9.tar.gz
senator-sperma-cb625506be95cec4d4a402119eec0c79699142b9.zip
fix tmp dir being deleted before sending file
Diffstat (limited to '')
-rw-r--r--cogs/downloader.py24
-rw-r--r--cogs/jarvis.py5
2 files changed, 19 insertions, 10 deletions
diff --git a/cogs/downloader.py b/cogs/downloader.py
index 42acc1d..a2e2299 100644
--- a/cogs/downloader.py
+++ b/cogs/downloader.py
@@ -70,7 +70,7 @@ class Downloader(commands.Cog):
70 try: 70 try:
71 logger.info(f"Downloading {url} as {fformat}") 71 logger.info(f"Downloading {url} as {fformat}")
72 72
73 with tempfile.TemporaryDirectory() as temp_dir: 73 with tempfile.TemporaryDirectory(delete=False) as temp_dir:
74 # Configure yt-dlp to use the original filename 74 # Configure yt-dlp to use the original filename
75 ydl_opts = { 75 ydl_opts = {
76 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'), 76 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
@@ -151,22 +151,24 @@ class Downloader(commands.Cog):
151 "status": "success", 151 "status": "success",
152 "type": "url", 152 "type": "url",
153 "content": dl_url, 153 "content": dl_url,
154 "format": fformat, 154 "filename": filename,
155 "filename": filename 155 "temp_dir": temp_dir
156 } 156 }
157 else: 157 else:
158 return { 158 return {
159 "status": "error", 159 "status": "error",
160 "type": "error", 160 "type": "error",
161 "content": "Invalid response from file server", 161 "content": "Invalid response from file server",
162 "details": str(result) 162 "details": str(result),
163 "temp_dir": temp_dir
163 } 164 }
164 except ValueError: 165 except ValueError:
165 return { 166 return {
166 "status": "error", 167 "status": "error",
167 "type": "error", 168 "type": "error",
168 "content": "Failed to parse server response", 169 "content": "Failed to parse server response",
169 "details": response.text[:200] # First 200 chars of response 170 "details": response.text[:200], # First 200 chars of response
171 "temp_dir": temp_dir
170 } 172 }
171 else: 173 else:
172 error_msg = response.text if response.text else 'Unknown error' 174 error_msg = response.text if response.text else 'Unknown error'
@@ -174,7 +176,8 @@ class Downloader(commands.Cog):
174 "status": "error", 176 "status": "error",
175 "type": "error", 177 "type": "error",
176 "content": f"Server returned {response.status_code}", 178 "content": f"Server returned {response.status_code}",
177 "details": error_msg 179 "details": error_msg,
180 "temp_dir": temp_dir
178 } 181 }
179 182
180 except Exception as upload_error: 183 except Exception as upload_error:
@@ -182,7 +185,8 @@ class Downloader(commands.Cog):
182 "status": "error", 185 "status": "error",
183 "type": "error", 186 "type": "error",
184 "content": "Upload failed", 187 "content": "Upload failed",
185 "details": str(upload_error) 188 "details": str(upload_error),
189 "temp_dir": temp_dir
186 } 190 }
187 191
188 else: 192 else:
@@ -191,7 +195,8 @@ class Downloader(commands.Cog):
191 "status": "success", 195 "status": "success",
192 "type": "file", 196 "type": "file",
193 "content": actual_file, 197 "content": actual_file,
194 "filename": filename 198 "filename": filename,
199 "temp_dir": temp_dir
195 } 200 }
196 201
197 except Exception as e: 202 except Exception as e:
@@ -201,7 +206,8 @@ class Downloader(commands.Cog):
201 "status": "error", 206 "status": "error",
202 "type": "error", 207 "type": "error",
203 "content": "Download failed", 208 "content": "Download failed",
204 "details": str(e) 209 "details": str(e),
210 "temp_dir": temp_dir
205 } 211 }
206 212
207 213
diff --git a/cogs/jarvis.py b/cogs/jarvis.py
index 0f4606b..21f71f6 100644
--- a/cogs/jarvis.py
+++ b/cogs/jarvis.py
@@ -4,6 +4,7 @@ from discord.ext import commands
4import requests 4import requests
5import json 5import json
6import logging 6import logging
7import os
7 8
8from cogs.compression import Compression 9from cogs.compression import Compression
9from cogs.downloader import Downloader 10from cogs.downloader import Downloader
@@ -78,7 +79,7 @@ class Jarvis(commands.Cog):
78 79
79 if action == "download": 80 if action == "download":
80 # Extract the URL from the message using regex 81 # Extract the URL from the message using regex
81 url_match = re.search(r'(https?://\S+)', message.content).group(0) 82 url_match = re.search(r'https://(?:[A-Za-z0-9-]+\.)*[A-Za-z0-9-]{1,63}\.[A-Za-z]{2,63}/\S*', message.content).group(0)
82 if not url_match: 83 if not url_match:
83 await channel.send("Nie znaleziono poprawnego linku w odpowiedzi.") 84 await channel.send("Nie znaleziono poprawnego linku w odpowiedzi.")
84 return 85 return
@@ -91,10 +92,12 @@ class Jarvis(commands.Cog):
91 elif response["type"] == "file": 92 elif response["type"] == "file":
92 file_data = response["content"] 93 file_data = response["content"]
93 await channel.send("wysyłon:", file=discord.File(file_data)) 94 await channel.send("wysyłon:", file=discord.File(file_data))
95 os.remove(response["temp_dir"])
94 else: 96 else:
95 logger.error(f"Error: {response['content']}") 97 logger.error(f"Error: {response['content']}")
96 if 'details' in response: 98 if 'details' in response:
97 logger.error(f"Details: {response['details']}") 99 logger.error(f"Details: {response['details']}")
100 os.remove(response["temp_dir"])
98 101
99 elif action == "compress": 102 elif action == "compress":
100 pass 103 pass