- API/app.py +33 -28
API/app.py
CHANGED
|
@@ -225,28 +225,33 @@ async def download_image(session, post_id, post_url):
|
|
| 225 |
content = await response.read()
|
| 226 |
return (post_id, content)
|
| 227 |
|
| 228 |
-
async def
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
@app.post("/download")
|
| 252 |
async def download(
|
|
@@ -254,13 +259,13 @@ async def download(
|
|
| 254 |
):
|
| 255 |
posts = items.posts
|
| 256 |
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
|
| 257 |
-
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
|
| 265 |
@app.get("/")
|
| 266 |
async def read_root():
|
|
|
|
| 225 |
content = await response.read()
|
| 226 |
return (post_id, content)
|
| 227 |
|
| 228 |
+
async def tar_stream_generator(posts, session):
|
| 229 |
+
for post_id, image_url in posts.items():
|
| 230 |
+
try:
|
| 231 |
+
# Download the image data
|
| 232 |
+
image_data = await download_image(session, post_id, image_url)
|
| 233 |
+
# Create the tar header
|
| 234 |
+
image_name = f"{post_id}.png"
|
| 235 |
+
tarinfo = tarfile.TarInfo(name=image_name)
|
| 236 |
+
tarinfo.size = len(image_data)
|
| 237 |
+
tarinfo.mtime = int(time.time())
|
| 238 |
+
tarinfo.mode = 0o644
|
| 239 |
+
tarinfo.uid = 0
|
| 240 |
+
tarinfo.gid = 0
|
| 241 |
+
header = tarinfo.tobuf()
|
| 242 |
+
# Yield the header
|
| 243 |
+
yield header
|
| 244 |
+
# Yield the image data
|
| 245 |
+
yield image_data
|
| 246 |
+
# Pad the image data to a multiple of 512 bytes
|
| 247 |
+
remainder = len(image_data) % 512
|
| 248 |
+
if remainder > 0:
|
| 249 |
+
padding = b'\0' * (512 - remainder)
|
| 250 |
+
yield padding
|
| 251 |
+
except Exception as e:
|
| 252 |
+
continue
|
| 253 |
+
# At the end, yield two 512-byte blocks of zeros
|
| 254 |
+
yield b'\0' * 1024
|
| 255 |
|
| 256 |
@app.post("/download")
|
| 257 |
async def download(
|
|
|
|
| 259 |
):
|
| 260 |
posts = items.posts
|
| 261 |
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
|
| 262 |
+
tar_generator = tar_stream_generator(posts, session)
|
| 263 |
|
| 264 |
+
return StreamingResponse(
|
| 265 |
+
tar_generator,
|
| 266 |
+
media_type="application/x-tar",
|
| 267 |
+
headers={"Content-Disposition": f"attachment; filename={base26_time()}.tar"}
|
| 268 |
+
)
|
| 269 |
|
| 270 |
@app.get("/")
|
| 271 |
async def read_root():
|