-
Notifications
You must be signed in to change notification settings - Fork 4
/
pagseguro2ofx.py
177 lines (157 loc) · 5.38 KB
/
pagseguro2ofx.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
import datetime
from decimal import Decimal
from xml.etree import ElementTree
import sys
class Transaction(object):
def __init__(self):
self.id = None
self.desc = None
self.value = None
self.date = None
self.type = None # CREDIT or DEBIT
OFX_HEADER = """OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<DTSERVER>%(start)s
<LANGUAGE>%(language)s
<DTACCTUP>%(start)s
<FI>
<ORG>%(org)s
<FID>%(fid)s
</FI>
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>0
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<STMTRS>
<CURDEF>BRL
<BANKACCTFROM>
<BANKID>001
<ACCTID>%(account)s
<ACCTTYPE>CHECKING
</BANKACCTFROM>
<BANKTRANLIST>
"""
OFX_STATEMENT = """
<STMTTRN>
<TRNTYPE>%(type)s
<DTPOSTED>%(date)s
<TRNAMT>%(value)s
<FITID>%(fid)s
<CHECKNUM>%(checknum)s
<MEMO>%(memo)s
</STMTTRN>
"""
OFX_FOOTER = """
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>%(balance)s
<DTASOF>%(end)s
</LEDGERBAL>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>"""
class OFXWriter(object):
def __init__(self, account):
self.account = account
self.transactions = []
self.balance = Decimal(0)
self.start = '20100201'
self.end = '20100228'
def add(self, transaction):
self.transactions.append(transaction)
def write(self, filename):
fp = open(filename, 'w')
self._write_header(fp)
for transaction in self.transactions:
self._write_transaction(fp, transaction)
self._write_footer(fp)
fp.close()
def _format_date(self, dt):
return dt.strftime('%Y%m%d')
def _write_header(self, fp):
fp.write(OFX_HEADER % dict(org='Pagseguro',
fid='001',
language='POR',
start=self.start,
account=self.account))
def _write_transaction(self, fp, transaction):
fp.write(OFX_STATEMENT % dict(type=transaction.type,
date=self._format_date(transaction.date),
value=transaction.value,
fid=transaction.id,
checknum=transaction.id,
memo=transaction.desc.encode('latin1')))
def _write_footer(self, fp):
fp.write(OFX_FOOTER % dict(balance=self.balance,
end=self.end))
class PagseguroParser(object):
def __init__(self):
self.transactions = []
def read(self, filename):
fp = open(filename)
et = ElementTree.parse(fp)
root = et.getroot()
for node in root.findall('Table'):
self._read_table(node)
def _parse_value(self, text):
text = text.replace('.', '')
text = text.replace(',', '.')
return Decimal(text)
def _parse_date(self, text):
return datetime.datetime.strptime(text, '%d/%m/%Y %H:%M:%S')
def _read_table(self, node):
status = node.find('Status').text
if status != 'Aprovada':
return
date = self._parse_date(node.find('Data_Compensacao').text)
t = Transaction()
t.id = node.find('Transacao_ID').text.split('-')[0]
t.value = self._parse_value(node.find('Valor_Bruto').text)
t.desc = '%s (%s)' % (node.find('Cliente_Nome').text,
node.find('Cliente_Email').text)
t.date = date
self.transactions.append(t)
if node.find('Debito_Credito').text == u'D\xe9bito':
t.type = 'DEBIT'
t.value = -t.value
else:
t.type = 'CREDIT'
taxa = self._parse_value(node.find('Valor_Taxa').text)
if taxa > 0:
oldid = t.id
t = Transaction()
t.id = oldid
t.type = 'CREDIT'
t.desc = "Pagseguro taxa 3.99% + R$0.40"
t.value = -self._parse_value(node.find('Valor_Taxa').text)
t.date = date
self.transactions.append(t)
def main():
psp = PagseguroParser()
psp.read(sys.argv[1])
ow = OFXWriter('[email protected]')
for transaction in psp.transactions:
ow.add(transaction)
ow.write(sys.argv[2])
if __name__ == '__main__':
main()