-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.py
141 lines (122 loc) · 5.08 KB
/
auth.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# splitwise_importer - a simple TUI program to export your bank log to Splitwise
# Copyright (C) 2021 Dmitry Frolov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# DISCLAIMER:
# This is not an official API. All the trademarks and copyright belongs to Splitwise.com
#!/usr/bin/env python3
import requests
import yaml
import os,sys
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0,CURR_DIR+'/splitwise')
from splitwise import *
from splitwise.user import ExpenseUser
#import iso18245
import pandas
import math
from time import strftime,sleep
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
# main class with Splitwise object
class splitwiseConnector:
def __init__(self):
# load config file
with open(CURR_DIR+'/config.yaml') as f:
config = yaml.safe_load(f)
self.config = Struct(**config)
self.layout = Struct(**self.config.bank_layout)
# load bank-splitwise category correspondence dictionary
with open(CURR_DIR+'/mccDic.yaml') as f:
self.mccDic = yaml.safe_load(f)
self.sObj = Splitwise(self.config.consumer_key,self.config.consumer_secret,api_key=self.config.api_key)
#['Utilities','Uncategorized','Entertainment','Food and drink','Home','Transportation','Life']
cats = self.sObj.getCategories()
self.cats=cats
# create dict with splitwise category objects and their names
self.catDic = {}
for cat in cats:
for subcat in cat.getSubcategories():
self.catDic[subcat.name] = subcat
def parseEntry(self,entry):
parsedEntry = Struct()
if self.layout.type_col != -1:
parsedEntry.type = entry[self.layout.type_col]
else:
parsedEntry.type = None
# identify Expense type
if parsedEntry.type in self.mccDic:
# return splitwise category name
catName = self.mccDic[parsedEntry.type]
else:
catName = "General"
# set category object
parsedEntry.category = self.catDic[catName]
# set date
ts = pandas.to_datetime(entry[self.layout.date_col],dayfirst=True)
parsedEntry.date = strftime("%d/%m/%Y",ts.timetuple())
parsedEntry.description = entry[self.layout.comment_col]
# round cost
if self.layout.negative_payments:
parsedEntry.paid = round(-1.*(entry[self.layout.sum_col]),2)
else:
parsedEntry.paid = round((entry[self.layout.sum_col]),2)
return parsedEntry
def createEqualExpenseFromEntry(self,entry):
# create an equally split expense
# entry - pandas.DataFrame or Series object
parsedEntry = self.parseEntry(entry)
expense = Expense()
expense.setCost(parsedEntry.paid)
expense.setDescription(parsedEntry.description)
expense.setCategory(parsedEntry.category)
expense.setGroupId(self.config.group_id)
expense.setSplitEqually()
expense.setCurrencyCode(self.config.currency_code)
expense.setDate(parsedEntry.date)
# calculate equally split share
members = self.sObj.getGroup(self.config.group_id).getMembers()
share = round(parsedEntry.paid/len(members),2)
# account for any rounding errors
# to prevent errors when total OwedShare is not equal to cost
mainShare = parsedEntry.paid - (len(members)-1)*share
# add main user (payer)
user1 = ExpenseUser()
user1.setId(self.sObj.getCurrentUser().getId())
user1.setPaidShare(parsedEntry.paid)
user1.setOwedShare(mainShare)
users = []
users.append(user1)
# add all other users in group
for i in members:
if i.getId() != user1.getId():
user = ExpenseUser()
user.setId(i.getId())
user.setPaidShare('0.00')
user.setOwedShare(share)
users.append(user)
expense.setUsers(users)
expense.setSplitEqually()
# create expense on Splitwise and return errors object
expense, errors = self.sObj.createExpense(expense)
return errors
# def createEqualExpenseFromEntryDummy(self,entry):
# import random
# if random.randint(0,1):
# error = SplitwiseError("This is a dummy error fo tests")
# else:
# error = SplitwiseError()
# sleep(0.3)
# return error