import simplejson import urllib import urllib2 import pprint import time from string import Template verbose = 1 # debug pp = pprint.PrettyPrinter(indent=4) credentialsFilename = '/home/ilia/byob/credentials.json' credentials = simplejson.load(open(credentialsFilename, 'r')) def getCredentialsForBank(bankId): return credentials[bankId.upper()] def getAccountsForBank(bankId): # send request to json-rpc server to get accounts. # post code taken from http://www.voidspace.org.uk/python/articles/urllib2.shtml#fetching-urls #url = 'http://macbook.lan.nurey.com:8080/Finance/Bank/JSONFactory' url = 'http://localhost:8080/Finance/Bank/JSONFactory' credentials = getCredentialsForBank(bankId) cachePath = '/home/ilia/byob/pages/cache' values = { 'params': [ { 'bank_id': bankId, 'card_number': credentials["CARD_NUMBER"], 'password': credentials["PASSWORD"], 'cache_path': cachePath } ], 'method': 'accounts', 'id': 'jsonRpcPyClient' } valuesJson = simplejson.dumps(values) data = valuesJson #data = urllib.urlencode(valuesJson) #pp.pprint(data) #debug req = urllib2.Request(url, data) response = urllib2.urlopen(req) # get the response object responseJson = response.read() #pp.pprint(response_json) #debug responseDict = simplejson.loads(responseJson) return responseDict['result'] def getAccountLine(account): """ >>> getAccountLine({ 'name': 'test name', 'available': '0', 'posted': '0' }) 'test name - 0 - 0' """ accountLine = Template('$name - $available - $posted') return accountLine.substitute(dict(account)) def getAccountBalance(): text = '' for bankId in ['pc', 'cibc-nurey', 'cibc-ilia']: accounts = getAccountsForBank(bankId) text += "%s:\n" % bankId for account in accounts: text += getAccountLine(account) + "\n" return text def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test() print getAccountBalance()