File size: 11,542 Bytes
ea8db81 4e1b77d ea8db81 4e1b77d da5502e d7d9d98 4e1b77d de8246b da5502e d44e0ff 4e1b77d bd75875 4e1b77d b3b97aa ea8db81 da5502e 4e1b77d da5502e 4e1b77d bd75875 4e1b77d cec3ffe 4e1b77d 947856c 4e1b77d 947856c 4e1b77d 947856c 4e1b77d da5502e 3936122 4e1b77d 3936122 06706ca 947856c 7115789 f213af5 7115789 f213af5 7115789 89c01b2 a3d8f57 ce0eaf7 a3d8f57 ce0eaf7 7115789 ce0eaf7 3936122 a28b012 3936122 4e1b77d 3b91e61 ea8db81 3b91e61 ea8db81 c51b70c 728c581 bd75875 c51b70c bd75875 c51b70c 728c581 c51b70c 96355f7 bd75875 96355f7 7115789 4e1b77d b6a07e0 bd75875 4e1b77d 3936122 4e1b77d 4ac6760 cec3ffe 3936122 4e1b77d ea8db81 3b91e61 ea8db81 3b91e61 ea8db81 28aa06e ea8db81 3b91e61 ea8db81 da5502e 4e1b77d d44e0ff bd75875 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
from fastapi import FastAPI, Query, BackgroundTasks
from fastapi.responses import FileResponse
import aiohttp
import asyncio
import time
import tempfile
import zipfile
import os
from pydantic import BaseModel
from typing import List, Dict, Optional
img_base = 'https://i.pximg.net/img-original/img/'
COMMENTS_ROOTS_URL = "https://www.pixiv.net/ajax/illusts/comments/roots"
COMMENTS_REPLIES_URL = "https://www.pixiv.net/ajax/illusts/comments/replies"
COMMENTS_LIMIT = 30
class pixifModel(BaseModel):
post_ids: List[int]
phpsessid: str
class PixifDownloadModel(BaseModel):
posts: Dict[str, str]
class PixifZipModel(BaseModel):
d: Dict[str, str]
class PixifCommentsModel(BaseModel):
post_ids: List[int]
phpsessid: str
limit: int = COMMENTS_LIMIT
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0",
'referer': 'https://www.pixiv.net/',
}
app = FastAPI()
async def fetch_page(session, url, params=None, extra_headers=None, semaphore: Optional[asyncio.Semaphore] = None):
if semaphore:
async with semaphore:
async with session.get(url, params=params, headers=extra_headers) as response:
data = await response.json()
return data
async with session.get(url, params=params, headers=extra_headers) as response:
data = await response.json()
return data
def build_cookies(phpsessid: str) -> Dict[str, str]:
return {"PHPSESSID": phpsessid}
def base26_time():
x=''
n=int(time.time()*100)
while n:
x=chr(97+n%26)+x
n//=26
return x
def determine_exif_type(metadata):
if metadata is None:
return None
elif metadata == b'TitleAI generated image':
return "novelai"
elif metadata.startswith(b"parameter"):
return "sd"
elif b'{"' in metadata:
return "comfy"
elif b"Dig" in metadata:
return "mj"
elif metadata.startswith(b"SoftwareCelsys"):
return "celsys"
else:
return "photoshop"
async def get_exif(url, session):
start_range = 0
end_range = 512
headers = {
"Referer": "https://www.pixiv.net/",
"Range": f"bytes={start_range}-{end_range}"
}
async with session.get(url, headers=headers) as response:
data = await response.read()
return parse_png_metadata(data)
def parse_png_metadata(data):
index = 8
while index < len(data):
if index + 8 > len(data):
break
chunk_len = int.from_bytes(data[index:index+4], 'big')
chunk_type = data[index+4:index+8].decode('ascii')
index += 8
if chunk_type in ['tEXt', 'iTXt']:
content = data[index:index+chunk_len]
if chunk_type == 'tEXt':
return content.replace(b'\0', b'')
elif chunk_type == 'iTXt':
return content.strip()
index += chunk_len + 4
return None
def format_comment_text(comment):
text = comment.get("comment") or ""
if not text and comment.get("stampId"):
text = f"[stamp:{comment['stampId']}]"
return text
async def fetch_comment_replies(comment_id, post_id, session, semaphore):
replies = []
page = 1
referer = {"Referer": f"https://www.pixiv.net/artworks/{post_id}"}
while True:
params = {"comment_id": comment_id, "page": page, "lang": "en"}
payload = await fetch_page(
session,
COMMENTS_REPLIES_URL,
params=params,
extra_headers=referer,
semaphore=semaphore,
)
if payload.get("error"):
message = payload.get("message") or "Unknown error"
raise RuntimeError(f"Pixiv error for reply {comment_id}: {message}")
body = payload.get("body") or {}
comments = body.get("comments") or []
for reply in comments:
replies.append(
{
"name": reply.get("userName") or "",
"Comment": format_comment_text(reply),
}
)
if not body.get("hasNext"):
break
page += 1
return replies
async def fetch_post_comments(post_id, session, limit, semaphore):
offset = 0
results = []
referer = {"Referer": f"https://www.pixiv.net/artworks/{post_id}"}
while True:
params = {"illust_id": post_id, "offset": offset, "limit": limit}
payload = await fetch_page(
session,
COMMENTS_ROOTS_URL,
params=params,
extra_headers=referer,
semaphore=semaphore,
)
if payload.get("error"):
message = payload.get("message") or "Unknown error"
raise RuntimeError(f"Pixiv error for {post_id}: {message}")
body = payload.get("body") or {}
comments = body.get("comments") or []
for comment in comments:
replies = []
if comment.get("hasReplies"):
try:
replies = await fetch_comment_replies(comment.get("id"), post_id, session, semaphore)
except Exception:
replies = []
results.append(
{
"name": comment.get("userName") or "",
"Comment": format_comment_text(comment),
"Replies": replies,
}
)
if not body.get("hasNext"):
break
offset += limit
return str(post_id), results
async def process_post(post_id, session, semaphore):
async with semaphore:
try:
data = await fetch_page(session, f"https://www.pixiv.net/ajax/illust/{post_id}/pages")
image_urls = [page['urls']['original'] for page in data['body'] if 'png' in page['urls']['original']]
initial_offsets = [1, 5, 5, 10, 10, 10]
chunks = []
start = 0
for offset in initial_offsets:
end = start + offset
if end > len(image_urls):
end = len(image_urls)
chunks.append((start, end))
start = end
while start < len(image_urls):
end = min(start + 10, len(image_urls))
chunks.append((start, end))
start = end
for s, e in chunks:
chunk_tasks = [get_exif(image_urls[i], session) for i in range(s, e)]
results = await asyncio.gather(*chunk_tasks)
for image_url, metadata in zip(image_urls[s:e], results):
exif_type = determine_exif_type(metadata)
if exif_type not in ['photoshop', 'celsys', None]:
return post_id, image_url
return post_id, None
except Exception:
return post_id, None
async def fetch_image_bytes(session, url, post_id, semaphore):
async with semaphore:
async with session.get(url) as response:
return post_id, await response.read()
@app.get("/allimages")
async def all_images(
only_first = Query("0", description="Only fetch the first image of each post."),
post_ids: List[int] = Query(..., alias='post_ids'),
phpsessid: str = Query(..., description="Pixiv PHPSESSID value.")
):
cookies = build_cookies(phpsessid)
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
tasks = []
for pid in post_ids:
tasks.append(fetch_page(session, f"https://www.pixiv.net/ajax/illust/{pid}/pages"))
results = await asyncio.gather(*tasks)
all_image_urls = []
for data in results:
if "body" in data:
for page in data["body"]:
if "urls" in page and "original" in page["urls"]:
all_image_urls.append(page["urls"]["original"])
if only_first != "0":
break
return {"image_urls": all_image_urls}
@app.get("/allimage")
async def all_image(
post_id: int = Query(..., description="The post ID to fetch all images from."),
phpsessid: str = Query(..., description="Pixiv PHPSESSID value.")
):
cookies = build_cookies(phpsessid)
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
try:
data = await fetch_page(session, f"https://www.pixiv.net/ajax/illust/{post_id}/pages")
image_urls = [page['urls']['original'] for page in data['body']]
return {"post_id": post_id, "image_urls": image_urls}
except Exception as e:
return {"error": str(e)}
@app.post("/pixif")
async def pixif(
items: pixifModel
):
post_ids = items.post_ids
semaphore = asyncio.Semaphore(100)
cookies = build_cookies(items.phpsessid)
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
tasks = [process_post(post_id, session, semaphore) for post_id in post_ids]
results = await asyncio.gather(*tasks)
image_exifs = {post_id: image_url.replace(img_base, '', 1) for post_id, image_url in results if image_url}
return image_exifs
@app.post("/pixif_zip")
async def pixif_zip(items: PixifZipModel, background_tasks: BackgroundTasks):
downloads = items.d
if not downloads:
return {"detail": "No downloads requested."}
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
tmp_path = tmp_file.name
tmp_file.close()
connector = aiohttp.TCPConnector(limit=20)
semaphore = asyncio.Semaphore(20)
async with aiohttp.ClientSession(headers=headers, connector=connector) as session:
tasks = []
for post_id, url in downloads.items():
full_url = url if url.startswith("http") else img_base + url
tasks.append(asyncio.create_task(fetch_image_bytes(session, full_url, post_id, semaphore)))
with zipfile.ZipFile(tmp_path, "w", compression=zipfile.ZIP_STORED) as zf:
for task in asyncio.as_completed(tasks):
try:
post_id, data = await task
except Exception:
continue
if data:
zf.writestr(f"{post_id}.png", data)
background_tasks.add_task(os.remove, tmp_path)
filename = f"pixif_{base26_time()}.zip"
return FileResponse(tmp_path, media_type="application/zip", filename=filename)
@app.post("/comments")
async def comments(items: PixifCommentsModel):
if not items.post_ids:
return {"comments": {}}
cookies = build_cookies(items.phpsessid)
semaphore = asyncio.Semaphore(10)
async with aiohttp.ClientSession(cookies=cookies, headers=headers) as session:
tasks = [
fetch_post_comments(post_id, session, items.limit, semaphore)
for post_id in items.post_ids
]
results = await asyncio.gather(*tasks, return_exceptions=True)
comments_by_post = {}
for post_id, result in zip(items.post_ids, results):
if isinstance(result, Exception):
comments_by_post[str(post_id)] = []
else:
key, comments = result
comments_by_post[str(key)] = comments
return {"comments": comments_by_post}
@app.get("/")
async def read_root():
return {"message": "Hello, World!"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=7860)
|