diff options
| author | Tulir Asokan <tulir@maunium.net> | 2020-09-05 14:43:07 +0300 |
|---|---|---|
| committer | Tulir Asokan <tulir@maunium.net> | 2020-09-05 14:43:07 +0300 |
| commit | 4190057b3cbcc8e2a5e12b0c886614c160a53ff4 (patch) | |
| tree | 96bfbca72b9b4195b6a4a1ddc5265110e25455fd /import.py | |
| parent | d698f058b51597a419910cb70b72495f8328c817 (diff) | |
Ask for Matrix access token on first run
Diffstat (limited to 'import.py')
| -rw-r--r-- | import.py | 35 |
1 files changed, 30 insertions, 5 deletions
@@ -31,11 +31,36 @@ parser.add_argument("--output-dir", help="Directory to write packs to", default= parser.add_argument("pack", help="Sticker pack URLs to import", action="append", nargs="*") args = parser.parse_args() -with open(args.config) as config_file: - config = json.load(config_file) - homeserver_url = config["homeserver"] - upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload" - access_token = config["access_token"] + +async def whoami(url: URL, access_token: str) -> str: + headers = {"Authorization": f"Bearer {access_token}"} + async with ClientSession() as sess, sess.get(url, headers=headers) as resp: + resp.raise_for_status() + user_id = (await resp.json())["user_id"] + print(f"Access token validated (user ID: {user_id})") + return user_id + + +try: + with open(args.config) as config_file: + config = json.load(config_file) + homeserver_url = config["homeserver"] + access_token = config["access_token"] +except FileNotFoundError: + print("Matrix config file not found. Please enter your homeserver and access token.") + homeserver_url = input("Homeserver URL: ") + access_token = input("Access token: ") + whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami" + user_id = asyncio.run(whoami(whoami_url, access_token)) + with open(args.config, "w") as config_file: + json.dump({ + "homeserver": homeserver_url, + "user_id": user_id, + "access_token": access_token + }, config_file) + print(f"Wrote config to {args.config}") + +upload_url = URL(homeserver_url) / "_matrix" / "media" / "r0" / "upload" async def upload(data: bytes, mimetype: str, filename: str) -> str: |
