From 6de4f5f9d8d0a40f724d7e5d5103bf05357c1b28 Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Mon, 13 Jan 2025 09:06:39 +0100 Subject: [PATCH] feat: add mobile methods --- myice/myice.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/myice/myice.py b/myice/myice.py index 07d4655..7fada78 100755 --- a/myice/myice.py +++ b/myice/myice.py @@ -17,6 +17,7 @@ from typing import List, Tuple import PyPDF2 import requests import typer +from rich import print from rl_ai_tools import utils # type: ignore user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:131.0) Gecko/20100101 Firefox/131.0" @@ -56,7 +57,7 @@ def save_cookies(file: str = "cookies.txt"): f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) -def get_login(local_file: str = "myice.ini") -> tuple[str, str, int]: +def get_login(local_file: str = "myice.ini") -> tuple[str, str, int, str]: config = configparser.ConfigParser() config.read( [ @@ -71,6 +72,7 @@ def get_login(local_file: str = "myice.ini") -> tuple[str, str, int]: username = default_config.get("username") password = default_config.get("password") userid = default_config.getint("userid") + token = default_config.get("token") if not username or not password: print( "Error: please configure username/password in ini file", file=sys.stderr @@ -79,13 +81,13 @@ def get_login(local_file: str = "myice.ini") -> tuple[str, str, int]: else: print("Error: please configure username/password in ini file", file=sys.stderr) sys.exit(1) - return username, password, userid + return username, password, userid, token def do_login(): global session global userid - username, password, userid = get_login() + username, password, userid, token = get_login() r = session.get("https://app.myice.hockey/", headers={"User-Agent": user_agent}) r.raise_for_status() form_data = { @@ -128,13 +130,14 @@ def wrapper_session(func): def wrapper(*args, **kwargs): global session, userid session = requests.Session() + # session.verify = False session.cookies = load_cookies() session.cookies.clear_expired_cookies() if not session.cookies.get("mih_v3_cookname"): print("login...", file=sys.stderr) do_login() save_cookies() - _, _, userid = get_login() + _, _, userid, _ = get_login() if not userid: print("get userid...", file=sys.stderr) userid = get_userid() @@ -372,5 +375,131 @@ def check_with_ai( history.append((question, answer)) +mobile_headers = { + "Accept": "application/json, text/plain, */*", + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", + "Sec-Fetch-Site": "cross-site", + "Accept-Language": "fr-FR,fr;q=0.9", + "Sec-Fetch-Mode": "cors", + "Origin": "null", + "Sec-Fetch-Dest": "empty", + "Content-Type": "application/x-www-form-urlencoded", +} + + +def mobile_login(): + import base64 + + username, password, userid, token = get_login() + with requests.post( + "https://app.myice.hockey/api/mobilerest/login", + headers=mobile_headers, + data=f"login_email={base64.b64encode(username.encode()).decode()}&login_password={base64.b64encode(password.encode()).decode()}&language=FR&v=2", + # verify=False, + ) as r: + r.raise_for_status() + return { + "id": r.json()["userid"], + "token": r.json()["userinfo"]["token"], + "id_club": r.json()["userinfo"]["id_club"], + } + + +userdata = { + "id": 0, + "id_club": 0, + "token": "", +} + + +def refresh_data(): + with requests.post( + "https://app.myice.hockey/api/mobilerest/refreshdata", + headers=mobile_headers, + data=f"token={userdata['token']}&id_club=186&language=FR", + # verify=False, + ) as r: + r.raise_for_status() + return r.json() + + +@app.command("mobile") +def mobile( + token: Annotated[str, typer.Option(envvar="MYICE_TOKEN")] = "", + id_club: Annotated[int, typer.Option(envvar="MYICE_CLUB")] = 0, +): + global userdata + if not token: + userdata = mobile_login() + else: + userdata = { + "id": 0, + "id_club": id_club, + "token": token, + } + print(json.dumps(refresh_data(), indent=2)) + + +@app.command("mobile-game") +def mobile_game( + 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, +): + global userdata + username, password, userid, existing_token = get_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() + with requests.post( + "https://app.myice.hockey/api/mobilerest/getevent", + headers=mobile_headers, + data="&".join( + [ + f"token={userdata['token']}", + f"id_event={game_id}", + "type=games", + f"id_player={userdata['id']}", + f"id_club={userdata['id_club']}", + "language=FR", + ] + ), + # verify=False, + ) as r: + data = r.json()["eventData"] + players = data["convocation"]["available"] + if raw: + print(data) + print( + f"Game {game_id}: {data['title']} ({data['type']}, {data['is_away']}) at {data['time_start']}" + ) + print(f"{len(players)} players") + for player in sorted( + players, + key=lambda x: ( + x["position"] if x["position"] else "none", + x["dob"], + ), + ): + print( + f"[{player['position']}] {player['fname']} {player['lname']} ({player['dob']})" + ) + + if __name__ == "__main__": app()