69 lines
1.8 KiB
Python
Executable File
69 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env -S uv run
|
|
# /// script
|
|
# dependencies = ["httpx"]
|
|
# ///
|
|
import argparse
|
|
import concurrent.futures
|
|
import json
|
|
import re
|
|
import sys
|
|
import typing
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
import logging
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
def upload_image(image_path: Path) -> dict:
|
|
session = httpx.Client(timeout=20)
|
|
logging.info(f"uploading {image_path.name}")
|
|
|
|
with image_path.open("rb") as f:
|
|
res = session.post(
|
|
"https://fapping.empornium.sx/upload.php",
|
|
files={
|
|
"ImageUp": f,
|
|
},
|
|
)
|
|
|
|
res = session.get("https://fapping.empornium.sx/uploaded/", follow_redirects=True)
|
|
if m := re.search(r"var ImagesUp = (\[.+\]);", res.text, re.MULTILINE):
|
|
data: list[dict] = json.loads(m.group(1))
|
|
logging.info(f"upload result on the page={data}")
|
|
|
|
return dict(
|
|
image_url=data[0]["image_url"],
|
|
thumbnail_url=data[0]["image_thumb_url"],
|
|
)
|
|
|
|
logging.error(f"failed to find upload result on the page. html={res.text}")
|
|
|
|
raise ValueError(f"failed to parse upload result for {image_path.name}")
|
|
|
|
|
|
def parse_args(argv: Optional[typing.Sequence[str]] = None) -> argparse.Namespace:
|
|
arger = argparse.ArgumentParser()
|
|
arger.add_argument("image_paths", nargs="+", action="store", type=Path, help="Path to image to upload")
|
|
if not argv or len(argv) == 0:
|
|
arger.print_help()
|
|
sys.exit(1)
|
|
|
|
return arger.parse_args(argv)
|
|
|
|
|
|
def main():
|
|
args = parse_args(sys.argv[1:])
|
|
image_paths = args.image_paths
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
|
|
results = list(pool.map(upload_image, image_paths))
|
|
for it in results:
|
|
print(it["image_url"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO)
|
|
main()
|