47 lines
1.3 KiB
Python
Executable File
47 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3.9
|
|
import argparse
|
|
import concurrent.futures
|
|
import os
|
|
import sys
|
|
import typing
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
import httpx
|
|
|
|
|
|
IMGBB_TOKEN = os.getenv("IMGBB_TOKEN", "391f0559cc209b11f37eda0084bb3281")
|
|
client = httpx.Client(base_url="https://api.imgbb.com/1/", params={"key": IMGBB_TOKEN})
|
|
|
|
|
|
def upload_image(image_path: Path) -> str:
|
|
filename = f"i{image_path.suffix}"
|
|
with image_path.open("rb") as f:
|
|
res = client.post("/upload", files={"image": (filename, f)})
|
|
res.raise_for_status()
|
|
data = res.json()["data"]
|
|
return data["url"]
|
|
|
|
|
|
def parse_args(argv: Optional[typing.Sequence[str]] = None) -> argparse.Namespace:
|
|
arger = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
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=3) as pool:
|
|
results = list(pool.map(upload_image, image_paths))
|
|
for url in results:
|
|
print(url)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|