parent
eafd022d7d
commit
9f3f3dc08f
|
|
@ -3,6 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
|
|
@ -13,6 +16,10 @@ type Transaction struct {
|
||||||
Date string
|
Date string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Transaction) Format() string {
|
||||||
|
return fmt.Sprintf("(%s) %v: %s @ %s", cleanDate(t.Date), t.Bank, t.Amount, t.Account)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Transaction) String() string {
|
func (t *Transaction) String() string {
|
||||||
return fmt.Sprint(*t)
|
return fmt.Sprint(*t)
|
||||||
}
|
}
|
||||||
|
|
@ -27,3 +34,18 @@ func NewTransaction(amount, account, date string, bank Bank) *Transaction {
|
||||||
t.ID = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprint(t))))
|
t.ID = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprint(t))))
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanDate(date string) string {
|
||||||
|
regexp := regexp.MustCompile(`[A-Z][a-z]{2}, [0-9][0-9]? [A-Z][a-z]{2} 2[0-9]{3}`)
|
||||||
|
matches := regexp.FindAllString(date, -1)
|
||||||
|
if len(matches) < 1 {
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
date = matches[0]
|
||||||
|
time, err := time.Parse(`Mon, 2 Jan 2006`, date)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
return time.Format("Mon Jan 2")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestTransactionFormat(t *testing.T) {
|
||||||
|
x := NewTransaction("12.34", "Amazon", "[Wed, 1 Apr 2020 10:14:11 -0400 (EDT)]", Chase)
|
||||||
|
t.Logf("%s", x.String())
|
||||||
|
t.Logf("%s", x.Format())
|
||||||
|
}
|
||||||
|
|
@ -12,7 +12,7 @@ import (
|
||||||
func Upload(config Config, transaction *Transaction) error {
|
func Upload(config Config, transaction *Transaction) error {
|
||||||
params := url.Values{
|
params := url.Values{
|
||||||
"list": {config.TodoList},
|
"list": {config.TodoList},
|
||||||
"title": {fmt.Sprintf("%v: %s @ %s @ %s", transaction.Bank, transaction.Amount, transaction.Account, transaction.Date)},
|
"title": {transaction.Format()},
|
||||||
"tag": {config.TodoTag},
|
"tag": {config.TodoTag},
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("POST", config.TodoAddr+"/ajax.php?newTask", strings.NewReader(params.Encode()))
|
req, err := http.NewRequest("POST", config.TodoAddr+"/ajax.php?newTask", strings.NewReader(params.Encode()))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue