From d1633d804064ff15e6eb9daa80101c592c4eda7f Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sat, 14 Dec 2024 16:30:24 +1100 Subject: [PATCH] Sketch of command-line registration script --- pyproject.toml | 3 +++ src/gotosocial/register.py | 44 ++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0cb5a17..96a40d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,9 @@ dependencies = [ "requests>=2.32.3", ] +[project.scripts] +register = "gotosocial.register:cli" + [build-system] requires = ["hatchling"] build-backend = "hatchling.build" diff --git a/src/gotosocial/register.py b/src/gotosocial/register.py index 620cfc8..9139ff0 100644 --- a/src/gotosocial/register.py +++ b/src/gotosocial/register.py @@ -1,6 +1,9 @@ # script to register an API import requests +from pathlib import Path +import json +import argparse class Application: @@ -10,6 +13,11 @@ class Application: self.headers = {"Content-Type": "application/json"} def register_app(self): + app_json = Path(f"./{self.name}_app.json") + if Path.isfile(): + print(f"Found {app_json} file") + print(f"Looks like you've already registered {self.name}") + return False response = requests.post( f"{self.base_url}/api/v1/statuses", data={ @@ -19,19 +27,27 @@ class Application: }, headers=self.headers, ) - response.raise_for_status() - json_r = response.json() - self.client_id = json_r["client_id"] - self.client_secret = json_r["client_secret"] - print(f"Registered {self.name} as {self.client_id}") + try: + response.raise_for_status() + json_r = response.json() + self.client_id = json_r["client_id"] + self.client_secret = json_r["client_secret"] + print(f"Registered {self.name}, client ID is {self.client_id}") + with open(app_json, "w") as jfh: + json.dump(json_r, jfh, indent=2) + print(f"Wrote response to {app_json}") + except Exception as e: + print("Something went wrong...") + print(e) -# curl \ -# -X POST \ -# -H 'Content-Type:application/json' \ -# -d '{ -# "client_name": "your_app_name", -# "redirect_uris": "urn:ietf:wg:oauth:2.0:oob", -# "scopes": "read" -# }' \ -# 'https://example.org/api/v1/apps' +def cli(): + ap = argparse.ArgumentParser() + ap.add_argument("-s", "--server", required=True, type=str, help="GoToSocial server") + ap.add_argument("-n", "--name", required=True, type=str, help="Application name") + ap.parse_args() + print(ap) + + +if __name__ == "__main__": + cli()