add secret= function
This commit is contained in:
parent
29a5b43ad1
commit
3eead1a769
54
oxo.py
54
oxo.py
|
@ -44,17 +44,18 @@ def post_to(
|
|||
data: t.Iterable[t.Dict],
|
||||
expires: t.Optional[int],
|
||||
token_dir: t.Optional[Path],
|
||||
use_long_name: bool,
|
||||
) -> str:
|
||||
retval = []
|
||||
with httpx.Client() as client:
|
||||
data_dict = {}
|
||||
if expires:
|
||||
data_dict["expires"] = expires
|
||||
if use_long_name:
|
||||
data_dict["secret"] = use_long_name
|
||||
for d in data:
|
||||
if isinstance(d.get("file"), io.BufferedReader):
|
||||
if expires:
|
||||
res = client.post(
|
||||
base_url, files=d, data={"expires": expires, "secret": ""}
|
||||
)
|
||||
else:
|
||||
res = client.post(base_url, files=d)
|
||||
res = client.post(base_url, files=d, data=data_dict)
|
||||
token = res.headers.get("x-token")
|
||||
remote_url = res.text.strip()
|
||||
if token:
|
||||
|
@ -72,7 +73,7 @@ def post_to(
|
|||
json.dumps(asdict(token_data))
|
||||
)
|
||||
else:
|
||||
res = client.post(base_url, data=d)
|
||||
res = client.post(base_url, data={**d, **data_dict})
|
||||
remote_url = res.text.strip()
|
||||
res.raise_for_status()
|
||||
retval.append(remote_url)
|
||||
|
@ -85,12 +86,14 @@ def post_files(
|
|||
*,
|
||||
expires: t.Optional[int],
|
||||
token_dir: t.Optional[Path],
|
||||
use_long_name: bool,
|
||||
):
|
||||
return post_to(
|
||||
base_url,
|
||||
data=({"file": f.open("rb")} for f in files),
|
||||
expires=expires,
|
||||
token_dir=token_dir,
|
||||
use_long_name=use_long_name,
|
||||
)
|
||||
|
||||
|
||||
|
@ -100,12 +103,14 @@ def post_repost(
|
|||
*,
|
||||
expires: t.Optional[int],
|
||||
token_dir: t.Optional[Path],
|
||||
use_long_name: bool,
|
||||
):
|
||||
return post_to(
|
||||
base_url,
|
||||
data=({"url": u.strip()} for u in urls),
|
||||
expires=expires,
|
||||
token_dir=token_dir,
|
||||
use_long_name=use_long_name,
|
||||
)
|
||||
|
||||
|
||||
|
@ -121,6 +126,7 @@ def post_shorten(
|
|||
data=({"shorten": u.strip()} for u in urls),
|
||||
expires=expires,
|
||||
token_dir=token_dir,
|
||||
use_long_name=False,
|
||||
)
|
||||
|
||||
|
||||
|
@ -152,6 +158,9 @@ def files(
|
|||
files: t.List[Path] = typer.Argument(
|
||||
..., min=1, exists=True, file_okay=True, dir_okay=False, resolve_path=True
|
||||
),
|
||||
use_long_name: bool = typer.Option(
|
||||
False, help="If true, create a harder to guess name."
|
||||
),
|
||||
base_url: str = BASE_URL,
|
||||
expires: t.Optional[int] = typer.Option(
|
||||
None, help="Expiration time, in hours or epoch milliseconds"
|
||||
|
@ -179,12 +188,23 @@ def files(
|
|||
token_dir.mkdir(exist_ok=True, parents=True)
|
||||
else:
|
||||
token_dir = None
|
||||
typer.echo(post_files(base_url, files, expires=expires, token_dir=token_dir))
|
||||
typer.echo(
|
||||
post_files(
|
||||
base_url,
|
||||
files,
|
||||
expires=expires,
|
||||
token_dir=token_dir,
|
||||
use_long_name=use_long_name,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.command()
|
||||
def repost(
|
||||
urls: t.List[str] = typer.Argument(..., min=1),
|
||||
use_long_name: bool = typer.Option(
|
||||
False, help="If true, create a harder to guess name."
|
||||
),
|
||||
base_url=BASE_URL,
|
||||
expires: t.Optional[int] = typer.Option(
|
||||
None, help="Expiration time, in hours or epoch milliseconds"
|
||||
|
@ -192,7 +212,15 @@ def repost(
|
|||
token_cache_dir: str = TOKEN_CACHE_DIR,
|
||||
):
|
||||
"""Repost one or more urls."""
|
||||
typer.echo(post_repost(base_url, urls, expires, token_cache_dir))
|
||||
typer.echo(
|
||||
post_repost(
|
||||
base_url,
|
||||
urls,
|
||||
expires=expires,
|
||||
token_dir=token_cache_dir,
|
||||
use_long_name=use_long_name,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.command()
|
||||
|
@ -234,12 +262,12 @@ def delete(
|
|||
token_files = token_cache_dir.glob("*.token")
|
||||
tokens = []
|
||||
good_tokens = []
|
||||
for t in token_files:
|
||||
for token in token_files:
|
||||
try:
|
||||
tokens.append(TokenData(**json.loads(t.read_text())))
|
||||
good_tokens.append(t)
|
||||
tokens.append(TokenData(**json.loads(token.read_text())))
|
||||
good_tokens.append(token)
|
||||
except Exception as e:
|
||||
typer.secho(f"Ignoring {t}: {e}", err=True, fg=typer.colors.YELLOW)
|
||||
typer.secho(f"Ignoring {token}: {e}", err=True, fg=typer.colors.YELLOW)
|
||||
with httpx.Client() as client:
|
||||
for token, token_file in zip(tokens, good_tokens):
|
||||
try:
|
||||
|
|
|
@ -14,7 +14,7 @@ dependencies = ["typer[all]",
|
|||
'importlib-metadata; python_version<"3.8"']
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = ["pre-commit","pytest", "ipython"]
|
||||
dev = ["pre-commit","pytest", "ipython", "ruff", "pudb"]
|
||||
|
||||
[project.scripts]
|
||||
oxo = "oxo:app"
|
||||
|
|
Loading…
Reference in New Issue