p / Client /Scripts /run_pixif_single.py
q6's picture
Move scripts to /scripts
3be9052
import asyncio
import os
import sys
import aiohttp
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
from API import app as pixif_app # noqa: E402
def read_dotenv_value(path, key):
try:
with open(path, "r") as env_file:
for line in env_file:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
k, v = line.split("=", 1)
if k == key:
return v
except FileNotFoundError:
return None
return None
def get_phpsessid():
phpsessid = os.getenv("PHPSESSID")
if phpsessid:
return phpsessid
env_path = os.path.join(PROJECT_ROOT, ".env")
phpsessid = read_dotenv_value(env_path, "PHPSESSID")
if phpsessid:
return phpsessid
raise RuntimeError("PHPSESSID is not set in the environment or .env")
async def run_local() -> dict:
post_id = 138954885
semaphore = asyncio.Semaphore(8)
cookies = pixif_app.build_cookies(get_phpsessid())
async with aiohttp.ClientSession(cookies=cookies, headers=pixif_app.headers) as session:
_, image_url = await pixif_app.process_post(post_id, session, semaphore)
if not image_url:
return {}
return {post_id: image_url.replace(pixif_app.img_base, "", 1)}
def main() -> int:
try:
data = asyncio.run(run_local())
except Exception as exc:
print(f"Run failed: {exc}")
return 1
print(data)
return 0
if __name__ == "__main__":
sys.exit(main())