feat: add account selection support in web UI

Add support for multiple MyIce accounts in the web interface:

- Create /accounts endpoint to fetch available accounts from config files

- Update index.html to dynamically show available accounts

- Modify /schedule and /game endpoints to accept account parameter

- Store selected account in localStorage for persistence
This commit is contained in:
2025-08-18 22:10:24 +02:00
parent 6eb9598012
commit 66b93af6b1
2 changed files with 110 additions and 11 deletions
+57 -6
View File
@@ -74,35 +74,86 @@ async def favico():
@app.get("/schedule")
async def schedule(
headers: Annotated[AuthHeaders, Header()],
account: str = "default",
):
if not headers.authorized():
raise HTTPException(401, detail="get out")
username, password, userid, existing_token, club_id = myice.get_login()
username, password, userid, existing_token, club_id = myice.get_login(
config_section=account
)
if existing_token:
myice.userdata = {
"id": userid,
"id_club": 186,
"id_club": club_id or 186,
"token": existing_token,
}
else:
myice.userdata = myice.mobile_login()
myice.userdata = myice.mobile_login(config_section=account)
return myice.refresh_data()["club_games"]
@app.get("/accounts")
async def accounts(
headers: Annotated[AuthHeaders, Header()],
):
if not headers.authorized():
raise HTTPException(401, detail="get out")
# Import configparser to read the available sections
import configparser
from pathlib import Path
config = configparser.ConfigParser()
config_files = [
Path("~/.config/myice.ini").expanduser(),
Path("myice.ini"),
]
# Read all config files
for config_file in config_files:
if config_file.exists():
try:
config.read(config_file, encoding="utf-8")
except Exception:
# Try without specifying encoding
config.read(config_file)
# Get all sections (accounts) from the config
accounts = []
for section in config.sections():
if section != "DEFAULT": # Skip DEFAULT section
# Capitalize first letter for display
label = (
section[0].upper() + section[1:]
if len(section) > 1
else section.upper()
)
accounts.append({"name": section, "label": label})
# If no accounts found, return default
if not accounts:
accounts = [{"name": "default", "label": "Default"}]
return accounts
@app.get("/game/{game_id}")
async def game(
headers: Annotated[AuthHeaders, Header()],
game_id: int,
account: str = "default",
):
username, password, userid, existing_token, club_id = myice.get_login()
username, password, userid, existing_token, club_id = myice.get_login(
config_section=account
)
if existing_token:
myice.userdata = {
"id": userid,
"id_club": 186,
"id_club": club_id or 186,
"token": existing_token,
}
else:
myice.userdata = myice.mobile_login()
myice.userdata = myice.mobile_login(config_section=account)
# data = refresh_data()
with requests.post(