Skip to content

Commit

Permalink
adding filtering, more structure, packages and lots of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lookfwd committed Mar 14, 2012
1 parent aa10703 commit e5fbf3c
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 30 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ Some account examples:

A complete example with multiple accounts and chaining:

from alpha import Alpha
from eurobank import Eurobank
from ConfigParser import RawConfigParser
import grbanks

config = RawConfigParser()
config.read('passwords.cfg')
(Eurobank(config)+Alpha(config)+Alpha(config,name="ALPHAB")).toCsv('summary.csv').printp()
(grbanks.Eurobank(config)+grbanks.Alpha(config)+grbanks.Alpha(config,name="ALPHAB")).printp()

You can save to Excel format with the toCsv method. The Excel format support Greek characters.
You can save to Excel format with the toCsv method. The Excel format support Greek characters. You can also perform arbitrary filtering, chaining and a custom formating

If you would like to avoid using files (for security reasons) you might find the following examples useful:
(a+e).printp().toCsv("ae.csv")
(a+e).printp().toCsv("ae.csv")
(a+e).filter(grbanks.FILTER_POSITIVE).printp().toCsv("ae.csv")
(a+e).format(grbanks.FORMAT_SUPERSIZE_ME).filter(grbanks.FILTER_POSITIVE).printp().toCsv("ae.csv")
(a+e).format(grbanks.FORMAT_SUPERSIZE_ME).filter(lambda row: float(row['amount'])>300).printp().toCsv("ae.csv")

You might want to use gpg to encrypt your account info data:

# See here on how to run gpg: http://www.madboa.com/geek/gpg-quickstart/
# See here if you have problem generating random numbers: http://www.howtoforge.com/helping-the-random-number-generator-to-gain-enough-entropy-with-rng-tools-debian-lenny
Expand Down
12 changes: 6 additions & 6 deletions banks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/python

from alpha import Alpha
from eurobank import Eurobank
from ConfigParser import RawConfigParser
import sys
from ConfigParser import RawConfigParser
import grbanks

config = RawConfigParser()

Expand All @@ -12,13 +11,14 @@
else:
config.read('passwords.cfg')

a = Alpha(config)
e = Eurobank(config)
a = grbanks.Alpha(config)
e = grbanks.Eurobank(config)

a.printp()

print "================================================="
e.printp()

print "================================================="
(a+e).printp(True)

(a+e).filter(grbanks.FILTER_POSITIVE).printp()
4 changes: 4 additions & 0 deletions grbanks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from alpha import Alpha
from eurobank import Eurobank
from utils import *
from basebank import BaseBank
11 changes: 6 additions & 5 deletions alpha.py → grbanks/alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, user=None, passw=None, acnt=None, name="ALPHA"):
super(Alpha, self).__init__(name)
if user!=None: self.load(user,passw,acnt)

def load(self, user, passw=None, acnt=None):
def _load(self, user, passw=None, acnt=None):
(user,passw,acnt)=self.manage_up(user,passw,acnt)

(s,v)=self.openUrl('https://secure.alpha.gr/e-services/')
Expand Down Expand Up @@ -71,10 +71,11 @@ def nophone(adr):
for r in v.xpath("//table[@class='ResultTable']//tr[@bgcolor]")]

#Pack transactions to the table
self.left = rep[len(rep)-1]['total']
self.table = zip(
left = rep[len(rep)-1]['total']
table = zip(
map(lambda s:time.strptime(s['date'],'%d/%m/%Y'), rep),
[self.name]*len(rep),
map(lambda s:s['desc'], rep),
map(lambda s:s['amount'], rep)
map(lambda s:s['amount'], rep),
map(lambda s:s['desc'], rep)
)
return left, table
46 changes: 37 additions & 9 deletions basebank.py → grbanks/basebank.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import unicode_excel_write
import urllib, urllib2, cookielib
from decimal import *
from lxml import html
import unicode_excel_write
import code, time
from utils import *

class BaseBank(object):

def __init__(self, name):
self.opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
Expand All @@ -16,6 +19,7 @@ def __init__(self, name):
)
self.opener.addheaders = [('User-agent', 'Mozilla/5.0')]
self.name = name
self.__format = FORMAT_DEFAULT

def openUrl(self, url, params=None):
if params==None:
Expand All @@ -27,6 +31,12 @@ def openUrl(self, url, params=None):
g.close()
return (s,v)


def load(self, user, passw=None, acnt=None):
(self.left, self.table) = self._load(user, passw, acnt)
self.left = Decimal(self.left)
self.table = map(lambda s: {'date':s[0], 'name':s[1], 'amount':Decimal(s[2]), 'description':s[3]}, self.table)

def manage_up(self, user, passw, acnt):
if passw==None:
# in this case the first argument is a configuration file
Expand All @@ -35,23 +45,41 @@ def manage_up(self, user, passw, acnt):
user=user.get(self.name,'user')
return (user, passw, acnt)

def toCsv(self, filename, posOnly=False):
def toCsv(self, filename):
fo = open(filename, 'wb')
g = unicode_excel_write.UnicodeWriter(fo)
g.writerow(['date','account','amount','description'])
g.writerows([(time.strftime('%d/%m/%Y',a),b,d,c) for (a,b,c,d) in self.table if not (posOnly and d.startswith('-'))])
g.writerow(["total: ","",self.left])
g.writerows([self.__format(row) for row in self.table])
g.writerow(["total: ","",self.__format(self.left, Type.CURRENCY)])
fo.close()
return self

def printp(self, posOnly=False):
print "\n".join([("%s %s %12s %s" % (time.strftime('%d/%m/%Y',a),b,d,c.encode('utf-8'))) for (a,b,c,d) in self.table if not (posOnly and d.startswith('-'))])
def printp(self):
print "\n".join(["%s %s %12s %s" % self.__format(row) for row in self.table]).encode('utf-8')
print "-------------------------------------------------"
print " total: %4s" % self.left
print " total: %4s" % self.__format(self.left, Type.CURRENCY)
return self

def __clone(self):
b = BaseBank(self.name)
b.__format = self.__format
b.left = self.left
b.table = self.table
return b

def format(self, format=FORMAT_DEFAULT):
b = self.__clone()
b.__format = format
return b

def filter(self, filter=FILTER_ALL):
b = self.__clone()
b.table = [row for row in b.table if filter(row)]
return b

def __add__(self, other):
b = BaseBank("%s_%s"%(self.name,other.name))
b.left = "%3.2f" % (float(self.left)+float(other.left))
b.table = sorted(self.table+other.table)
b.__format = self.__format
b.left = self.left+other.left
b.table = sorted(self.table+other.table, key=lambda s: s['date'])
return b
12 changes: 7 additions & 5 deletions eurobank.py → grbanks/eurobank.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, user=None, passw=None, acnt=None, name="EUROB"):
super(Eurobank, self).__init__(name)
if user!=None: self.load(user,passw,acnt)

def load(self, user, passw=None, acnt=None):
def _load(self, user, passw=None, acnt=None):
(user,passw,acnt)=self.manage_up(user,passw,acnt)

(s,v)=self.openUrl('https://ebanking.eurobank.gr/ebanking/login.faces')
Expand All @@ -30,12 +30,14 @@ def load(self, user, passw=None, acnt=None):

(s,v)=self.openUrl("https://ebanking.eurobank.gr/ebanking/cashmanagement/accounts.faces?n=%s&ic=1"%acnt)

self.left = v.xpath('//table[@class="fldgrp lft cash"]/tbody/tr[4]/td[2]/text()')[0].replace(',','.')
left = v.xpath('//table[@class="fldgrp lft cash"]/tbody/tr[4]/td[2]/text()')[0].replace(',','.')

date = [time.strptime(g,'%d/%m/%Y') for g in v.xpath('//table[@id="accountTransactionsTable"]/tbody/tr/td[1]/a/text()')]
self.table = zip(
table = zip(
date,
[self.name]*len(date),
v.xpath('//table[@id="accountTransactionsTable"]/tbody/tr/td[3]/text()'),
[g.replace(',','.') for g in v.xpath('//table[@id="accountTransactionsTable"]/tbody/tr/td[4]/span/text()')]
[g.replace(',','.') for g in v.xpath('//table[@id="accountTransactionsTable"]/tbody/tr/td[4]/span/text()')],
v.xpath('//table[@id="accountTransactionsTable"]/tbody/tr/td[3]/text()')
)[::-1]

return left, table
File renamed without changes.
25 changes: 25 additions & 0 deletions grbanks/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import time
from decimal import *

FILTER_ALL = lambda row: True

FILTER_POSITIVE = lambda row: row['amount']>0

FILTER_NEGATIVE = lambda row: row['amount']<0

class Type:
ROW=1
CURRENCY=2

def format_day(row):
return time.strftime('%d/%m/%Y',row['date'])

FORMAT_DEFAULT = lambda row, type = Type.ROW: (format_day(row), row['name'], row['amount'], row['description']) if type == Type.ROW else row

def FORMAT_SUPERSIZE_ME(row, type = Type.ROW):
supersize_me = lambda x: str(Decimal(x)*100)
if type == Type.ROW:
return (format_day(row), row['name'], supersize_me(row['amount']), row['description'])
else:
return supersize_me(row)

0 comments on commit e5fbf3c

Please sign in to comment.