q6 commited on
Commit
272b76f
·
1 Parent(s): d521dd1
Files changed (2) hide show
  1. API/app.py +13 -20
  2. Client/Extract Pixiv/user.py +5 -0
API/app.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, Query
2
- from fastapi.responses import StreamingResponse
3
  import aiohttp
4
  import requests
5
  import asyncio
@@ -219,37 +219,30 @@ async def pixif(
219
  image_exifs = {post_id: image_url.replace('https://i.pximg.net/img-original/', '', 1) for post_id, image_url in results if image_url}
220
  return image_exifs
221
 
222
- async def download_image(session, post_id, post_url, semaphore):
223
- async with semaphore:
224
- url = f"{img_base}{post_url}"
225
- async with session.get(url) as response:
226
- content = await response.read()
227
- return (post_id, content)
228
 
229
  @app.post("/download")
230
  async def download(
231
  items: PixifDownloadModel
232
  ):
233
  posts = items.posts
234
- semaphore = asyncio.Semaphore(100) # Adjust the number as needed
235
-
236
  async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
237
- tasks = [download_image(session, post_id, image_url, semaphore) for post_id, image_url in posts.items()]
238
  images = await asyncio.gather(*tasks)
239
 
240
- zip_buffer = io.BytesIO()
241
- with ZipFile(zip_buffer, "w") as zipf:
242
  for post_id, image_data in images:
243
  zipf.writestr(f"{post_id}.png", image_data)
244
- zip_buffer.seek(0)
245
- filename = f"{base26_time()}.zip"
246
- headers = {
247
- 'Content-Disposition': f'attachment; filename="{filename}"'
248
- }
249
- return StreamingResponse(
250
- zip_buffer,
251
  media_type="application/zip",
252
- headers=headers
253
  )
254
 
255
  @app.get("/")
 
1
  from fastapi import FastAPI, Query
2
+ from fastapi.responses import FileResponse
3
  import aiohttp
4
  import requests
5
  import asyncio
 
219
  image_exifs = {post_id: image_url.replace('https://i.pximg.net/img-original/', '', 1) for post_id, image_url in results if image_url}
220
  return image_exifs
221
 
222
+ async def download_image(session, post_id, post_url):
223
+ url = f"{img_base}{post_url}"
224
+ async with session.get(url) as response:
225
+ content = await response.read()
226
+ return (post_id, content)
 
227
 
228
  @app.post("/download")
229
  async def download(
230
  items: PixifDownloadModel
231
  ):
232
  posts = items.posts
233
+
 
234
  async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
235
+ tasks = [download_image(session, post_id, image_url) for post_id, image_url in posts.items()]
236
  images = await asyncio.gather(*tasks)
237
 
238
+ timezip = f"{base26_time()}.zip"
239
+ with ZipFile(timezip, "w", compression=0) as zipf:
240
  for post_id, image_data in images:
241
  zipf.writestr(f"{post_id}.png", image_data)
242
+
243
+ return FileResponse(
244
+ timezip,
 
 
 
 
245
  media_type="application/zip",
 
246
  )
247
 
248
  @app.get("/")
Client/Extract Pixiv/user.py CHANGED
@@ -15,6 +15,11 @@ user_ids = re.findall(r"\d+", inp)
15
 
16
  user_ids = [int(uid) for uid in user_ids]
17
 
 
 
 
 
 
18
  response = requests.get(f'{endpoint}/users', params={'user_ids': user_ids})
19
  response.raise_for_status()
20
  data = response.json()
 
15
 
16
  user_ids = [int(uid) for uid in user_ids]
17
 
18
+ if len(user_ids) == 0:
19
+ import sys
20
+
21
+ sys.exit()
22
+
23
  response = requests.get(f'{endpoint}/users', params={'user_ids': user_ids})
24
  response.raise_for_status()
25
  data = response.json()