forked from f-klubben/stregsystemet-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·924 lines (777 loc) · 29.9 KB
/
main.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
#!/usr/bin/env python3
# START_STS
from __future__ import print_function
from random import random
import requests
import re
import json
import argparse
import sys
import os
import types
import urllib3
import importlib.machinery
import configparser
import builtins as __builtins__
import datetime
from collections import Counter
from datetime import date
from pprint import pprint
urllib3.disable_warnings()
CONSTANTS = {
'url': 'https://stregsystem.fklub.dk',
'room': '10',
'exit_words': [':q', 'exit', 'quit', 'q'],
'update_url': 'https://raw.githubusercontent.com/f-klubben/stregsystemet-cli/master/main.py',
'debug': False,
}
if sys.argv[0] == './main.py':
print('You are running the script in debug mode.')
CONSTANTS['url'] = 'http://localhost:8000'
CONSTANTS['room'] = '1'
CONSTANTS['debug'] = True
is_windows = sys.platform == "win32"
referer_header = {'Referer': CONSTANTS['url']}
balance = float()
config = configparser.ConfigParser()
file_loaded = False
user_id = ''
if is_windows:
os.system('color')
SHORTHANDS = {
'abrikos': 1899,
'ale16': 54,
'alkofri': 1901,
'alkoholfri': 1901,
'cacao': 1882,
'cider': 1831,
'classic': 14,
'cocio': 16,
'fritfit': 1902,
'fuzetea': 1879,
'glaspant': 1848,
'grøn': 14,
'kaffe': 32,
'kakao': 1882,
'kildevand': 1839,
'kinley': 1893,
'monster': 1837,
'monsterkaffe': 1900,
'månedens': 1903,
'noalc': 1901,
'nordic': 1901,
'nul': 1901,
'panta': 1848,
'pantb': 31,
'pantc': 1849,
'porter': 42,
'redbull': 1895,
'soda': 11,
'sodapant': 31,
'sodavand': 11,
'sommersby': 1831,
'specialøl': 1848,
'sportcola': 1891,
'sportscola': 1891,
'storpant': 1849,
'te': 1904,
'the': 1904,
'tuborgnul': 1901,
'øl': 14,
}
try:
if not os.path.exists(os.path.expanduser('~/.sts-wares')):
SHORTHANDS.update(
json.loads(
requests.get(
f'{CONSTANTS["url"]}/api/products/named_products', headers=referer_header, verify=False
).text
)
)
time = datetime.datetime.now()
with open(os.path.expanduser('~/.sts-wares'), 'a') as f:
f.writelines([str(time) + '\n', str(SHORTHANDS)])
else:
with open(os.path.expanduser('~/.sts-wares'), 'r') as f:
date_ = datetime.datetime.fromisoformat(f.readline().strip())
line = f.readline().replace("'", '"')
SHORTHANDS = json.loads(line)
if date_ + datetime.timedelta(days=7) < datetime.datetime.now():
if CONSTANTS['debug']:
print('Updating SHORTHANDS')
SHORTHANDS.update(
json.loads(
requests.get(
f'{CONSTANTS["url"]}/api/products/named_products', headers=referer_header, verify=False
).text
)
)
date_ = datetime.datetime.now()
with open(os.path.expanduser('~/.sts-wares'), 'w') as f:
f.writelines([str(date_) + '\n', str(SHORTHANDS)])
file_loaded = True
except Exception as e:
if CONSTANTS['debug']:
__builtins__.print(e)
SHORTHANDS = {
'abrikos': 1899,
'ale16': 54,
'alkofri': 1901,
'alkoholfri': 1901,
'cacao': 1882,
'cider': 1831,
'classic': 14,
'cocio': 16,
'fritfit': 1902,
'fuzetea': 1879,
'glaspant': 1848,
'grøn': 14,
'kaffe': 32,
'kakao': 1882,
'kildevand': 1839,
'kinley': 1893,
'monster': 1837,
'monsterkaffe': 1900,
'månedens': 1903,
'noalc': 1901,
'nordic': 1901,
'nul': 1901,
'panta': 1848,
'pantb': 31,
'pantc': 1849,
'porter': 42,
'redbull': 1895,
'soda': 11,
'sodapant': 31,
'sodavand': 11,
'sommersby': 1831,
'specialøl': 1848,
'sportcola': 1891,
'sportscola': 1891,
'storpant': 1849,
'te': 1904,
'the': 1904,
'tuborgnul': 1901,
'øl': 14,
}
with open(os.path.expanduser('~/.sts-wares'), 'w') as f:
f.writelines([str(datetime.datetime.now()) + '\n', str(SHORTHANDS)])
def is_int(value):
if not value:
return False
try:
int(value)
return True
except ValueError:
return False
_date = date.today()
month = _date.month
bat_amount = _date.day
lines_counted = 0
# Unicode characters for emojis
# https://apps.timwhitlock.info/emoji/tables/unicode
printables = {
4: '\U0001F414', # Chicken emoji
10: '\U0001F987', # Bat emoji
12: '\U00002744', # Snowflake emoji
}
special_months = [4, 10, 12]
if CONSTANTS['debug']:
print(f'MONTH={_date.month}')
print(f'PRINTABLE={printables.get(_date.month, "")}')
def print(*args, **kwargs):
global bat_amount, lines_counted
msg = ' '.join(map(str, args))
if _date.month in special_months and bat_amount > 0 and config.getboolean('sts', 'emoji_support', fallback=True):
# CPU Heater. Doesn't matter. Still faster than the actual stregsystem
print_prob = (
((random() * 10) / (len(' '.join(map(str, args))) * random() + 0.1) * random())
* ((_date.day / 100) + 1)
* 0.7
* (lines_counted * 0.15)
)
print_prob = print_prob / int(print_prob) if int(print_prob) >= 1 else print_prob
batted = False
for i in range(len(msg)):
if print_prob > random() and random() > random() and bat_amount > 0 and not batted:
if msg[i] == '-' or is_int(msg[i]):
if random() > 0.04:
print_prob += random()
continue
msg = msg[:i] + printables[_date.month] + msg[i + 1 :]
bat_amount -= 1
batted = True
print_prob = print_prob - random()
if print_prob <= 0 or bat_amount == 0:
break
else:
batted = False
__builtins__.print(msg, **kwargs)
else:
__builtins__.print(msg, **kwargs)
lines_counted += 1
def format_triple(ware, index_0, index_1, index_2):
r = ware[1]
if re.match(r'<\w+?\d*?>', ware[1]):
r = re.sub(r'<br>', ' - ', ware[1])
r = re.sub(r'</?\w+?\d*?>', '', r)
print('\u001B[31m{0:<{1}} {2:<{3}} {4:>{5}}\u001B[0m'.format(ware[0], index_0, r, index_1, ware[2], index_2))
else:
print('{0:<{1}} {2:<{3}} {4:>{5}}'.format(ware[0], index_0, r, index_1, ware[2], index_2))
def get_wares():
try:
session = requests.Session()
r = session.get(f"{CONSTANTS['url']}/{CONSTANTS['room']}/", verify=False)
except Exception:
print('Could not fetch wares from Stregsystement...')
raise SystemExit(1)
body = re.sub(r'\s{2,}', '', r.text)
item_id_list = re.findall(r'<td>(\d+)( / \w+)?</td>', body)
item_name_list = re.findall(r'<td>(.+?)</td>', body)
item_price_list = re.findall(r'<td align="right">(\d+\.\d+ kr)</td>', body)
item_name_list = [x for x in item_name_list if x not in [f'{y[0]}{y[1]}' for y in item_id_list]]
item_name_list = [x.replace('<br>', ' - ') for x in item_name_list]
# Filter out the items that are center html tagged
item_name_list = [x for x in item_name_list if re.match(r'<\w+\d+>', x) is not None or not x.startswith('<')]
global SHORTHANDS
# use dict comprehension to create a dictionary of item_id: item_name
SHORTHANDS.update(
{x[1].replace(' / ', '').lower(): int(x[0]) for x in item_id_list if len(x[1].replace(' / ', '')) > 1}
)
item_id_list = [x[0] for x in item_id_list]
session.close()
wares = []
for i in range(len(item_id_list)):
wares.append((item_id_list[i], item_name_list[i], item_price_list[i]))
return wares
wares = get_wares()
def print_wares(wares):
print('{:<8} {:<50} {:>10}'.format('Id', 'Item', 'Price'))
print('-' * 70)
[format_triple(ware, 8, 50, 10) for ware in wares]
def print_no_user_help(user):
print(
f'Det var sært, {user}. \nDet lader ikke til, at du er registreret som aktivt medlem af F-klubben i TREOENs dat'
f'abase. \nMåske tastede du forkert? \nHvis du ikke er medlem, kan du blive det ved at følge guiden på fklub.dk'
)
def test_user(user):
session = requests.Session()
r = session.get(f"{CONSTANTS['url']}/{CONSTANTS['room']}/", verify=False)
if r.status_code != 200:
print('Noget gik galt', r.status_code)
if CONSTANTS.get('debug', False):
print(r.content)
raise SystemExit(r.status_code)
token = re.search('(?<=name="csrfmiddlewaretoken" value=")(.+?)"', r.text)
json = {'quickbuy': f"{user}", 'csrfmiddlewaretoken': token.group(1)}
# pprint(json)
cookies = {'csrftoken': session.cookies.get_dict()['csrftoken'], 'djdt': 'show'}
# pprint(cookies)
sale = session.post(
f"{CONSTANTS['url']}/{CONSTANTS['room']}/sale/",
verify=False,
data=json,
cookies=cookies,
headers=referer_header,
)
session.close()
if sale.status_code != 200:
print('Noget gik galt.', sale.status_code, sale.content)
raise SystemExit
if 'Det lader ikke til, at du er registreret som aktivt medlem af F-klubben' in sale.text:
return False
balance_regex = re.search(r'(\d+.\d+) kroner til gode!', sale.text)
global balance
balance = float(balance_regex.group(1))
global user_id
user_id = re.search(r'\<a href="/' + CONSTANTS['room'] + '/user/(\d+)"', sale.text).group(1)
return True
def get_user_validated():
input_split = [None]
user = input('Hvad er dit brugernavn? ')
if ' ' in user:
input_split = user.split(' ')
user = input_split[0]
while not user or not test_user(user):
if user.lower() in CONSTANTS['exit_words']:
raise SystemExit
print(f"'{user}' is not a valid user")
user = input('Hvad er dit brugernavn? ')
if ' ' in user:
input_split = user.split(' ')
user = input_split[0]
return user, input_split[1:]
def print_history(wares):
print('{:<29} {:<40} {:>10}'.format('Date', 'Item', 'Price'))
print('-' * 81)
[format_triple(ware, 29, 40, 10) for ware in wares]
print('')
print('')
def get_history(user_id):
if not user_id:
print('Den angivne bruger har ikke noget ID. Afslutter')
raise SystemExit(1)
try:
session = requests.Session()
r = session.get(f"{CONSTANTS['url']}/{CONSTANTS['room']}/user/{user_id}", verify=False)
except Exception:
print('Kunne ikke oprette forbindelse til Stregsystenet')
raise SystemExit(1)
body = r.text
item_date_list = re.findall(r'<td>(\d+\.\s\w+\s\d+\s\d+:\d+)</td>', body)
item_name_list = [x for x in re.findall(r'<td>(.+?)</td>', body) if x not in item_date_list][
0 : len(item_date_list)
]
item_price_list = re.findall(r'<td align="right">(\d+\.\d+)</td>', body)
history = []
for x in range(len(item_date_list)):
history.append((item_date_list[x], item_name_list[x], f"{item_price_list[x]} kr"))
print_history(history)
global is_strandvejen
is_strandvejen = False
def print_coffee_amount(sale):
if 'koffein i kroppen' in sale.text:
in_body = re.search(r'Du har \d+mg koffein i kroppen\.', sale.text)
cups = re.search(r'Det svarer til at drikke .*? kopper kaffe i streg!', sale.text)
if cups:
print(in_body.group(0), cups.group(0))
else:
print(in_body.group(0))
def print_blood_alcohol_ration(sale):
if 'Din alkohol promille er ca. ' in sale.text:
bac = re.search(r'<b>(\d+,\d+‰)</b>', sale.text).group(1)
print(f'Din alkohol promille er ca. {bac}')
def parse_split_multibuy(itm):
count = 1
itm_arr = itm.split(':')
if len(itm_arr) == 2:
count = itm_arr[1]
itm = itm_arr[0]
elif len(itm_arr) > 2:
print('Du har fejl i dit multibuy format. Brug kun et : for at adskille antal og vare')
print('Din fejl er her:')
print(itm)
print(('-' * itm.index(':', itm.index(':') + 1)) + '^')
return None, 0
return itm, count
# Parse the table from stregsystemet that represents the scoreboard for a user
def parse_scoreboard(content):
content = (
re.search(r'<table class="ranking">(.+?)</table>', content, re.DOTALL)
.group(1)
.replace(' ', '')
.replace('\n', '')
)
headers = re.findall(r'<th class="ranking">(.+?)</th>', content)
fields = re.findall(r'<td class="ranking">(.+?)</td>', content)
return headers, fields[len(headers) :], fields[: len(headers)]
def format_scoreboard(triple):
print('{:<20} {:>10} {:>10}'.format(*triple))
def get_scoreboard():
global user_id
r = requests.get(f"{CONSTANTS['url']}/{CONSTANTS['room']}/user/{user_id}/rank", verify=False)
if r.status_code != 200:
__builtins__.print('Noget gik galt.', r.status_code, r.url)
raise SystemExit(r.status_code)
items = parse_scoreboard(r.text)
print('{:<20} {:>10} {:>10}'.format('Navn', 'Gennemsnit', 'Rank'))
print('-' * 42)
for i in range(len(items[0])):
format_scoreboard((items[0][i], items[1][i], items[2][i]))
def sale(user, itm, count=1):
if int(count) <= 0:
print('Du kan ikke købe negative mængder af varer.')
return
if count == 1 or ':' in itm.lower():
itm, count = parse_split_multibuy(itm)
# check for shorthand and replace
if itm in SHORTHANDS:
itm = str(SHORTHANDS[itm])
session = requests.Session()
r = session.get(f"{CONSTANTS['url']}/{CONSTANTS['room']}/", verify=False)
if r.status_code != 200:
print('Noget gik galt', r.status_code)
if CONSTANTS.get('debug', False):
print(r.content)
raise SystemExit
token = re.search('(?<=name="csrfmiddlewaretoken" value=")(.+?)"', r.text)
json = {'quickbuy': f"{user} {itm}:{count}", 'csrfmiddlewaretoken': token.group(1)}
sale = session.post(
f"{CONSTANTS['url']}/{CONSTANTS['room']}/sale/",
data=json,
cookies={'csrftoken': session.cookies.get_dict()['csrftoken'], 'djdt': 'show'},
headers=referer_header,
)
session.close()
if sale.status_code != 200 and sale.status_code != 402: # Stregforbud har 402, men no more inventory har ikke????
print("Du har ikke købt din vare. Prøv igen", sale.status_code)
raise SystemExit
elif 'STREGFORBUD!' not in sale.text:
if ':' in itm:
if is_int(itm.split(':')[1]):
count = itm.split(':')[1]
if int(count) <= 0:
print('Du kan ikke købe negative mængder af varer.')
return
itm = itm.split(':')[0]
else:
print('Du har angivet tekst hvor du skal angive en mængde')
return
ware = [x for x in wares if x[0] == itm]
if not len(ware):
print(f"Der findes ikke nogen varer med id {itm}.")
return
print(f'{user} har købt', count, ware[0][1], 'til', ware[0][2], 'stykket')
global balance
balance -= float(ware[0][2].replace('kr', '').strip()) * float(count)
itm_unit_price = float(ware[0][2].replace("kr", "").strip())
itm_units_left = f"{(balance / itm_unit_price):.2f}" if itm_unit_price > 0 else "∞"
print(f'Der er {balance:.2f} stregdollars - eller {itm_units_left} ' f'x {ware[0][1]} - tilbage')
print_coffee_amount(sale)
print_blood_alcohol_ration(sale)
else:
ware = [x for x in wares if x[0] == itm]
print(
f'''STREGFORBUD!
Du kan ikke foretage køb, før du har foretaget en indbetaling!
Du kan foretage indbetaling via MobilePay. Du har {balance} stregdollars til gode. Den vare du prøvede at købe kostede {ware[0][2]}'''
)
print('\nHer er en QR kode til at indsætte 50 kr på din stregkonto')
get_qr(user, 50)
def pre_parse(args, parser: argparse.ArgumentParser):
parser.add_argument('-z', '--noplugins', action='store_true', help='Disables the plugin loader')
parser.add_argument(
'-x', '--strandvejen', action='store_true', help='Flag used for the CRT terminal version running in strandvejen'
)
parser.add_argument('-a', '--update', action='store_true', help='Update the script and then exists')
parser.add_argument('-v', '--verbose', action='store_true', help='Prints information about the running script')
parser.add_argument('-s', '--setup', action='store_true', help='Runs the setup script')
args, _ = parser.parse_known_args(args)
return args
def parse(args, parser: argparse.ArgumentParser):
parser.add_argument(
'-u', '--user', default=None, nargs='?', dest='user', help='Specifies your Stregsystem username'
)
parser.add_argument('-i', '--item', default=None, nargs='?', dest='item', help='Specifies the item you wish to buy')
parser.add_argument(
'-c',
'--count',
default=1,
nargs='?',
dest='count',
type=int,
help='Specifies the amount of items you wish to buy',
)
parser.add_argument('-b', '--balance', action='store_true', help='Output only stregdollar balance')
parser.add_argument('-l', '--history', action='store_true', help='Shows your recent purchases')
parser.add_argument('-p', '--mobilepay', dest='money', help='Provides a QR code to insert money into your account')
parser.add_argument('-a', '--update', action='store_true', help='Update the script and then exists')
parser.add_argument('-o', '--shorthands', action='store_true', help='Shows shorthands')
parser.add_argument('-z', '--noplugins', action='store_true', help='Disables the plugin loader')
parser.add_argument(
'-x', '--strandvejen', action='store_true', help='Flag used for the CRT terminal version running in strandvejen'
)
parser.add_argument(
'-s', '--setup', action='store_true', help='Creates a .sts at /home/<user> storing your account username'
)
parser.add_argument('product', type=str, nargs='?', help="Specifies the product to buy")
parser.add_argument('-v', '--verbose', action='store_true', help='Prints information about the running script')
parser.add_argument('-r', '--rank', action='store_true', help='Shows your rank in different categories')
args = parser.parse_args(args)
return args
def get_item(ware_ids):
count = 1
item_id = input('Id> ')
if item_id.lower() in CONSTANTS['exit_words']:
return 'exit', 0
if ':' in item_id:
item_id, count = parse_split_multibuy(item_id)
if is_int(count):
if int(count) <= 0:
print('Du kan ikke købe negative mængder af varer.')
return None, 0
else:
print('Du har angivet tekst hvor du skal angive en mængde')
return None, 0
has_space = False
# check if all items in the item_id split on space are either in shorthands or is a valid item
if ' ' in item_id:
split_items = item_id.split(' ')
if any(((x in SHORTHANDS) or (is_int(x) and x in ware_ids)) for x in split_items):
has_space = True
return item_id, 1
if not (item_id in SHORTHANDS) and (has_space or (not is_int(item_id) or item_id not in ware_ids)):
if item_id.lower() in CONSTANTS['exit_words']:
return 'exit', 0
print(f"'{item_id}' is not a valid item")
return get_item(ware_ids)
return item_id, count
def user_buy(user):
if test_user(user):
# os.system('cls||clear')
print('Hej,', user)
print(f'Du har {balance:.2f} stregdollars')
print('')
print(
"Hvad ønsker at købe i Stregsystemet? (Skriv en af",
str(CONSTANTS['exit_words']),
"for at komme ud af interfacet)",
)
print_wares(wares)
print('')
global is_strandvejen
while True:
item, count = get_item([x[0] for x in wares])
if ' ' in item:
items = item.split(' ')
# Check if any of the items in items is in the constant exit words
if any(x in CONSTANTS['exit_words'] for x in items):
raise SystemExit
for purchase in Counter(items).items():
sale(user, purchase[0], purchase[1])
if is_strandvejen:
import time
time.sleep(5)
raise SystemExit
else:
if item in CONSTANTS['exit_words']:
raise SystemExit
elif item is None:
continue
sale(user, item, count)
if is_strandvejen:
import time
time.sleep(5)
raise SystemExit
else:
print_no_user_help(user)
def no_info_buy():
print("Du kan skrive 'exit' for at annullere.")
user, purchases = get_user_validated()
# If wares exists make a sale on every ware
if purchases:
for purchase in Counter(purchases).items():
sale(user, purchase[0], purchase[1])
else:
user_buy(user)
def get_qr(user, amount):
if is_int(amount) and int(amount) < 50:
print('Mindste indsætningsbeløb er 50. Alt under, håndteres ikke.')
return
print("Skan QR koden nedenfor, for at indsætte penge på din stregkonto")
session = requests.Session()
r = session.get(f"https://qrcode.show/mobilepay://send?phone=90601&comment={user}&amount={int(amount)}")
if r.status_code != 200:
print('Noget gik galt', r.status_code)
if CONSTANTS.get('debug', False):
__builtins__.print(r.content)
raise SystemExit
print(r.content.decode('UTF-8'))
def update_config_file(dirs):
# This function iterates all config files and prepends "[sts]\n" to them.
for path in dirs:
if not os.path.exists(path):
continue
with open(path, 'r') as original:
data = original.read()
if not data.startswith('[sts]'):
with open(path, 'w') as modified:
modified.write('[sts]\n' + data)
def read_config():
dirs = [os.path.expanduser('~/.config/sts/.sts'), os.path.expanduser('~/.sts'), '.sts']
try:
config.read(dirs)
except configparser.MissingSectionHeaderError:
# update the config to new format
update_config_file(dirs)
config.read(dirs)
def get_saved_user() -> str:
return config.get('sts', 'user', fallback=None)
def get_plugin_dir() -> str:
rawpath = config.get('sts', 'plugin_dir', fallback=None)
if rawpath == None:
return ""
else:
return os.path.expanduser(rawpath)
def calculate_sha256_binary(binary) -> str:
import hashlib
return hashlib.sha256(binary).hexdigest()
def has_version_difference():
r = requests.get(CONSTANTS['update_url'])
newest_file_hash = calculate_sha256_binary(r.content)
with open(__file__, 'rb') as f:
data = f.read()
current_file_hash = calculate_sha256_binary(data)
return newest_file_hash != current_file_hash
def update_script():
if not os.access(__file__, os.W_OK):
print('Stregsystemet-CLI er i læs-kun modus og kan derfor ikke opdateres. Er du på NIX operativsystem?')
return
if not has_version_difference():
return
# I perform open heart surgery on myself :)
with open(__file__, 'w', encoding="utf-8") as f:
r = requests.get(CONSTANTS['update_url'])
if 'START_STS' in r.text and 'END_STS' in r.text:
f.write(r.text)
def set_up_plugins(arg_array):
if not get_plugin_dir() and not os.path.exists('plugins/') and '-z' not in arg_array:
arg_array.insert(0, '-z')
elif get_plugin_dir():
pl_dir = get_plugin_dir()
pl_dir = f'{pl_dir}/' if not pl_dir.endswith('/') or not pl_dir.endswith('\\') else pl_dir
if not os.path.exists(f'{pl_dir}__init__.py'):
with open(f'{pl_dir}__init__.py', 'w') as f:
f.write('\x00')
return arg_array
def main():
arg_array = sys.argv[1::]
read_config()
purchases = None
parser = argparse.ArgumentParser()
_parser = argparse.ArgumentParser(add_help=False)
_args = pre_parse(arg_array, _parser)
if not _args.setup:
arg_array = set_up_plugins(arg_array)
if _args.verbose:
__builtins__.print(
f'PLUGIN FOLDER={get_plugin_dir()}',
f'RUNNING FROM={__file__}',
f'CURRENT USER={get_saved_user()}',
f'CONFIG LOCATION={"".join([p for p in [os.path.expanduser("~/.config/sts/.sts"), os.path.expanduser("~/.sts")] if os.path.exists(p)])}',
sep='\n',
)
if os.path.exists(get_plugin_dir() or 'plugins'):
try:
plugins = []
for item in [
f'{get_plugin_dir()}{item}'
for item in os.listdir(get_plugin_dir() or 'plugins')
if item.endswith('.py') and item != '__init__.py'
]:
loader = importlib.machinery.SourceFileLoader(item.replace('.py', ''), item)
mod = types.ModuleType(loader.name)
loader.exec_module(mod)
plugins.append(mod)
except Exception as e:
if e and CONSTANTS['debug']:
__builtins__.print('An error occured while loading plugin:\n', e)
elif not _args.strandvejen:
print('STS now supports plugins. Add "plugin_dir=~/.sts_plugins/" to your .sts file')
plugins = []
else:
if not _args.strandvejen:
print('STS now supports plugins. Add "plugin_dir=~/.sts_plugins/" to your .sts file')
plugins = []
if not _args.noplugins:
for plugin in plugins:
try:
plugin.pre_argparse(parser, CONSTANTS)
except (AttributeError, TypeError) as e:
__builtins__.print(e)
except Exception:
pass
if has_version_difference():
print("Der er en opdatering til STS. Hent den fra GitHub eller kør sts med --update.", file=sys.stderr)
args = parse(arg_array, parser)
if args.user is None:
args.user = get_saved_user()
if not args.noplugins:
for plugin in plugins:
try:
plugin.run(wares, args, arg_array, SHORTHANDS, CONSTANTS)
except (AttributeError, TypeError) as e:
__builtins__.print(e)
except Exception:
pass
global is_strandvejen
is_strandvejen = args.strandvejen
if args.update is True:
update_script()
return
if args.shorthands:
print("You can use the following shorthand for purchasing")
pprint(SHORTHANDS)
if args.verbose:
print(f'file_loaded={file_loaded}')
return
if args.user is None:
args.user = get_saved_user()
if args.setup:
if args.user is None:
args.user, purchases = get_user_validated()
if test_user(args.user):
answer = 'a'
while answer.lower() not in ['y', 'n']:
__builtins__.print('Please check if you are able to see the emojis between the <> symbols below')
__builtins__.print(f'<{printables[4]}><{printables[10]}><{printables[12]}>')
answer = input('Does your terminal support emojis? (y/n) ')
emoji_support = answer.lower() == 'y'
home = os.path.expanduser('~')
if not os.path.isfile(f"{home}/.sts"):
with open(f"{home}/.sts", "w") as f:
__builtins__.print(f"Your .sts file has been created at location {home}/.sts")
f.write("[sts]\n")
f.write(f"user={args.user}\n")
f.write(f"emoji_support={emoji_support}\n")
f.write('plugin_dir=')
if args.rank:
test_user(args.user)
get_scoreboard()
return
if args.user and args.product:
if test_user(args.user):
sale(args.user, args.item if args.item else str(args.product), args.count)
else:
print_no_user_help(args.user)
return
if args.balance and args.user:
test_user(args.user)
print(f'{balance:.2f}')
return
if args.history and args.user:
global user_id
test_user(args.user)
get_history(user_id)
if args.user is None or args.item is None:
if args.user is None:
args.user, purchases = get_user_validated()
if args.verbose:
print(f'USER={args.user}')
print(f'PURCHASES={purchases}')
if args.user is not None:
if args.money:
get_qr(args.user, args.money)
elif purchases:
for purchase in Counter(purchases).items():
sale(args.user, purchase[0], purchase[1])
if is_strandvejen:
import time
time.sleep(5)
raise SystemExit
else:
user_buy(args.user)
else:
no_info_buy()
else:
if test_user(args.user):
if args.money:
get_qr(args.user, args.money)
else:
sale(args.user, args.item, args.count)
else:
print_no_user_help(args.user)
if not args.noplugins:
for plugin in plugins:
try:
plugin.post_run(wares, args, arg_array, SHORTHANDS, CONSTANTS)
except (AttributeError, TypeError) as e:
__builtins__.print(e)
except Exception:
pass
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, EOFError):
raise SystemExit(0)
# END_STS