- API/app.py +28 -33
API/app.py
CHANGED
|
@@ -225,33 +225,28 @@ 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 |
-
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,13 +254,13 @@ async def download(
|
|
| 259 |
):
|
| 260 |
posts = items.posts
|
| 261 |
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
|
| 262 |
-
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
|
| 270 |
@app.get("/")
|
| 271 |
async def read_root():
|
|
|
|
| 225 |
content = await response.read()
|
| 226 |
return (post_id, content)
|
| 227 |
|
| 228 |
+
async def generate_tar(posts, session):
|
| 229 |
+
tar_buffer = io.BytesIO()
|
| 230 |
+
tarf = tarfile.open(mode="w", fileobj=tar_buffer)
|
| 231 |
+
|
| 232 |
+
semaphore = asyncio.Semaphore(100)
|
| 233 |
+
|
| 234 |
+
async def add_to_tar(post_id, image_url):
|
| 235 |
+
async with semaphore:
|
| 236 |
+
url = f"{img_base}{image_url}"
|
| 237 |
+
async with session.get(url) as response:
|
| 238 |
+
image_data = await response.read()
|
| 239 |
+
image_name = f"{post_id}.png"
|
| 240 |
+
info = tarfile.TarInfo(name=image_name)
|
| 241 |
+
info.size = len(image_data)
|
| 242 |
+
tarf.addfile(tarinfo=info, fileobj=io.BytesIO(image_data))
|
| 243 |
+
|
| 244 |
+
tasks = [add_to_tar(post_id, image_url) for post_id, image_url in posts.items()]
|
| 245 |
+
await asyncio.gather(*tasks)
|
| 246 |
+
|
| 247 |
+
tarf.close()
|
| 248 |
+
tar_buffer.seek(0)
|
| 249 |
+
return tar_buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
@app.post("/download")
|
| 252 |
async def download(
|
|
|
|
| 254 |
):
|
| 255 |
posts = items.posts
|
| 256 |
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
|
| 257 |
+
tar_buffer = await generate_tar(posts, session)
|
| 258 |
|
| 259 |
+
return StreamingResponse(
|
| 260 |
+
tar_buffer,
|
| 261 |
+
media_type="application/x-tar",
|
| 262 |
+
headers={"Content-Disposition": f"attachment; filename={base26_time()}.tar"}
|
| 263 |
+
)
|
| 264 |
|
| 265 |
@app.get("/")
|
| 266 |
async def read_root():
|