-
Hi, i already saw this thread, but i though, my issue might be a bot off, so i wrote a seperate one. I'm currently facing a lot of duplicate transactions. I wrote a small python script to delete the transactions. I'm using Afterwards i use pandas to filter out duplicates and get a list of them. I'm trying to delete transactions using This is the python script: from dotenv import load_dotenv
from os import environ
from Transactions import Transaction
from datetime import datetime
import pandas as pd
import calendar
import json
import uuid
import requests
load_dotenv(override=True)
url = environ['API_URL']
headers = {
"X-Trace-Id": str(uuid.uuid1()),
"accept": "application/vnd.api+json",
"Content-Type": "application/json",
"Authorization": f"Bearer {environ['ACCESS_TOKEN']}"
}
def delete_transaction(transaction_id: int):
# params = { "id": transaction_id}
res = requests.delete(url + f"/v1/transactions/{transaction_id}", headers=headers)
if res.status_code == 204:
print(f'Successfully deleted transaction {transaction_id}')
else:
print(f'Failed to delete transaction ({transaction_id}) ')
today = datetime.now()
monthrange = calendar.monthrange(today.year, 1)
firstday = '2024-01-01' # today.replace(day=1, month=1).strftime("%Y-%m-%d")
lastday = '2024-10-31' # today.replace(day=monthrange[1], month=1).strftime("%Y-%m-%d")
params = {
"limit": 5000,
"page": 1,
"start": str(firstday),
"end": str(lastday),
"type": "all"
}
res = requests.get(url + "/v1/transactions", headers=headers, params=params)
transactions = []
if res.status_code == 200:
print("successfully received transactions")
response = json.loads(res.content)
for row in response['data']:
if 'attributes' not in row and len(row['attributes']) == 0:
continue
attributes = row['attributes']
if 'transaction' not in attributes and len(attributes['transactions']) == 0:
continue
transactions.extend([Transaction(t) for t in attributes['transactions']])
data = pd.DataFrame([ t.as_dict() for t in transactions ])
print(f'created {data.shape[0]} data sets')
duplicates = data[data.duplicated(keep=False, subset=['amount', 'date', 'description'])]
print(f'found {duplicates.shape[0]} rows with an sum amount of {duplicates['amount'].sum()}')
if duplicates.shape[0] == 0:
print("no duplicates found")
exit(0)
for duplicate in duplicates.loc:
delete_transaction(duplicate.id)
else:
print(f"failed with status code={res.status_code}") The from datetime import datetime
from typing import List
class Transaction:
def __init__(self, json_data):
self.validate(['date', 'amount', 'notes', 'description', 'external_id'], json_data)
self._date = json_data['date']
self._amount = json_data['amount']
self._notes = json_data['notes']
self._description = json_data['description']
self._id = json_data['external_id']
def validate(self, properties: List[str], json_data):
for property in properties:
if property not in json_data:
raise AttributeError(property)
@property
def date(self) -> datetime:
return datetime.fromisoformat(self._date)
@property
def amount(self) -> float:
return float(self._amount)
@property
def notes(self) -> str:
return str(self._notes)
@property
def desctiption(self) -> str:
return str(self._description)
@property
def id(self) -> int:
return int(self._id)
def as_dict(self):
return {'date': self.date, 'amount': self.amount, 'notes': self.notes, 'description': self._description, 'id': self._id} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can use the "id" field. Its the one not under attributes. |
Beta Was this translation helpful? Give feedback.
You can use the "id" field. Its the one not under attributes.