p
File size: 1,666 Bytes
02dda34
 
 
 
 
 
 
 
 
 
 
 
 
 
bd75875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02dda34
 
 
bd75875
02dda34
bd75875
02dda34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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())