-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transaction export to YNAB import format
- Loading branch information
1 parent
dc3b72d
commit fb25982
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import asyncio | ||
from pytr.utils import preview | ||
import json | ||
from datetime import datetime, timedelta | ||
|
||
class Transactions: | ||
def __init__(self, tr,output_path, last_days): | ||
self.tr = tr | ||
self.output_path = output_path | ||
self.last_days = last_days | ||
self.transactions = [] | ||
|
||
async def loop(self): | ||
recv = 0 | ||
await self.tr.timeline_transactions() | ||
while True: | ||
_subscription_id, subscription, response = await self.tr.recv() | ||
|
||
if subscription['type'] == 'timelineTransactions': | ||
self.transactions.extend(response["items"]) | ||
|
||
# Transactions in the response are ordered from newest to oldest | ||
# If the oldest (= last) transaction is older than what we want, exit the loop | ||
t = self.transactions[-1] | ||
if datetime.fromisoformat(t['timestamp']) < datetime.now().astimezone() - timedelta(days=self.last_days): | ||
return | ||
|
||
await self.tr.timeline_transactions(response["cursors"]["after"]) | ||
|
||
else: | ||
print(f"unmatched subscription of type '{subscription['type']}':\n{preview(response)}") | ||
|
||
if recv == 1: | ||
return | ||
|
||
def output(self): | ||
transactions = [ | ||
t | ||
for t in self.transactions | ||
if datetime.fromisoformat(t['timestamp']) > datetime.now().astimezone() - timedelta(days=self.last_days) | ||
] | ||
|
||
with open(self.output_path, mode='w', encoding='utf-8') as output_file: | ||
json.dump(transactions, output_file) | ||
|
||
def get(self): | ||
asyncio.get_event_loop().run_until_complete(self.loop()) | ||
self.output() |