-
Notifications
You must be signed in to change notification settings - Fork 2
/
PaperCouponHelper.py
95 lines (82 loc) · 4.29 KB
/
PaperCouponHelper.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
import os
from datetime import datetime
import Helper
from Helper import saveJson, getTimezone, loadJson
from BaseUtils import logging
""" Helper to complete config file with data from 'paper_coupon_helper_ids.txt'. """
def main():
activePaperCouponInfo = getActivePaperCouponInfo()
if len(activePaperCouponInfo) == 0:
# logging.info("Failed to find any currently valid paper coupon candidates --> Cannot add additional information")
return
paperCouponConfig = loadPaperCouponConfigFile()
for paperIdentifier in activePaperCouponInfo.keys():
filepath = 'paper_coupon_data/paper_coupon_helper_ids_' + paperIdentifier + '.txt'
if not os.path.isfile(filepath):
# Shouldn't happen but it's not too fatal - maybe there are just no paper coupons available at this moment.
logging.warning('No file available for paper coupon char ' + paperIdentifier + ' | ' + filepath)
continue
mapping = {}
if paperIdentifier == 'NOCHAR':
# Build mapping for currently valid coupons without char
couponIDs = []
with open(os.path.join(os.getcwd(), filepath), encoding='utf-8') as infile:
lineNumber = 0
for line in infile:
line = line.strip()
couponInfo = line.split(':')
couponIDStr = couponInfo[0]
if not couponIDStr.isdecimal():
raise Exception('Invalid paper coupon input: ' + line)
if couponIDStr in couponIDs:
logging.warning('Found at least one duplicate ID at line' + str(lineNumber) + ' : ' + couponIDStr)
continue
mapping[couponIDStr] = couponInfo[1]
lineNumber += 1
else:
couponIDs = []
with open(os.path.join(os.getcwd(), filepath), encoding='utf-8') as infile:
lineNumber = 0
for line in infile:
line = line.strip()
if not line.isdecimal():
raise Exception('Invalid paper coupon input: ' + line)
couponIDStr = line
if line in couponIDs:
logging.warning('Found at least one duplicate ID at line' + str(lineNumber) + ' : ' + couponIDStr)
continue
couponIDs.append(couponIDStr)
lineNumber += 1
if len(couponIDs) == 0:
raise Exception("Failed to find any mapping data in mapping file " + filepath)
# Validate array size
if len(couponIDs) < 47 or len(couponIDs) > 48:
logging.warning('Array length mismatch: ' + str(len(couponIDs)))
# Build mapping
number = 1
for couponIDStr in couponIDs:
# Correct number: Assume than when only 47 items are given, the payback code is missing -> That is usually number 47
if len(couponIDs) == 47 and number == 47:
number = 48
mapping[couponIDStr] = paperIdentifier + str(number)
number += 1
# print('Mapping result = ' + str(mapping))
# Finally update our config
paperCouponConfig.setdefault(paperIdentifier, {})['mapping'] = mapping
# paperCouponConfig[paperChar]['mapping'] = mapping
# Update our config file accordingly
saveJson(Helper.Paths.paperCouponExtraDataPath, paperCouponConfig)
if __name__ == "__main__":
main()
def getActivePaperCouponInfo() -> dict:
paperCouponInfo = {}
""" Load file which contains some extra data which can be useful to correctly determine the "CouponType" and expire date of paper coupons. """
for paperIdentifier, paperData in loadPaperCouponConfigFile().items():
validuntil = datetime.strptime(paperData['expire_date'] + ' 23:59:59', '%Y-%m-%d %H:%M:%S').astimezone(getTimezone()).timestamp()
if validuntil > Helper.getCurrentDate().timestamp():
newPaperData = paperData
newPaperData['expire_timestamp'] = validuntil
paperCouponInfo[paperIdentifier] = newPaperData
return paperCouponInfo
def loadPaperCouponConfigFile() -> dict:
return loadJson(Helper.Paths.paperCouponExtraDataPath)