amex ready
parent
95bab29a63
commit
e55f7c78aa
11
bank.go
11
bank.go
|
|
@ -4,10 +4,11 @@ type Bank int
|
|||
|
||||
const (
|
||||
Chase Bank = iota + 1
|
||||
Citi Bank = iota + 1
|
||||
UCCU Bank = iota + 1
|
||||
BankOfAmerica Bank = iota + 1
|
||||
Fidelity Bank = iota + 1
|
||||
Citi
|
||||
UCCU
|
||||
BankOfAmerica
|
||||
Fidelity
|
||||
Amex
|
||||
)
|
||||
|
||||
func (b Bank) String() string {
|
||||
|
|
@ -22,6 +23,8 @@ func (b Bank) String() string {
|
|||
return "Citi"
|
||||
case UCCU:
|
||||
return "UCCU"
|
||||
case Amex:
|
||||
return "AmericanExpress"
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
|
|
|
|||
23
config.go
23
config.go
|
|
@ -11,13 +11,12 @@ import (
|
|||
type Uploader int
|
||||
|
||||
const (
|
||||
UploaderTodo = Uploader(iota)
|
||||
DeprecatedUploaderTodo = Uploader(iota)
|
||||
UploaderLedger
|
||||
UploaderPTTodo
|
||||
)
|
||||
|
||||
var uploaders = map[string]Uploader{
|
||||
"todo": UploaderTodo,
|
||||
"ledger": UploaderLedger,
|
||||
"pttodo": UploaderPTTodo,
|
||||
}
|
||||
|
|
@ -28,8 +27,6 @@ type Config struct {
|
|||
EmailIMAP string
|
||||
EmailLimit int
|
||||
TodoAddr string
|
||||
TodoToken string
|
||||
TodoList string
|
||||
TodoTag string
|
||||
Uploader Uploader
|
||||
Storage storage.DB
|
||||
|
|
@ -48,9 +45,9 @@ func NewConfig() Config {
|
|||
as.Append(args.STRING, "emailimap", "email imap", "imap.gmail.com:993")
|
||||
as.Append(args.INT, "emaillimit", "email limit", 0)
|
||||
|
||||
as.Append(args.STRING, "uploader", "todo, ledger, pttodo", "todo")
|
||||
as.Append(args.STRING, "uploader", "ledger|pttodo", "ledger")
|
||||
|
||||
as.Append(args.STRING, "todoaddr", "todo addr", "https://todo-server.remote.blapointe.com")
|
||||
as.Append(args.STRING, "todoaddr", "todo addr", "/tmp/email-xactions-to-todo.dat.txt")
|
||||
as.Append(args.STRING, "todopass", "todo pass", "gJtEXbbLHLf54yS9EdujtVN2n6Y")
|
||||
as.Append(args.STRING, "todotoken", "todo token", "")
|
||||
as.Append(args.STRING, "todolist", "todo list", "")
|
||||
|
|
@ -102,20 +99,6 @@ func NewConfig() Config {
|
|||
}
|
||||
log.Printf("config: %+v", config)
|
||||
|
||||
if config.Uploader == UploaderTodo {
|
||||
token := as.GetString("todotoken")
|
||||
if len(token) == 0 {
|
||||
token = getToken(as)
|
||||
}
|
||||
|
||||
list := as.GetString("todolist")
|
||||
if len(list) == 0 {
|
||||
list = getList(as, token)
|
||||
}
|
||||
config.TodoToken = token
|
||||
config.TodoList = list
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -1,6 +1,6 @@
|
|||
module gitea.inhome.blapointe.com/local/email-xactions-to-todo
|
||||
|
||||
go 1.17
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
gitea.inhome.blapointe.com/local-sandbox/contact v0.0.2-0.20231109150121-14036702ee2a
|
||||
|
|
|
|||
56
scrape.go
56
scrape.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"io/ioutil"
|
||||
"net/mail"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -20,6 +21,7 @@ type bankOfAmericaScraper struct{}
|
|||
type chaseScraper struct{}
|
||||
type citiScraper struct{}
|
||||
type uccuScraper struct{}
|
||||
type amexScraper struct{}
|
||||
|
||||
func Scrape(m *mail.Message, banks map[Bank]bool) ([]*Transaction, error) {
|
||||
scraper, err := buildScraper(m, banks)
|
||||
|
|
@ -50,6 +52,9 @@ func buildScraper(m *mail.Message, banks map[Bank]bool) (scraper, error) {
|
|||
if strings.Contains(from, "Notifications@uccu.com") && banks[UCCU] {
|
||||
return newUCCUScraper(), nil
|
||||
}
|
||||
if strings.Contains(from, "American Express") && banks[Amex] {
|
||||
return newAmexScraper(), nil
|
||||
}
|
||||
return nil, errors.New("unknown sender: " + from)
|
||||
}
|
||||
|
||||
|
|
@ -73,6 +78,10 @@ func newCitiScraper() scraper {
|
|||
return &citiScraper{}
|
||||
}
|
||||
|
||||
func newAmexScraper() scraper {
|
||||
return &amexScraper{}
|
||||
}
|
||||
|
||||
func containsAny(a string, b ...string) bool {
|
||||
for i := range b {
|
||||
if strings.Contains(a, b[i]) {
|
||||
|
|
@ -245,6 +254,53 @@ func (c *uccuScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
|||
return []*Transaction{transaction}, nil
|
||||
}
|
||||
|
||||
func (c *amexScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
||||
b, err := ioutil.ReadAll(m.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bytes.ReplaceAll(b, []byte("=\n"), []byte(""))
|
||||
|
||||
matches := regexp.MustCompile(`\$([0-9]+,?)+\.[0-9][0-9]`).FindAll(b, -1)
|
||||
matches = slices.DeleteFunc(matches, func(match []byte) bool {
|
||||
return string(match) == "$1.00"
|
||||
})
|
||||
if len(matches) == 0 {
|
||||
return nil, fmt.Errorf("no matches found")
|
||||
}
|
||||
match := matches[0]
|
||||
match = match[1:]
|
||||
match = bytes.ReplaceAll(match, []byte(","), []byte{})
|
||||
f, err := strconv.ParseFloat(string(match), 10)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f *= -1.0
|
||||
|
||||
vendors := regexp.MustCompile(`>[A-Z][A-Z ]*<`).FindAll(b, -1)
|
||||
vendors = slices.DeleteFunc(vendors, func(b []byte) bool { return string(b) == ">BREE A LAPOINTE<" })
|
||||
vendor := "*"
|
||||
if len(vendors) > 0 {
|
||||
vendor = string(vendors[0])
|
||||
}
|
||||
vendor = strings.TrimSpace(strings.Trim(strings.Trim(vendor, ">"), "<"))
|
||||
|
||||
accs := regexp.MustCompile(`Account Ending: [0-9]*([0-9]{4})[^0-9]`).FindSubmatch(b)
|
||||
acc := "?"
|
||||
if len(accs) > 1 {
|
||||
acc = string(accs[1])
|
||||
}
|
||||
|
||||
transaction := NewTransaction(
|
||||
fmt.Sprintf("%s-%s", Amex.String(), acc),
|
||||
fmt.Sprintf("%.2f", f),
|
||||
vendor,
|
||||
fmt.Sprint(m.Header["Date"]),
|
||||
Amex,
|
||||
)
|
||||
return []*Transaction{transaction}, nil
|
||||
}
|
||||
|
||||
func (c *fidelityScraper) scrape(m *mail.Message) ([]*Transaction, error) {
|
||||
subject := fmt.Sprint(m.Header["Subject"])
|
||||
if strings.Contains(subject, "Daily Balance") {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,39 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestScrapeAmex(t *testing.T) {
|
||||
b, _ := os.ReadFile("testdata/amex.txt")
|
||||
|
||||
message := &mail.Message{
|
||||
Header: map[string][]string{
|
||||
"Subject": []string{"Large Purchase Approved"},
|
||||
},
|
||||
Body: bytes.NewReader(b),
|
||||
}
|
||||
|
||||
amex := &amexScraper{}
|
||||
|
||||
gots, err := amex.scrape(message)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(gots) != 1 {
|
||||
t.Fatal(gots)
|
||||
}
|
||||
got := gots[0]
|
||||
|
||||
if got.Account != "AmericanExpress-2003" {
|
||||
t.Fatalf("bad account: %v: %+v", got.Account, got)
|
||||
}
|
||||
if got.Amount != "-30.00" {
|
||||
t.Fatalf("bad amount: %v: %+v", got.Amount, got)
|
||||
}
|
||||
if got.Vendor != "CRAWFORD LEISHMAN DENTAL" {
|
||||
t.Fatalf("bad vendor: %v: %+v", got.Vendor, got)
|
||||
}
|
||||
t.Logf("%+v", got)
|
||||
}
|
||||
|
||||
func TestScrapeFidelityBalance(t *testing.T) {
|
||||
b, _ := os.ReadFile("testdata/fidelity.balance.txt")
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue