-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonzo.py
79 lines (59 loc) · 2.79 KB
/
monzo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Contract
# Give me a price, date, text, and email links
# I upload these if they match a debit from the monzo account (date close and price match)
# Fails in the event of multiple matches, returns false
# Download transactions around the date of the data
# Match price, if one match succeed, if multiple pick closest date, if still multiple fail
# Upload data - text & link in notes, (email screenshot as receipt?)
# pipenv install requests
import uuid
import requests
#
import json
#
from datetime import timedelta, datetime
from main import ReceiptsClient
import main
import receipt_types
def get_transactions( ACCOUNT_ID, ACCESS_TOKEN):
r = requests.get('https://api.monzo.com/transactions?expand[]=merchant&account_id='+ACCOUNT_ID, headers={'Authorization': 'Bearer '+ACCESS_TOKEN})
raw_transactions = json.loads(r.text)["transactions"]
transactions = {}
for raw_transaction in raw_transactions :
transactions[str(raw_transaction['amount'])] = []
for raw_transaction in raw_transactions :
transactions[str(raw_transaction['amount'])].append(raw_transaction)
return transactions
#price integer pennies, date is datetime, text is string, link is string
def match_and_upload_receipt(price, date, text, link, transactions, ACCESS_TOKEN):
#get all transactions
#http "https://api.monzo.com/transactions" \
"Authorization: Bearer $access_token" \
"account_id==$account_id"
if not str(-abs(price)) in transactions:
return -1
candidates = list(transactions[str(-abs(price))])
#print("candidates" + str(candidates))
#print("firstcandidates" + str(candidates[0]))
if len(candidates) == 0:
print("no candidates")
return -1
#candidate = min(candidates, key=lambda x: (datetime.fromisoformat(x['created']) - date).total_seconds())
candidate = candidates[0]
before = date + timedelta(days=7)
since = date - timedelta(days=7)
if not (True):
#fix the time thing
return
#print("found candidate: " + str(candidate))
# Using a random receipt ID we generate as external ID
receipt_id = uuid.uuid4().hex
example_items = [receipt_types.Item(text+" "+link, 1, "", abs(candidate["amount"]), "GBP", 0, [])]
example_receipt = receipt_types.Receipt("", receipt_id, candidate["id"],
abs(candidate["amount"]), "GBP", "", "", example_items)
example_receipt_marshaled = example_receipt.marshal()
#print(date.isoformat() + " " + str(example_receipt_marshaled))
client = requests.put("https://api.monzo.com/transaction-receipts/", data=example_receipt_marshaled, headers={'Authorization': 'Bearer '+ACCESS_TOKEN})
return 1
# test
#match_and_upload_receipt(-1010, datetime(2019,1,2),"Testing testing 1 2 3 receipt muncher","downloadmoreram.com")