diff --git a/main.py b/main.py index ed1eb4c..f2ed100 100755 --- a/main.py +++ b/main.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 = """ - - - - 180114572 - 2022-04-22T00:29:16.262806 - - - 180114572EUR - 2022-04-22T00:29:16.262806 - - 2022-04-21T01:00:00 - 2022-04-22T00:29:15 - - - - EE467700771002926893 - - EUR - - - LHVBEE22 - AS LHV Pank - - BIZZ - Tartu mnt. - 2 - 10145 - Tallinn - Harjumaa - EE - - - - - - - - OPBD - - - 0.00 - CRDT -
-
2022-04-21
- -
- - - - CLBD - - - 64.00 - CRDT -
-
2022-04-22
- -
- - - 2 - 64.00 - - - 0 - 0.00 - - - - 35.00 - CRDT - BOOK - -
2022-04-21
-
- C3D9FD211FC1EC11A2E400155DA95D0B - - - PMNT - - RCDT - OTHR - - - - INTERNAL - - - - - - C3D9FD211FC1EC11A2E400155DA95D0B - 31 - - - - 35.00 - - - 35.00 - - - - - Rasmus Kallas - - - - 50304110853 - - NIDN - - - - - - - - EE677700771002886454 - - - - K-SPACE MTÜ - - - - EE467700771002926893 - - - - - - - LHVBEE22 - AS LHV Pank - - - - - LHVBEE22 - AS LHV Pank - - - - - Membership - - - -
- - 29.00 - CRDT - BOOK - -
2022-04-21
-
- 1A79D7E887C1EC11A2E400155DA95D0B - - - PMNT - - RCDT - OTHR - - - - INST - - - - - - 1A79D7E887C1EC11A2E400155DA95D0B - RD230928989 - 294 - - - - 29.00 - - - 29.00 - - - - - JOON OTT - - - - 39907180819 - - NIDN - - - - - - - - EE531010010557795013 - - - - K-SPACE MTÜ - - - - EE467700771002926893 - - - - - - - EEUHEE2X - - - - - LHVBEE22 - - - - - Regular tier - - - -
-
-
-
""" \ No newline at end of file diff --git a/mock_response.py b/mock_response.py new file mode 100644 index 0000000..aad955f --- /dev/null +++ b/mock_response.py @@ -0,0 +1,235 @@ +MOCK_RESPONSE = """ + + + + 180114572 + 2022-04-22T00:29:16.262806 + + + 180114572EUR + 2022-04-22T00:29:16.262806 + + 2022-04-21T01:00:00 + 2022-04-22T00:29:15 + + + + EE467700771002926893 + + EUR + + + LHVBEE22 + AS LHV Pank + + BIZZ + Tartu mnt. + 2 + 10145 + Tallinn + Harjumaa + EE + + + + + + + + OPBD + + + 0.00 + CRDT +
+
2022-04-21
+ +
+ + + + CLBD + + + 64.00 + CRDT +
+
2022-04-22
+ +
+ + + 2 + 64.00 + + + 0 + 0.00 + + + + 35.00 + CRDT + BOOK + +
2022-04-21
+
+ C3D9FD211FC1EC11A2E400155DA95D0B + + + PMNT + + RCDT + OTHR + + + + INTERNAL + + + + + + C3D9FD211FC1EC11A2E400155DA95D0B + 31 + + + + 35.00 + + + 35.00 + + + + + Rasmus Kallas + + + + 50304110853 + + NIDN + + + + + + + + EE677700771002886454 + + + + K-SPACE MTÜ + + + + EE467700771002926893 + + + + + + + LHVBEE22 + AS LHV Pank + + + + + LHVBEE22 + AS LHV Pank + + + + + Membership + + + +
+ + 29.00 + CRDT + BOOK + +
2022-04-21
+
+ 1A79D7E887C1EC11A2E400155DA95D0B + + + PMNT + + RCDT + OTHR + + + + INST + + + + + + 1A79D7E887C1EC11A2E400155DA95D0B + RD230928989 + 294 + + + + 29.00 + + + 29.00 + + + + + JOON OTT + + + + 39907180819 + + NIDN + + + + + + + + EE531010010557795013 + + + + K-SPACE MTÜ + + + + EE467700771002926893 + + + + + + + EEUHEE2X + + + + + LHVBEE22 + + + + + Regular tier + + + +
+
+
+
""" \ No newline at end of file