transaction pull added

leonid98
leonid98 7 months ago
parent bc18683455
commit f82aac5173
  1. 284
      main.py
  2. 235
      mock_response.py

@ -1,5 +1,7 @@
from flask import Flask
from mock_response import MOCK_RESPONSE
app = Flask(__name__)
@app.route("/")
@ -9,239 +11,51 @@ def index():
if __name__ == "__main__":
app.run(port=8080)
def transactions_pull(mongodb):
if not const.LHV_IMPORT:
return ""
last_timestamps = {}
for self_account in SELF_ACCOUNTS:
last_timestamps[self_account] = date(2015, 1, 1)
for row in mongodb.cashflow.find({"type": "transaction", "self_account": self_account}).sort([("_id",-1)]).limit(1):
last_timestamps[self_account] = datetime.strptime(row["timestamp"], "%Y-%m-%d")
today = date.today()
for self_account in SELF_ACCOUNTS:
created = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000")
start = last_timestamps[self_account].strftime("%Y-%m-%d")
finish = today.strftime("%Y-%m-%d")
# zomg what
while True:
response = requests.get("https://connect.lhv.eu/messages/next", cert=KEYPAIR)
msg_id = response.headers.get("Message-Response-Id")
if not msg_id:
break
print("Discarding:", msg_id)
response = requests.delete("https://connect.lhv.eu/messages/%s" % msg_id, cert=KEYPAIR)
response = requests.post("https://connect.lhv.eu/account-statement", data=BUF % locals(), cert=KEYPAIR)
sleep(2)
response = requests.get("https://connect.lhv.eu/messages/next", cert=KEYPAIR)
buf = response.content
if not buf:
raise ValueError("Failed to retrieve next %s" % response.status_code)
for values in import_statements(buf):
mongodb.cashflow.update_one({
"type": "transaction",
"reference": values[0]
}, {
"$set": {
"timestamp": values[1],
"self_account": values[2],
"peer_account": values[3],
"peer_name": values[4],
"peer_name_normalized": norm(normalize_legal_entity(values[4])),
"credit_debit": values[5],
"amount": values[6],
"comment": values[7]
},
}, upsert=True)
auto_associate(mongodb)
requests.get("https://hc-ping.com/375391a5-ff13-47e3-8892-876d1d995058")
return "ok"
MOCK_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd">
<BkToCstmrStmt>
<GrpHdr>
<MsgId>180114572</MsgId>
<CreDtTm>2022-04-22T00:29:16.262806</CreDtTm>
</GrpHdr>
<Stmt>
<Id>180114572EUR</Id>
<CreDtTm>2022-04-22T00:29:16.262806</CreDtTm>
<FrToDt>
<FrDtTm>2022-04-21T01:00:00</FrDtTm>
<ToDtTm>2022-04-22T00:29:15</ToDtTm>
</FrToDt>
<Acct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
<Ccy>EUR</Ccy>
<Svcr>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
<PstlAdr>
<AdrTp>BIZZ</AdrTp>
<StrtNm>Tartu mnt.</StrtNm>
<BldgNb>2</BldgNb>
<PstCd>10145</PstCd>
<TwnNm>Tallinn</TwnNm>
<CtrySubDvsn>Harjumaa</CtrySubDvsn>
<Ctry>EE</Ctry>
</PstlAdr>
</FinInstnId>
</Svcr>
</Acct>
<Bal>
<Tp>
<CdOrPrtry>
<Cd>OPBD</Cd>
</CdOrPrtry>
</Tp>
<Amt Ccy="EUR">0.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Dt>
<Dt>2022-04-21</Dt>
</Dt>
</Bal>
<Bal>
<Tp>
<CdOrPrtry>
<Cd>CLBD</Cd>
</CdOrPrtry>
</Tp>
<Amt Ccy="EUR">64.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Dt>
<Dt>2022-04-22</Dt>
</Dt>
</Bal>
<TxsSummry>
<TtlCdtNtries>
<NbOfNtries>2</NbOfNtries>
<Sum>64.00</Sum>
</TtlCdtNtries>
<TtlDbtNtries>
<NbOfNtries>0</NbOfNtries>
<Sum>0.00</Sum>
</TtlDbtNtries>
</TxsSummry>
<Ntry>
<Amt Ccy="EUR">35.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Sts>BOOK</Sts>
<BookgDt>
<Dt>2022-04-21</Dt>
</BookgDt>
<AcctSvcrRef>C3D9FD211FC1EC11A2E400155DA95D0B</AcctSvcrRef>
<BkTxCd>
<Domn>
<Cd>PMNT</Cd>
<Fmly>
<Cd>RCDT</Cd>
<SubFmlyCd>OTHR</SubFmlyCd>
</Fmly>
</Domn>
<Prtry>
<Cd>INTERNAL</Cd>
</Prtry>
</BkTxCd>
<NtryDtls>
<TxDtls>
<Refs>
<AcctSvcrRef>C3D9FD211FC1EC11A2E400155DA95D0B</AcctSvcrRef>
<InstrId>31</InstrId>
</Refs>
<AmtDtls>
<InstdAmt>
<Amt Ccy="EUR">35.00</Amt>
</InstdAmt>
<TxAmt>
<Amt Ccy="EUR">35.00</Amt>
</TxAmt>
</AmtDtls>
<RltdPties>
<Dbtr>
<Nm>Rasmus Kallas</Nm>
<Id>
<PrvtId>
<Othr>
<Id>50304110853</Id>
<SchmeNm>
<Cd>NIDN</Cd>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>EE677700771002886454</IBAN>
</Id>
</DbtrAcct>
<Cdtr>
<Nm>K-SPACE MTÜ</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
</CdtrAcct>
</RltdPties>
<RltdAgts>
<DbtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
</FinInstnId>
</CdtrAgt>
</RltdAgts>
<RmtInf>
<Ustrd>Membership</Ustrd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
<Ntry>
<Amt Ccy="EUR">29.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Sts>BOOK</Sts>
<BookgDt>
<Dt>2022-04-21</Dt>
</BookgDt>
<AcctSvcrRef>1A79D7E887C1EC11A2E400155DA95D0B</AcctSvcrRef>
<BkTxCd>
<Domn>
<Cd>PMNT</Cd>
<Fmly>
<Cd>RCDT</Cd>
<SubFmlyCd>OTHR</SubFmlyCd>
</Fmly>
</Domn>
<Prtry>
<Cd>INST</Cd>
</Prtry>
</BkTxCd>
<NtryDtls>
<TxDtls>
<Refs>
<AcctSvcrRef>1A79D7E887C1EC11A2E400155DA95D0B</AcctSvcrRef>
<PmtInfId>RD230928989</PmtInfId>
<InstrId>294</InstrId>
</Refs>
<AmtDtls>
<InstdAmt>
<Amt Ccy="EUR">29.00</Amt>
</InstdAmt>
<TxAmt>
<Amt Ccy="EUR">29.00</Amt>
</TxAmt>
</AmtDtls>
<RltdPties>
<Dbtr>
<Nm>JOON OTT</Nm>
<Id>
<PrvtId>
<Othr>
<Id>39907180819</Id>
<SchmeNm>
<Cd>NIDN</Cd>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>EE531010010557795013</IBAN>
</Id>
</DbtrAcct>
<Cdtr>
<Nm>K-SPACE MTÜ</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
</CdtrAcct>
</RltdPties>
<RltdAgts>
<DbtrAgt>
<FinInstnId>
<BIC>EEUHEE2X</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
</FinInstnId>
</CdtrAgt>
</RltdAgts>
<RmtInf>
<Ustrd>Regular tier</Ustrd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Stmt>
</BkToCstmrStmt>
</Document>"""

@ -0,0 +1,235 @@
MOCK_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd">
<BkToCstmrStmt>
<GrpHdr>
<MsgId>180114572</MsgId>
<CreDtTm>2022-04-22T00:29:16.262806</CreDtTm>
</GrpHdr>
<Stmt>
<Id>180114572EUR</Id>
<CreDtTm>2022-04-22T00:29:16.262806</CreDtTm>
<FrToDt>
<FrDtTm>2022-04-21T01:00:00</FrDtTm>
<ToDtTm>2022-04-22T00:29:15</ToDtTm>
</FrToDt>
<Acct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
<Ccy>EUR</Ccy>
<Svcr>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
<PstlAdr>
<AdrTp>BIZZ</AdrTp>
<StrtNm>Tartu mnt.</StrtNm>
<BldgNb>2</BldgNb>
<PstCd>10145</PstCd>
<TwnNm>Tallinn</TwnNm>
<CtrySubDvsn>Harjumaa</CtrySubDvsn>
<Ctry>EE</Ctry>
</PstlAdr>
</FinInstnId>
</Svcr>
</Acct>
<Bal>
<Tp>
<CdOrPrtry>
<Cd>OPBD</Cd>
</CdOrPrtry>
</Tp>
<Amt Ccy="EUR">0.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Dt>
<Dt>2022-04-21</Dt>
</Dt>
</Bal>
<Bal>
<Tp>
<CdOrPrtry>
<Cd>CLBD</Cd>
</CdOrPrtry>
</Tp>
<Amt Ccy="EUR">64.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Dt>
<Dt>2022-04-22</Dt>
</Dt>
</Bal>
<TxsSummry>
<TtlCdtNtries>
<NbOfNtries>2</NbOfNtries>
<Sum>64.00</Sum>
</TtlCdtNtries>
<TtlDbtNtries>
<NbOfNtries>0</NbOfNtries>
<Sum>0.00</Sum>
</TtlDbtNtries>
</TxsSummry>
<Ntry>
<Amt Ccy="EUR">35.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Sts>BOOK</Sts>
<BookgDt>
<Dt>2022-04-21</Dt>
</BookgDt>
<AcctSvcrRef>C3D9FD211FC1EC11A2E400155DA95D0B</AcctSvcrRef>
<BkTxCd>
<Domn>
<Cd>PMNT</Cd>
<Fmly>
<Cd>RCDT</Cd>
<SubFmlyCd>OTHR</SubFmlyCd>
</Fmly>
</Domn>
<Prtry>
<Cd>INTERNAL</Cd>
</Prtry>
</BkTxCd>
<NtryDtls>
<TxDtls>
<Refs>
<AcctSvcrRef>C3D9FD211FC1EC11A2E400155DA95D0B</AcctSvcrRef>
<InstrId>31</InstrId>
</Refs>
<AmtDtls>
<InstdAmt>
<Amt Ccy="EUR">35.00</Amt>
</InstdAmt>
<TxAmt>
<Amt Ccy="EUR">35.00</Amt>
</TxAmt>
</AmtDtls>
<RltdPties>
<Dbtr>
<Nm>Rasmus Kallas</Nm>
<Id>
<PrvtId>
<Othr>
<Id>50304110853</Id>
<SchmeNm>
<Cd>NIDN</Cd>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>EE677700771002886454</IBAN>
</Id>
</DbtrAcct>
<Cdtr>
<Nm>K-SPACE MTÜ</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
</CdtrAcct>
</RltdPties>
<RltdAgts>
<DbtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
<Nm>AS LHV Pank</Nm>
</FinInstnId>
</CdtrAgt>
</RltdAgts>
<RmtInf>
<Ustrd>Membership</Ustrd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
<Ntry>
<Amt Ccy="EUR">29.00</Amt>
<CdtDbtInd>CRDT</CdtDbtInd>
<Sts>BOOK</Sts>
<BookgDt>
<Dt>2022-04-21</Dt>
</BookgDt>
<AcctSvcrRef>1A79D7E887C1EC11A2E400155DA95D0B</AcctSvcrRef>
<BkTxCd>
<Domn>
<Cd>PMNT</Cd>
<Fmly>
<Cd>RCDT</Cd>
<SubFmlyCd>OTHR</SubFmlyCd>
</Fmly>
</Domn>
<Prtry>
<Cd>INST</Cd>
</Prtry>
</BkTxCd>
<NtryDtls>
<TxDtls>
<Refs>
<AcctSvcrRef>1A79D7E887C1EC11A2E400155DA95D0B</AcctSvcrRef>
<PmtInfId>RD230928989</PmtInfId>
<InstrId>294</InstrId>
</Refs>
<AmtDtls>
<InstdAmt>
<Amt Ccy="EUR">29.00</Amt>
</InstdAmt>
<TxAmt>
<Amt Ccy="EUR">29.00</Amt>
</TxAmt>
</AmtDtls>
<RltdPties>
<Dbtr>
<Nm>JOON OTT</Nm>
<Id>
<PrvtId>
<Othr>
<Id>39907180819</Id>
<SchmeNm>
<Cd>NIDN</Cd>
</SchmeNm>
</Othr>
</PrvtId>
</Id>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>EE531010010557795013</IBAN>
</Id>
</DbtrAcct>
<Cdtr>
<Nm>K-SPACE MTÜ</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>EE467700771002926893</IBAN>
</Id>
</CdtrAcct>
</RltdPties>
<RltdAgts>
<DbtrAgt>
<FinInstnId>
<BIC>EEUHEE2X</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>LHVBEE22</BIC>
</FinInstnId>
</CdtrAgt>
</RltdAgts>
<RmtInf>
<Ustrd>Regular tier</Ustrd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Stmt>
</BkToCstmrStmt>
</Document>"""
Loading…
Cancel
Save