-
Notifications
You must be signed in to change notification settings - Fork 1
/
sas.go
44 lines (37 loc) · 1.17 KB
/
sas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SubscribeAndSave = 定期オトク便
package sas
import (
"fmt"
"regexp"
"github.com/akiakaba/escol"
"github.com/akiakaba/escol/internal/parse"
"github.com/akiakaba/escol/internal/slices"
"github.com/akiakaba/escol/mu"
)
type Receipt struct {
Products []string
TotalAmount int
}
func Filter(mail escol.Mail) bool {
return mail.From() == `"Amazon定期おトク便" <no-reply@amazon.co.jp>`
}
func Scrape(mail escol.Mail) (*Receipt, error) {
plain, found := mail.FindPart("text/plain")
if !found {
return nil, fmt.Errorf("text/plain parts not found. from:%s", mail.From())
}
body, err := mu.DecodeBase64(plain.Body())
if err != nil {
return nil, err
}
products := regexp.MustCompile(`商品\d+\s+(.+?)\s+数量:\s+\d+\s+商品の価格:\s+¥[\d,]+`).FindAllStringSubmatch(body, -1)
amountMatch := regexp.MustCompile(`合計金額\s+¥([\d,]+)`).FindStringSubmatch(body)
if len(amountMatch) < 2 {
return nil, fmt.Errorf("sas amount unmatched: %s", body)
}
totalAmount := parse.ParseIntFromCommaedDecimal(amountMatch[1])
return &Receipt{
Products: slices.Map(products, func(p []string) string { return p[1] }),
TotalAmount: totalAmount,
}, nil
}