Impl uccu

master v0.5
Bel LaPointe 2020-07-13 14:55:20 -06:00
parent 4006975a21
commit f32fb5aad1
2 changed files with 35 additions and 0 deletions

View File

@ -5,6 +5,7 @@ type Bank int
const (
Chase Bank = iota + 1
Citi Bank = iota + 1
UCCU Bank = iota + 1
)
func (b Bank) String() string {
@ -13,6 +14,8 @@ func (b Bank) String() string {
return "Chase"
case Citi:
return "Citi"
case UCCU:
return "UCCU"
}
return "?"
}

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/mail"
"regexp"
"strconv"
"strings"
)
@ -16,6 +17,7 @@ type scraper interface {
type chaseScraper struct{}
type citiScraper struct{}
type uccuScraper struct{}
func Scrape(m *mail.Message) ([]*Transaction, error) {
scraper, err := buildScraper(m)
@ -37,6 +39,9 @@ func buildScraper(m *mail.Message) (scraper, error) {
if strings.Contains(from, "Citi") {
return newCitiScraper(), nil
}
if strings.Contains(from, "Notifications@uccu.com") {
return newUCCUScraper(), nil
}
return nil, errors.New("unknown sender: " + from)
}
@ -44,6 +49,10 @@ func newChaseScraper() scraper {
return &chaseScraper{}
}
func newUCCUScraper() scraper {
return &uccuScraper{}
}
func newCitiScraper() scraper {
return &citiScraper{}
}
@ -144,3 +153,26 @@ func (c *citiScraper) scrape(m *mail.Message) ([]*Transaction, error) {
return transactions, nil
*/
}
func (c *uccuScraper) scrape(m *mail.Message) ([]*Transaction, error) {
b, err := ioutil.ReadAll(m.Body)
if err != nil {
return nil, err
}
regexp := regexp.MustCompile(`\$([0-9]+,?)+\.[0-9][0-9]`)
match := regexp.Find(b)
if len(match) == 0 {
return nil, fmt.Errorf("no matches found")
}
match = match[1:]
match = bytes.ReplaceAll(match, []byte(","), []byte{})
f, err := strconv.ParseFloat(string(match), 10)
if err != nil {
return nil, err
}
if !bytes.Contains(b, []byte("credit")) {
f *= -1.0
}
transaction := NewTransaction(fmt.Sprintf("%.2f", f), "?", fmt.Sprint(m.Header["Date"]), UCCU)
return []*Transaction{transaction}, nil
}