-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
101 lines (79 loc) · 2.12 KB
/
parser.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
import csv
import urllib.request
import sys
import datetime
import xml.etree.ElementTree as ET
prefix = ""
host = "billing.example.com"
path = "cgi-bin/osmp.cgi"
url_base = "http://{host}/{path}".format(host=host,path=path)
check = "command=check&account={account}&txn_id={txn_id}&sum={sum}"
pay = "command=pay&account={account}&txn_id={txn_id}&sum={sum}&txn_date={txn_date:%Y%m%d%H%M%S}"
#txn_date=20090815120133
filename = sys.argv[1]
print('Loading %s' % filename)
def push(to,row):
q = to.format(**row)
url = '?'.join([url_base,q])
print(url)
try:
re = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
print('[http {}]'.format(e.code))
return
data = re.read().decode()
print(data)
try:
root = ET.fromstring(data)
except:
print('[bad xml]')
return
assert root.tag == 'response', 'bad format'
code = int(root.find('result').text)
assert code == 0, 'Status %s' % code
return True
def parse(row):
row['txn_id'] = prefix + row['txn_id']
row['txn_date'] = datetime.datetime.strptime( "%s %s" % (row['date'], row['time']), '%d-%m-%Y %H-%M-%S')
row['sum'] = row['sum'].replace(',','.')
print(row['txn_date'], row['account'])
try:
print('check', end=' ')
push(check,row)
print('[ok]')
except Exception as e:
print('[failed]', end='')
print(e)
return False
try:
print('pay', end=' ')
push(pay,row)
print('[ok]')
return True
except Exception as e:
print('[failed]', end=' ')
print(e)
return False
fieldnames = [
'date',
'time',
'no_ot',
'no_kas',
'txn_id',
'account',
'fio',
'address',
'sum',
'sum2',
'commission'
]
import codecs
from kassa import Kassa
k = Kassa("socket://192.168.137.111:7778",115200)
with codecs.open(filename,encoding='cp1251') as csvfile:
reader = csv.DictReader(csvfile,fieldnames=fieldnames,delimiter=';')
for row in reader:
if "=" in row['date']:
continue
if parse(row):
k.bill(row)