Biblioteca Visa Vale em Python
[cascardo/visavale.git] / vale.py
1 #
2 #  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 3 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License along
15 #  with this program; if not, write to the Free Software Foundation, Inc.,
16 #  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #
18
19 import urllib2
20 import urllib
21 import re
22
23 class VisaVale():
24         def __init__(self, cartao, periodo=0):
25                 self._URL = "http://www.cartoesbeneficio.com.br/inst/convivencia/SaldoExtrato.jsp"
26                 self._form = { 'numeroCartao': cartao, 'periodoSelecionado': periodo }
27                 self._r = re.compile("Saldo disp.*(R\$ [0-9,]*)")
28                 self._d = re.compile("<td.*>(../..)</td>")
29                 self._i = re.compile("<td.*400px.*>(.*)</td>")
30                 self._v = re.compile("<td.*>(R\$.*)</td>")
31         def _normalize(self, value):
32                 _int = re.search("([0-9]*),", value).group(1)
33                 _dec = re.search(",([0-9]*)", value).group(1)
34                 return int(_int) + int(_dec) / 100.00
35         def run(self):
36                 self._h = urllib2.urlopen(self._URL, urllib.urlencode(self._form), 15)
37                 self._s = self._h.read()
38                 self._saldo = self._normalize(self._r.search(self._s).group(1))
39                 self._datas = self._d.findall(self._s)[1:]
40                 self._itens = self._i.findall(self._s)[1:]
41                 self._valores = map(self._normalize, self._v.findall(self._s)[:-1])
42         def saldo(self):
43                 return self._saldo
44         def extrato(self):
45                 return zip(self._datas, self._itens, self._valores)