master
parent
48f49e9b3a
commit
9dc2c7ecac
29
scrape.go
29
scrape.go
|
|
@ -83,13 +83,38 @@ func containsAny(a string, b ...string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *chaseScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
func (c *chaseScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
||||||
transactions, err := c.scrape2021(m)
|
if transactions, err := c.scrape2025Balance(m); err == nil && len(transactions) > 0 {
|
||||||
if err == nil && len(transactions) > 0 {
|
return transactions, err
|
||||||
|
}
|
||||||
|
if transactions, err := c.scrape2021(m); err == nil && len(transactions) > 0 {
|
||||||
return transactions, err
|
return transactions, err
|
||||||
}
|
}
|
||||||
return c.scrape2020(m)
|
return c.scrape2020(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *chaseScraper) scrape2025Balance(m *mail.Message) ([]*Transaction, error) {
|
||||||
|
re := regexp.MustCompile(`^Your.*balance is \$[0-9,\.]*$`)
|
||||||
|
if !re.Match([]byte(m.Header["Subject"][0])) {
|
||||||
|
return nil, errors.New("no match subject search")
|
||||||
|
}
|
||||||
|
subject := m.Header["Subject"][0]
|
||||||
|
|
||||||
|
fields := strings.Fields(subject)
|
||||||
|
amount := fields[len(fields)-1]
|
||||||
|
amount = strings.TrimLeft(amount, "$")
|
||||||
|
amount = strings.ReplaceAll(amount, ",", "")
|
||||||
|
|
||||||
|
b, err := ioutil.ReadAll(m.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
re = regexp.MustCompile(`>Chase = [^<]*`)
|
||||||
|
account := strings.TrimPrefix(string(re.Find(b)), ">Chase = ")
|
||||||
|
|
||||||
|
return []*Transaction{NewTransaction(account, amount, "*", fmt.Sprint(m.Header["Date"]), Chase)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *chaseScraper) scrape2021(m *mail.Message) ([]*Transaction, error) {
|
func (c *chaseScraper) scrape2021(m *mail.Message) ([]*Transaction, error) {
|
||||||
if t, err := c.scrape2021Payment(m); err == nil {
|
if t, err := c.scrape2021Payment(m); err == nil {
|
||||||
return t, err
|
return t, err
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,39 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestScrapeChase2025Balance(t *testing.T) {
|
||||||
|
message := &mail.Message{
|
||||||
|
Header: map[string][]string{
|
||||||
|
"Subject": []string{"Your Chase Freedom Unlimited balance is $1,029.08"},
|
||||||
|
},
|
||||||
|
Body: bytes.NewReader([]byte(`
|
||||||
|
TODO
|
||||||
|
`)),
|
||||||
|
}
|
||||||
|
|
||||||
|
chase := &chaseScraper{}
|
||||||
|
|
||||||
|
gots, err := chase.scrape2025balance(message)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(gots) != 1 {
|
||||||
|
t.Fatal(gots)
|
||||||
|
}
|
||||||
|
got := gots[0]
|
||||||
|
|
||||||
|
if got.Account != "5876" {
|
||||||
|
t.Fatalf("bad account: %v: %+v", got.Account, got)
|
||||||
|
}
|
||||||
|
if got.Amount != "1029.08" {
|
||||||
|
t.Fatalf("bad amount: %v: %+v", got.Amount, got)
|
||||||
|
}
|
||||||
|
if got.Vendor != "*" {
|
||||||
|
t.Fatalf("bad vendor: %v: %+v", got.Vendor, got)
|
||||||
|
}
|
||||||
|
t.Logf("%+v", got)
|
||||||
|
}
|
||||||
|
|
||||||
func TestScrapeChase202112Payment(t *testing.T) {
|
func TestScrapeChase202112Payment(t *testing.T) {
|
||||||
b, err := ioutil.ReadFile("./testdata/chase.2021.12.payment.txt")
|
b, err := ioutil.ReadFile("./testdata/chase.2021.12.payment.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue