-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_transactions_daily.py
executable file
·96 lines (76 loc) · 3.33 KB
/
gather_transactions_daily.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
import os; os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'brmburo.settings')
from brmburo.transactions import payment_income, account_sum
from brmburo.models import BankTransaction, Currency, BankAccount
from brmburo import settings
from fiobank import FioBank
from django.core.exceptions import ObjectDoesNotExist
from decimal import Decimal
from time import sleep
import datetime
import logging
#FIRST_DATE = datetime.date(2010, 9, 1)
FIRST_DATE = datetime.date(2015,1,1)
logger = logging.getLogger(__name__)
for token in settings.BANK_TOKENS:
client = FioBank(token=token)
info = client.info()
curr,created = Currency.objects.get_or_create(symbol=info.get('currency'))
if created:
curr.name = curr.symbol
curr.save()
my,created = BankAccount.objects.get_or_create(account_number=info['account_number'], bank_code=info['bank_code'])
if created:
my.currency = curr
my.save()
logger.info('Processing account %s...' % my)
try:
from_id = BankTransaction.objects.filter(my_account=my).latest('date').tid
sleep(30) # to avoid HTTP 409 - "Conflict" response
trans = client.last(from_id=from_id)
except ObjectDoesNotExist:
sleep(30) # to avoid HTTP 409 - "Conflict" response
trans = client.last(from_date=FIRST_DATE)
for tran in trans:
tid = tran.get('transaction_id')
if BankTransaction.objects.filter(tid=tid).exists():
logger.warning('Transaction %s already here.'%tid)
continue
if 'account_number' not in tran or ( 'bank_code' not in tran and 'bic' not in tran ):
continue
acc,created = BankAccount.objects.get_or_create(account_number=tran['account_number'], bank_code=tran['bic'] if 'bic' in tran else tran['bank_code'])
if created:
acc.account_name = tran.get('account_name')
acc.save()
curr,created = Currency.objects.get_or_create(symbol=tran.get('currency'))
if created:
curr.name = curr.symbol
curr.save()
todecimal = lambda x: Decimal(str(x)) if x is not None else x
t = BankTransaction.objects.create(
tid = tran.get('transaction_id'),
my_account = my,
their_account = acc,
amount = todecimal(tran.get('amount')),
currency = curr,
constant_symbol = tran.get('constant_symbol'),
specific_symbol = tran.get('specific_symbol'),
variable_symbol = tran.get('variable_symbol'),
recipient_message = tran.get('recipient_message'),
comment = tran.get('comment'),
date = tran.get('date'),
)
t.save()
logger.info(u'Imported transaction %s.' % t)
lt, b = payment_income(t)
if lt is not None and b is not None:
template = u'Paired incomming payment %(amount)0.2f[%(curr1)s] from @%(nickname)s accounted as transaction #%(tid)d. New balance for @%(nickname)s is %(balance)s %(curr2)s.'
logger.info(template % dict(
amount = t.amount,
curr1 = t.currency.symbol,
curr2 = b.logic_account.currency.symbol,
nickname = b.nickname,
tid = lt.id,
balance = account_sum(b),
))
else:
logger.info(u'Not paired.')