1 |
# Shell MasterCard |
2 |
# |
3 |
# Douglas Thrift |
4 |
# |
5 |
# $Id$ |
6 |
|
7 |
import bank |
8 |
from ClientForm import FormParser |
9 |
from datetime import datetime |
10 |
import decimal |
11 |
from mechanize import LinkNotFoundError |
12 |
import re |
13 |
import website |
14 |
|
15 |
class _FormParser(FormParser): |
16 |
def __init__(self, *args, **kwargs): |
17 |
FormParser.__init__(self, *args, **kwargs) |
18 |
|
19 |
def do_input(self, attributes): |
20 |
self.__enable(attributes) |
21 |
FormParser.do_input(self, attributes) |
22 |
|
23 |
def start_select(self, attributes): |
24 |
self.__name = None |
25 |
|
26 |
for name, value in attributes: |
27 |
if name == 'name': |
28 |
self.__name = value |
29 |
|
30 |
self.__enable(attributes) |
31 |
FormParser.start_select(self, attributes) |
32 |
|
33 |
def end_select(self): |
34 |
if self.__name == 'download_date': |
35 |
self.do_option([('value', 'Activity Since Last Statement')]) |
36 |
elif self.__name == 'download_format': |
37 |
self.do_option([('value', 'QIF')]) |
38 |
|
39 |
FormParser.end_select(self) |
40 |
|
41 |
def __enable(self, attributes): |
42 |
try: |
43 |
attributes.remove(('disabled', 'disabled')) |
44 |
except ValueError: |
45 |
pass |
46 |
|
47 |
class Bank(website.Website, bank.Bank): |
48 |
PAYMENT_DUE = re.compile(r'([A-Z][a-z]{2})\.?( \d{1,2}, \d{4})') |
49 |
|
50 |
def __init__(self, config, debug): |
51 |
website.Website.__init__(self, debug, factory = website.Factory(_FormParser)) |
52 |
bank.Bank.__init__(self, config) |
53 |
|
54 |
self.browser.open('http://www.shellmc.accountonline.com/') |
55 |
self.browser.select_form(name = 'LOGIN') |
56 |
|
57 |
self.browser['USERNAME'] = self._username() |
58 |
self.browser['PASSWORD'] = self._password() |
59 |
|
60 |
self.browser.submit() |
61 |
|
62 |
def __del__(self): |
63 |
try: |
64 |
self.browser.follow_link(text_regex = 'Log Out') |
65 |
except (AttributeError, LinkNotFoundError): |
66 |
pass |
67 |
|
68 |
def download(self, account): |
69 |
try: |
70 |
balance = -decimal.Decimal(str(self.Soup(self._follow_link(text_regex = str(account))).find(attrs = {'class': 'curr_balance'}).string[1:]).translate(None, ',')) |
71 |
|
72 |
self._follow_link(text_regex = 'View/Download Your Statement') |
73 |
self.browser.select_form(name = 'download_statement') |
74 |
|
75 |
self.browser.form.action = 'https://www.accountonline.com/cards/svc/StatementDownload.do?dateRange=unbilled&viewType=qif' |
76 |
self.browser['download_date'] = ['Activity Since Last Statement'] |
77 |
self.browser['download_format'] = ['QIF'] |
78 |
|
79 |
return self._submit().read(), balance |
80 |
finally: |
81 |
self._back() |
82 |
|
83 |
def due(self, account): |
84 |
try: |
85 |
return datetime.strptime(''.join(self.PAYMENT_DUE.search(self.Soup(self._follow_link(text_regex = str(account))).find(text = re.compile('Payment Due')).findNext(text = self.PAYMENT_DUE)).group(1, 2)), '%b %d, %Y').date() |
86 |
finally: |
87 |
self._back() |