|
| 1 | +from typing import Optional, Callable |
| 2 | + |
1 | 3 | from requests import Session |
2 | 4 |
|
3 | | -def check_status(session: Session, show_quota: bool = False) -> bool: |
4 | | - response = session.get(url = 'https://kox.moe/my.php') |
| 5 | +from kmdr.core.error import LoginError |
| 6 | + |
| 7 | +PROFILE_URL = 'https://kox.moe/my.php' |
| 8 | +LOGIN_URL = 'https://kox.moe/login.php' |
| 9 | + |
| 10 | +NICKNAME_ID = 'div_nickname_display' |
| 11 | + |
| 12 | +VIP_ID = 'div_user_vip' |
| 13 | +NOR_ID = 'div_user_nor' |
| 14 | +LV1_ID = 'div_user_lv1' |
| 15 | + |
| 16 | +def check_status( |
| 17 | + session: Session, |
| 18 | + show_quota: bool = False, |
| 19 | + is_vip_setter: Optional[Callable[[int], None]] = None, |
| 20 | + level_setter: Optional[Callable[[int], None]] = None |
| 21 | +) -> bool: |
| 22 | + response = session.get(url = PROFILE_URL) |
5 | 23 |
|
6 | 24 | try: |
7 | 25 | response.raise_for_status() |
8 | 26 | except Exception as e: |
9 | 27 | print(f"Error: {type(e).__name__}: {e}") |
10 | 28 | return False |
| 29 | + |
| 30 | + if response.history and any(resp.status_code in (301, 302, 307) for resp in response.history) \ |
| 31 | + and response.url == LOGIN_URL: |
| 32 | + raise LoginError("Invalid credentials, please login again.", ['kmdr config -c cookie', 'kmdr login -u <username>']) |
11 | 33 |
|
12 | | - if not show_quota: |
| 34 | + if not is_vip_setter and not level_setter and not show_quota: |
13 | 35 | return True |
14 | 36 |
|
15 | 37 | from bs4 import BeautifulSoup |
16 | 38 |
|
17 | 39 | soup = BeautifulSoup(response.text, 'html.parser') |
| 40 | + |
| 41 | + script = soup.find('script', language="javascript") |
| 42 | + |
| 43 | + if script: |
| 44 | + var_define = extract_var_define(script.text[:100]) |
| 45 | + |
| 46 | + is_vip = int(var_define.get('is_vip', '0')) |
| 47 | + user_level = int(var_define.get('user_level', '0')) |
| 48 | + |
| 49 | + if is_vip_setter: |
| 50 | + is_vip_setter(is_vip) |
| 51 | + if level_setter: |
| 52 | + level_setter(user_level) |
18 | 53 |
|
19 | | - nickname = soup.find('div', id='div_nickname_display').text.strip().split(' ')[0] |
20 | | - print(f"=========================\n\nLogged in as {nickname}\n\n=========================\n") |
21 | | - |
22 | | - quota = soup.find('div', id='div_user_vip').text.strip() |
23 | | - print(f"=========================\n\n{quota}\n\n=========================\n") |
| 54 | + if not show_quota: |
| 55 | + return True |
| 56 | + |
| 57 | + nickname = soup.find('div', id=NICKNAME_ID).text.strip().split(' ')[0] |
| 58 | + quota = soup.find('div', id=__resolve_quota_id(is_vip, user_level)).text.strip() |
| 59 | + |
| 60 | + print(f"\n当前登录为 {nickname}\n\n{quota}") |
24 | 61 | return True |
| 62 | + |
| 63 | +def extract_var_define(script_text) -> dict[str, str]: |
| 64 | + var_define = {} |
| 65 | + for line in script_text.splitlines(): |
| 66 | + line = line.strip() |
| 67 | + if line.startswith("var ") and "=" in line: |
| 68 | + var_name, var_value = line[4:].split("=", 1) |
| 69 | + var_define[var_name.strip()] = var_value.strip().strip(";").strip('"') |
| 70 | + return var_define |
| 71 | + |
| 72 | +def __resolve_quota_id(is_vip: Optional[int] = None, user_level: Optional[int] = None): |
| 73 | + if is_vip is not None and is_vip >= 1: |
| 74 | + return VIP_ID |
| 75 | + |
| 76 | + if user_level is not None and user_level <= 1: |
| 77 | + return LV1_ID |
25 | 78 |
|
| 79 | + return NOR_ID |
0 commit comments