Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f32fb5aad1 | ||
|
|
4006975a21 |
3
bank.go
3
bank.go
@@ -5,6 +5,7 @@ type Bank int
|
|||||||
const (
|
const (
|
||||||
Chase Bank = iota + 1
|
Chase Bank = iota + 1
|
||||||
Citi Bank = iota + 1
|
Citi Bank = iota + 1
|
||||||
|
UCCU Bank = iota + 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b Bank) String() string {
|
func (b Bank) String() string {
|
||||||
@@ -13,6 +14,8 @@ func (b Bank) String() string {
|
|||||||
return "Chase"
|
return "Chase"
|
||||||
case Citi:
|
case Citi:
|
||||||
return "Citi"
|
return "Citi"
|
||||||
|
case UCCU:
|
||||||
|
return "UCCU"
|
||||||
}
|
}
|
||||||
return "?"
|
return "?"
|
||||||
}
|
}
|
||||||
|
|||||||
104
scrape.go
104
scrape.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ type scraper interface {
|
|||||||
|
|
||||||
type chaseScraper struct{}
|
type chaseScraper struct{}
|
||||||
type citiScraper struct{}
|
type citiScraper struct{}
|
||||||
|
type uccuScraper struct{}
|
||||||
|
|
||||||
func Scrape(m *mail.Message) ([]*Transaction, error) {
|
func Scrape(m *mail.Message) ([]*Transaction, error) {
|
||||||
scraper, err := buildScraper(m)
|
scraper, err := buildScraper(m)
|
||||||
@@ -37,6 +39,9 @@ func buildScraper(m *mail.Message) (scraper, error) {
|
|||||||
if strings.Contains(from, "Citi") {
|
if strings.Contains(from, "Citi") {
|
||||||
return newCitiScraper(), nil
|
return newCitiScraper(), nil
|
||||||
}
|
}
|
||||||
|
if strings.Contains(from, "Notifications@uccu.com") {
|
||||||
|
return newUCCUScraper(), nil
|
||||||
|
}
|
||||||
return nil, errors.New("unknown sender: " + from)
|
return nil, errors.New("unknown sender: " + from)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +49,10 @@ func newChaseScraper() scraper {
|
|||||||
return &chaseScraper{}
|
return &chaseScraper{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newUCCUScraper() scraper {
|
||||||
|
return &uccuScraper{}
|
||||||
|
}
|
||||||
|
|
||||||
func newCitiScraper() scraper {
|
func newCitiScraper() scraper {
|
||||||
return &citiScraper{}
|
return &citiScraper{}
|
||||||
}
|
}
|
||||||
@@ -84,41 +93,86 @@ func (c *chaseScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *citiScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
func (c *citiScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
||||||
|
date := fmt.Sprint(m.Header["Date"])
|
||||||
b, err := ioutil.ReadAll(m.Body)
|
b, err := ioutil.ReadAll(m.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
targetLineRegexp := regexp.MustCompile(`Account #: XXXX[0-9]{4} .*`)
|
re := regexp.MustCompile(`Citi Alert: A \$[0-9][0-9]*\.[0-9][0-9] transaction was made at .* on card ending in`)
|
||||||
targetMatches := targetLineRegexp.FindAll(b, -1)
|
match := re.Find(b)
|
||||||
if len(targetMatches) == 0 {
|
if len(match) == 0 {
|
||||||
return nil, errors.New("no lines with transactions found")
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
results := make(map[string][]string)
|
rePrice := regexp.MustCompile(`[0-9][0-9]*\.[0-9][0-9]`)
|
||||||
for _, b := range targetMatches {
|
price := rePrice.Find(match)
|
||||||
// Account #: XXXX3837 $137.87 at AMZN Mktp US Amzn.com/bill WA on 04/03/2020, 09:05 PM ET
|
|
||||||
regexp := regexp.MustCompile(`Account #: XXXX[0-9]{4} \$(?P<amount>[0-9]+\.[0-9]*) at (?P<account>[^,]*)`)
|
vendor := bytes.Split(bytes.Split(match, []byte(" on card ending in"))[0], []byte("transaction was made at "))[1]
|
||||||
matches := regexp.FindSubmatch(b)
|
|
||||||
if len(matches) < 2 {
|
transaction := NewTransaction(string(price), string(vendor), date, Citi)
|
||||||
return nil, fmt.Errorf("no full matches found: %s", b)
|
|
||||||
|
return []*Transaction{transaction}, nil
|
||||||
|
//Citi Alert: A $598.14 transaction was made at REMIX MUSIC SPRINGDA on card ending in 3837
|
||||||
|
/*
|
||||||
|
b, err := ioutil.ReadAll(m.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
for i, name := range regexp.SubexpNames() {
|
targetLineRegexp := regexp.MustCompile(`Account #: XXXX[0-9]{4} .*`)
|
||||||
if i != 0 && name != "" {
|
targetMatches := targetLineRegexp.FindAll(b, -1)
|
||||||
if name == "account" {
|
if len(targetMatches) == 0 {
|
||||||
matches[i] = bytes.Split(matches[i], []byte(" on "))[0]
|
return nil, errors.New("no lines with transactions found")
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make(map[string][]string)
|
||||||
|
for _, b := range targetMatches {
|
||||||
|
// Account #: XXXX3837 $137.87 at AMZN Mktp US Amzn.com/bill WA on 04/03/2020, 09:05 PM ET
|
||||||
|
regexp := regexp.MustCompile(`Account #: XXXX[0-9]{4} \$(?P<amount>[0-9]+\.[0-9]*) at (?P<account>[^,]*)`)
|
||||||
|
matches := regexp.FindSubmatch(b)
|
||||||
|
if len(matches) < 2 {
|
||||||
|
return nil, fmt.Errorf("no full matches found: %s", b)
|
||||||
|
}
|
||||||
|
for i, name := range regexp.SubexpNames() {
|
||||||
|
if i != 0 && name != "" {
|
||||||
|
if name == "account" {
|
||||||
|
matches[i] = bytes.Split(matches[i], []byte(" on "))[0]
|
||||||
|
}
|
||||||
|
results[name] = append(results[name], string(matches[i]))
|
||||||
}
|
}
|
||||||
results[name] = append(results[name], string(matches[i]))
|
}
|
||||||
|
if len(results) != 2 || len(results["amount"]) != len(results["account"]) {
|
||||||
|
return nil, fmt.Errorf("unexpected matches found looking for transactions: %+v", results)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(results) != 2 || len(results["amount"]) != len(results["account"]) {
|
|
||||||
return nil, fmt.Errorf("unexpected matches found looking for transactions: %+v", results)
|
transactions := make([]*Transaction, len(results["amount"]))
|
||||||
|
for i := range results["amount"] {
|
||||||
|
transactions[i] = NewTransaction(results["amount"][i], results["account"][i], fmt.Sprint(m.Header["Date"]), Citi)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
transactions := make([]*Transaction, len(results["amount"]))
|
return transactions, nil
|
||||||
for i := range results["amount"] {
|
*/
|
||||||
transactions[i] = NewTransaction(results["amount"][i], results["account"][i], fmt.Sprint(m.Header["Date"]), Citi)
|
}
|
||||||
}
|
|
||||||
|
func (c *uccuScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
||||||
return transactions, nil
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
0
transaction_test.go
Normal file → Executable file
0
transaction_test.go
Normal file → Executable file
Reference in New Issue
Block a user