Skip to content

Instantly share code, notes, and snippets.

@lukas-hetzenecker
Created June 19, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukas-hetzenecker/c94588053e71517986d6ef43d353012d to your computer and use it in GitHub Desktop.
Save lukas-hetzenecker/c94588053e71517986d6ef43d353012d to your computer and use it in GitHub Desktop.
from splitwise import Splitwise
from splitwise.expense import Expense
from splitwise.user import ExpenseUser
from decimal import Decimal
import csv
userMapping = [3249451, 44166785, 14661289, 4758492, 52240291, 52106172, 45456543, 52106173]
s = Splitwise("***","***", api_key="***")
current = s.getCurrentUser()
print("Importing as user " + str(current.email))
with open('/home/lukas/Downloads/Costs - Luki.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile)
# Ignore first row
next(csvreader)
next(csvreader)
for row in csvreader:
print(row)
total_cost = str(sum([Decimal(c[1:].replace(',','')) for c in row[-len(userMapping):]]))
expense = Expense()
expense.setGroupId(33624135)
expense.setCost(total_cost)
expense.setCurrencyCode('EUR')
expense.setDescription(row[1])
for idx, cost in enumerate(row[-len(userMapping):]):
user = ExpenseUser()
user.setId(userMapping[idx])
if userMapping[idx] == current.id:
user.setPaidShare(total_cost)
else:
user.setPaidShare('0.00')
user.setOwedShare(cost[1:].replace(',',''))
expense.addUser(user)
nExpense, errors = s.createExpense(expense)
if errors is not None:
print(errors.errors)
print(nExpense.getId())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment