Source code for personalcapital.synchronization

import pymongo
import structlog
from datetime import datetime, timedelta

from personalcapital import APIConnector
from personalcapital.database import (
    Database,
    most_recent_transaction,
    has_transactions,
    insert_transactions
)
from personalcapital.query import (
    get_transactions,
    fetch_and_dump,
    get_accounts,
    update_transaction_categories,
    get_transaction_categories
)

log = structlog.getLogger()


[docs]def sync_transactions(margin=14): """ Update the database with transactions since the most recent transaction. An additional `margin` days are considered before the date of the most recent transaction date to account for late-arriving entries. :param int margin: Number of days to consider prior to the most recent transaction date. :return: None """ # set the time bounds as today back to a certain lookback period befor the most recent txn most_recent_ixn = most_recent_transaction() date_end = datetime.today() date_start = datetime.strptime(most_recent_ixn["transactionDate"], Database.DATE_FORMAT) - timedelta(days=margin) # query for all transactions in this period txns = get_transactions( date_start.strftime(Database.DATE_FORMAT), date_end.strftime(Database.DATE_FORMAT) ) # filter out transactions which are already in the database txn_existence_mask = has_transactions(txns) txns_new = [txn for txn, txn_exists in zip(txns, txn_existence_mask) if not txn_exists] insert_transactions(txns_new)