forked from peters-josh/spending-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·61 lines (46 loc) · 1.84 KB
/
app.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
import re
from flask import Flask, jsonify, request
from sheets import Sheets
import constants
app = Flask(__name__)
@app.route("/budget/api/email", methods=["POST"])
def parse_email():
payload = request.json
body = payload["body"]
subject = payload["subject"]
sheet = Sheets()
print(repr(body))
if "You paid" in subject:
description = extract("(?<=\) \\n \\n)(.*)(?= \\n \ T)", body)
amount = extract("(?<=\- \$)(.*)(?= \\n \\n L)", body)
category = assign_cat_venmo(description)
method = "Vemno"
sheet.add_expense(amount, description, category, method)
elif "charge request" in subject:
description = extract("(?<=\-5\) \\n \\n)(.*)(?= \\n T)", body)
amount = extract("(?<=\- \$)(.*)(?= \\n \\n L)", body)
category = assign_cat_venmo(description)
method = "Vemno"
sheet.add_expense(amount, description, category, method)
elif "Your Single Transaction Alert from Chase" in subject:
description = extract("(?<= at )(.*)(?= has been authorized)", body)
amount = extract("(?<=charge\ of\ \(\$USD\) )(.*)(?=\ at)", body)
category = assign_cat_card(description)
method = "Chase"
sheet.add_expense(amount, description, category, method)
return jsonify({"success": "true"})
def extract(regex, body):
result = re.search(regex, body)
return "NOT FOUND" if result is None else result.group()
def assign_cat_venmo(description):
for venmo in constants.VENMOS.keys():
if venmo in description.lower():
return constants.VENMOS[venmo]
return "NOT FOUND"
def assign_cat_card(description):
for store in constants.STORES.keys():
if store in description.lower():
return constants.STORES[store]
return "NOT FOUND"
if __name__ == "__main__":
app.run()