Skip to content

Instantly share code, notes, and snippets.

@ardrian
Last active February 8, 2018 07:30
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 ardrian/ab5a71344d1c08ae31f8bb99eebcf14e to your computer and use it in GitHub Desktop.
Save ardrian/ab5a71344d1c08ae31f8bb99eebcf14e to your computer and use it in GitHub Desktop.
Update Splitwise group expenses
# This script wasn't completed. There is an authentication error returned when we attempt to update
# a transaction. The strange thing is that we are allowed to retrieve items with no problems,
# also if we send an invalid write, we receive an error response which makes sense (i.e. when
# Sum(user_costs) != expense_cost
#
# If we were finishing this script we would need to do the following:
# 1) Use a proper exchange rate
# 2) Detect when currency == JPY, only covert then
# 3) Update the currency value to AUD after the conversion
# 4) Some kind of logging in case we mess things up
from requests_oauthlib import OAuth1Session
import requests
from requests_oauthlib import OAuth1
consumer_key = "<snip>"
consumer_secret = "<snip>"
request_token_url = "https://secure.splitwise.com/oauth/request_token"
access_token_url = "https://secure.splitwise.com/oauth/access_token"
autorize_url = "https://secure.splitwise.com/oauth/authorize"
def use_keys():
print("using keys")
resource_owner_key = "<snip>"
resource_owner_secret = "<snip>"
oauth = OAuth1Session(consumer_key,
client_secret=consumer_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
signature_type='auth_header')
r = oauth.get('https://secure.splitwise.com/api/v3.0/test')
print(r)
print(r.json())
return oauth
def get_keys():
# Get Access Token
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)
fetch_response = oauth.fetch_request_token(request_token_url)
print(fetch_response)
# User Authorisation
resource_owner_key = fetch_response.get('oauth_token')
resource_owner_secret = fetch_response.get('oauth_token_secret')
print ("Resource owner key: {}".format(resource_owner_key))
print ("Secret: {}".format(resource_owner_secret))
authorization_url = oauth.authorization_url(autorize_url)
print 'Please go here and authorize,', authorization_url
oauth_verifier = raw_input("Paste the oauth_verifier from Splitwise here: ")
# Get Access Token
oauth = OAuth1Session(consumer_key,
client_secret=consumer_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=oauth_verifier)
oauth_tokens = oauth.fetch_access_token(access_token_url)
print("oauth_tokens: {}".format(oauth_tokens))
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')
print("Save these keys for later:")
print("resource_owner_key : {}".format(resource_owner_key))
print("resource_owner_secret : {}".format(resource_owner_secret))
r = oauth.get('https://secure.splitwise.com/api/v3.0/test')
print(r)
print(r.json())
return oauth
def find_group_with_name(oauth, name):
r = oauth.get('https://secure.splitwise.com/api/v3.0/get_groups')
json_response = r.json()
groups = json_response['groups']
for item in groups:
print(item['name'])
if item['name'] == name:
print("Found Match")
return item
print("Error: No group found with name: {}".format(name))
return ""
def update_expense(oauth, expense):
print(expense)
print("Cost: " + expense['cost'])
print("Name: " + expense['description'])
users = expense['users']
expense_multiplier = 2.0
updated_users = []
# Update contributions for each user
for user in users:
paid_share = float(user['paid_share'])
owed_share = float(user['owed_share'])
new_paid_share = paid_share * expense_multiplier
new_owed_share = owed_share * expense_multiplier
print ("User ID: {} Paid: {} Owed: {} Net Balance: {}".format(user['user_id'],user['paid_share'],user['owed_share'],user['net_balance']))
new_user = {'user_id':user['user_id'], 'paid_share':new_paid_share,'owed_share':new_owed_share}
updated_users.append(new_user)
url = "https://secure.splitwise.com/api/v3.0/update_expense/{}".format(expense['id'])
print("Posting to {}".format(url))
# Multipy overall cost
cost = float(expense['cost'])
new_cost = expense_multiplier * cost
data = {'cost' : new_cost, 'users' : updated_users}
print("\nNew Data: {}".format(data))
response = oauth.post(url,data)
print(response)
print(response.json())
def get_expenses_for_group(oauth,group_id):
print("Get Expenses for Group: {}".format(group_id))
url = 'https://secure.splitwise.com/api/v3.0/get_expenses?group_id={}&limit=0'.format(group_id)
r = oauth.get(url)
expenses = r.json()['expenses']
print ("Group contains {} expenses".format(len(expenses)))
expense = expenses[0]
update_expense(oauth, expense)
def main():
print("Get Keys")
#oauth = get_keys()
oauth = use_keys()
group_name = "Niseko 2016"
group = find_group_with_name(oauth,group_name)
print(group)
get_expenses_for_group(oauth,group['id'])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment