dotfiles/roles/base/desktop/files/srht.py
Matej Focko 0283426127
chore: move out the base roles
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-07-12 21:07:20 +02:00

86 lines
1.9 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import click
import requests
def get_username():
response = requests.post(
"https://meta.sr.ht/query",
json={"query": "{ me { canonicalName } }"},
headers={"Authorization": f"Bearer {os.getenv('SRHT')}"},
).json()
return response["data"]["me"]["canonicalName"]
@click.group()
def cli():
pass
@cli.command(help="Prints out canonical name of the authenticated user")
def whoami():
click.echo(get_username())
@cli.command(help="Provides paste to the paste.sr.ht")
@click.option(
"--public",
"visibility",
flag_value="public",
help="Public paste that is visible on the user's profile",
)
@click.option(
"--unlisted",
"visibility",
flag_value="unlisted",
default=True,
help="Unlisted paste that is accessible via a link",
)
@click.option(
"--private",
"visibility",
flag_value="private",
help="Private paste that is accessible only to the authenticated user",
)
@click.argument(
"src",
type=click.File("r"),
nargs=-1,
)
def paste(visibility, src):
request = {
"visibility": visibility,
"files": [
{"filename": s.name.split("/")[-1], "contents": s.read()}
for s in src
],
}
response = requests.post(
"https://paste.sr.ht/api/pastes",
json=request,
headers={"Authorization": f"token {os.getenv('SRHT_LEGACY')}"},
).json()
if "sha" in response:
click.secho("*** Paste link ***", fg="green")
click.secho(
f"https://paste.sr.ht/{response['user']['canonical_name']}/{response['sha']}"
)
click.secho("\n*** Files ***", fg="green")
for file in response["files"]:
filename = file["filename"]
blob_id = file["blob_id"]
click.secho(f"https://paste.sr.ht/blob/{blob_id}\t{filename}")
else:
click.secho(response, fg="red")
if __name__ == "__main__":
cli()