-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathktfetch
executable file
·111 lines (86 loc) · 2.44 KB
/
ktfetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import animexx
import argparse
import sys
from bs4 import BeautifulSoup
DEBUG = False
def fatal(msg, exit_code = 1):
print(msg, file = sys.stderr)
exit(exit_code)
def log_debug(msg):
if DEBUG:
print(msg, file = sys.stderr)
def fetch_ktabhol_hash(userId, session):
url = 'https://{host}/mitglieder/steckbrief.php?id={userId:d}'.format(
host = animexx.Host,
userId = userId,
)
response = animexx.fetch(url, session = session)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract `ktabhol_hash`, which is written to an HTML `script` tag as an
# JavaScript assignment.
for scriptTag in soup.find_all('script'):
script = scriptTag.text.strip()
if script.startswith('ktabhol_hash'):
return script[16:-2]
return None
def fetch_kt(ktabhol_hash, session):
url = 'https://{host}/items/ajax-ktabholen.php?hash={ktabhol_hash}'.format(
host = animexx.Host,
ktabhol_hash = ktabhol_hash,
)
response = animexx.fetch(url, session = session)
log_debug('Response: %s' % response.text)
# Clean up text
text = response.text
if text.startswith('"') and text.endswith('"'):
text = text[1:-1]
try:
new_saldo = int(text)
except ValueError:
new_saldo = None
# Reasons for failure could be:
# - Already fetched today – might also return the current saldo
# - Session expired (should trigger an error when fetching `ktabhol_hash`)
# - Not allowed to fetch (i.e. for "Sonderaccounts")
return new_saldo
def main():
global DEBUG
parser = argparse.ArgumentParser()
parser.add_argument(
'--debug',
help = 'Enable debug output on `stderr`.',
action = 'store_true',
default = False,
)
parser.add_argument(
'-u', '--user',
help = 'Your Animexx User-ID.',
type = int,
required = True,
)
parser.add_argument(
'-s', '--session',
help = 'Your `PHPSESSID` cookie value.',
type = str,
required = True,
)
parser.add_argument(
'-l', '--login',
help = 'Your `autologin` cookie value.',
type = str,
required = True,
)
args = parser.parse_args()
DEBUG = args.debug
userId = args.user
session = animexx.new_session(args.session, args.login)
ktabhol_hash = fetch_ktabhol_hash(userId, session)
if not ktabhol_hash:
fatal('No `ktabhol_hash` found. Maybe the login has expired…', 2)
new_saldo = fetch_kt(ktabhol_hash, session)
if not new_saldo:
fatal('Failed to fetch KTs.')
print('Fetched KT, you now have {saldo:d} KTs.'.format(saldo = new_saldo))
if __name__ == '__main__':
main()