mirror of
https://forgejo.altau.su/lego/lego-monitoring.git
synced 2026-03-10 04:41:10 +00:00
creds file -> config file
This commit is contained in:
parent
c653055134
commit
f199292aba
5 changed files with 70 additions and 49 deletions
|
|
@ -1,41 +1,45 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import asyncio
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
|
||||
from common import CREDS_FILE, DEVICE_NAME, HOMESERVER, USER_ID
|
||||
from common import CONFIG_FILE
|
||||
from nio import AsyncClient, LoginResponse
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
if os.path.exists(CREDS_FILE):
|
||||
print(f"Creds already configured in {CREDS_FILE}")
|
||||
raise SystemExit
|
||||
try:
|
||||
with open(CONFIG_FILE) as f:
|
||||
cfg = json.load(f)
|
||||
if "matrix" in cfg:
|
||||
print(f"Creds already configured in {CONFIG_FILE}")
|
||||
raise SystemExit
|
||||
cfg["matrix"] = {}
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
open(CONFIG_FILE, "w").close()
|
||||
os.chmod(CONFIG_FILE, 0o600)
|
||||
cfg = {"matrix": {}}
|
||||
|
||||
client = AsyncClient(HOMESERVER, USER_ID)
|
||||
homeserver = input("Homeserver: ")
|
||||
user_id = input("User ID: ")
|
||||
device_name = input("Device name: ")
|
||||
room_id = input("Room ID: ")
|
||||
password = getpass.getpass()
|
||||
resp = await client.login(password, device_name=DEVICE_NAME)
|
||||
|
||||
client = AsyncClient(homeserver, user_id)
|
||||
resp = await client.login(password, device_name=device_name)
|
||||
await client.close()
|
||||
|
||||
if isinstance(resp, LoginResponse):
|
||||
open(CREDS_FILE, "w").close()
|
||||
os.chmod(CREDS_FILE, stat.S_IRUSR | stat.S_IWUSR)
|
||||
with open(CREDS_FILE, "w") as f:
|
||||
json.dump(
|
||||
{
|
||||
"homeserver": HOMESERVER,
|
||||
"user_id": resp.user_id,
|
||||
"device_id": resp.device_id,
|
||||
"access_token": resp.access_token,
|
||||
},
|
||||
f,
|
||||
)
|
||||
print(f"Logged in as {resp.user_id}. Credentials saved to {CREDS_FILE}")
|
||||
cfg["matrix"]["homeserver"] = homeserver
|
||||
cfg["matrix"]["user_id"] = resp.user_id
|
||||
cfg["matrix"]["device_id"] = resp.device_id
|
||||
cfg["matrix"]["access_token"] = resp.access_token
|
||||
cfg["matrix"]["room_id"] = room_id
|
||||
|
||||
with open(CONFIG_FILE, "w") as f:
|
||||
json.dump(cfg, f, indent=2)
|
||||
print(f"Logged in as {resp.user_id}. Credentials saved to {CONFIG_FILE}")
|
||||
else:
|
||||
raise Exception(f"Failed to log in: {resp}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue