feat: add club selection support and improve mobile API integration
This commit is contained in:
@@ -163,7 +163,9 @@ def save_cookies(file: str = "cookies.txt"):
|
|||||||
f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
|
f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
|
||||||
|
|
||||||
|
|
||||||
def get_login(local_file: str = "myice.ini") -> tuple[str, str, int | None, str | None]:
|
def get_login(
|
||||||
|
local_file: str = "myice.ini",
|
||||||
|
) -> tuple[str, str, int | None, str | None, int | None]:
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(
|
config.read(
|
||||||
[
|
[
|
||||||
@@ -179,6 +181,7 @@ def get_login(local_file: str = "myice.ini") -> tuple[str, str, int | None, str
|
|||||||
password = default_config.get("password")
|
password = default_config.get("password")
|
||||||
userid = default_config.getint("userid")
|
userid = default_config.getint("userid")
|
||||||
token = default_config.get("token")
|
token = default_config.get("token")
|
||||||
|
club_id = default_config.getint("club_id")
|
||||||
if not username or not password:
|
if not username or not password:
|
||||||
print(
|
print(
|
||||||
"Error: please configure username/password in ini file", file=sys.stderr
|
"Error: please configure username/password in ini file", file=sys.stderr
|
||||||
@@ -187,13 +190,22 @@ def get_login(local_file: str = "myice.ini") -> tuple[str, str, int | None, str
|
|||||||
else:
|
else:
|
||||||
print("Error: please configure username/password in ini file", file=sys.stderr)
|
print("Error: please configure username/password in ini file", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return username, password, userid, token
|
return username, password, userid, token, club_id
|
||||||
|
|
||||||
|
|
||||||
|
def select_club(club_id: int = 172):
|
||||||
|
"""Select a club by ID after login."""
|
||||||
|
global session
|
||||||
|
r = session.get(
|
||||||
|
f"https://app.myice.hockey/?cl={club_id}", headers={"User-Agent": user_agent}
|
||||||
|
)
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
def do_login():
|
def do_login():
|
||||||
global session
|
global session
|
||||||
global userid
|
global userid
|
||||||
username, password, userid, token = get_login()
|
username, password, userid, token, club_id = get_login()
|
||||||
r = session.get("https://app.myice.hockey/", headers={"User-Agent": user_agent})
|
r = session.get("https://app.myice.hockey/", headers={"User-Agent": user_agent})
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
form_data = {
|
form_data = {
|
||||||
@@ -213,8 +225,7 @@ def do_login():
|
|||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
# select the club we want
|
# select the club we want
|
||||||
session.get("https://app.myice.hockey/?cl=172", headers={"User-Agent": user_agent})
|
select_club(club_id)
|
||||||
r.raise_for_status()
|
|
||||||
|
|
||||||
|
|
||||||
def get_userid():
|
def get_userid():
|
||||||
@@ -246,7 +257,7 @@ def wrapper_session(func):
|
|||||||
print("login...", file=sys.stderr)
|
print("login...", file=sys.stderr)
|
||||||
do_login()
|
do_login()
|
||||||
save_cookies()
|
save_cookies()
|
||||||
_, _, userid, _ = get_login()
|
_, _, userid, _, _ = get_login()
|
||||||
if not userid:
|
if not userid:
|
||||||
print("get userid...", file=sys.stderr)
|
print("get userid...", file=sys.stderr)
|
||||||
userid = get_userid()
|
userid = get_userid()
|
||||||
@@ -515,7 +526,11 @@ mobile_headers = {
|
|||||||
def mobile_login():
|
def mobile_login():
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
username, password, userid, token = get_login()
|
username, password, _, token, club_id = get_login()
|
||||||
|
if token and club_id:
|
||||||
|
return {"id": 0, "token": token, "id_club": club_id}
|
||||||
|
|
||||||
|
print("Requesting token", file=sys.stderr)
|
||||||
with requests.post(
|
with requests.post(
|
||||||
"https://app.myice.hockey/api/mobilerest/login",
|
"https://app.myice.hockey/api/mobilerest/login",
|
||||||
headers=mobile_headers,
|
headers=mobile_headers,
|
||||||
@@ -524,9 +539,10 @@ def mobile_login():
|
|||||||
) as r:
|
) as r:
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return {
|
return {
|
||||||
"id": r.json()["userid"],
|
"id": 0,
|
||||||
"token": r.json()["userinfo"]["token"],
|
"token": r.json()["userinfo"]["token"],
|
||||||
"id_club": r.json()["userinfo"]["id_club"],
|
"id_club": club_id
|
||||||
|
or r.json()["userinfo"]["id_club"], # Use configured club_id if available
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -541,54 +557,35 @@ def refresh_data():
|
|||||||
with requests.post(
|
with requests.post(
|
||||||
"https://app.myice.hockey/api/mobilerest/refreshdata",
|
"https://app.myice.hockey/api/mobilerest/refreshdata",
|
||||||
headers=mobile_headers,
|
headers=mobile_headers,
|
||||||
data=f"token={userdata['token']}&id_club=186&language=FR",
|
data=f"token={userdata['token']}&id_club={userdata['id_club']}&language=FR",
|
||||||
# verify=False,
|
verify=False,
|
||||||
) as r:
|
) as r:
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.json()
|
return r.json()
|
||||||
|
|
||||||
|
|
||||||
@app.command("mobile")
|
@app.command("mobile-login")
|
||||||
def mobile(
|
def do_mobile_login():
|
||||||
token: Annotated[str, typer.Option(envvar="MYICE_TOKEN")] = "",
|
|
||||||
id_club: Annotated[int, typer.Option(envvar="MYICE_CLUB")] = 0,
|
|
||||||
):
|
|
||||||
global userdata
|
global userdata
|
||||||
if not token:
|
userdata = mobile_login()
|
||||||
userdata = mobile_login()
|
print(json.dumps(userdata, indent=2))
|
||||||
else:
|
|
||||||
userdata = {
|
|
||||||
"id": 0,
|
@app.command("mobile")
|
||||||
"id_club": id_club,
|
def mobile():
|
||||||
"token": token,
|
global userdata
|
||||||
}
|
userdata = mobile_login()
|
||||||
print(json.dumps(refresh_data(), indent=2))
|
games = [x for x in refresh_data().get("club_games")]
|
||||||
|
print(json.dumps(games, indent=2))
|
||||||
|
|
||||||
|
|
||||||
@app.command("mobile-game")
|
@app.command("mobile-game")
|
||||||
def mobile_game(
|
def mobile_game(
|
||||||
game_id: Annotated[int, typer.Argument(help="game id")],
|
game_id: Annotated[int, typer.Argument(help="game id")],
|
||||||
token: Annotated[str, typer.Option(envvar="MYICE_TOKEN")] = "",
|
|
||||||
id_club: Annotated[int, typer.Option(envvar="MYICE_CLUB")] = 0,
|
|
||||||
raw: Annotated[bool, typer.Option(help="display raw output")] = False,
|
raw: Annotated[bool, typer.Option(help="display raw output")] = False,
|
||||||
):
|
):
|
||||||
global userdata
|
global userdata
|
||||||
username, password, userid, existing_token = get_login()
|
userdata = mobile_login()
|
||||||
if token:
|
|
||||||
userdata = {
|
|
||||||
"id": 0,
|
|
||||||
"id_club": 186,
|
|
||||||
"token": token,
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
if existing_token:
|
|
||||||
userdata = {
|
|
||||||
"id": userid,
|
|
||||||
"id_club": 186,
|
|
||||||
"token": existing_token,
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
userdata = mobile_login()
|
|
||||||
|
|
||||||
# data = refresh_data()
|
# data = refresh_data()
|
||||||
with requests.post(
|
with requests.post(
|
||||||
@@ -604,7 +601,7 @@ def mobile_game(
|
|||||||
"language=FR",
|
"language=FR",
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
# verify=False,
|
verify=False,
|
||||||
) as r:
|
) as r:
|
||||||
data = r.json()["eventData"]
|
data = r.json()["eventData"]
|
||||||
players = data["convocation"]["available"]
|
players = data["convocation"]["available"]
|
||||||
|
|||||||
Reference in New Issue
Block a user