can pull states from email and fill config file
parent
366b35d76b
commit
3d76fffdd3
18
config.json
18
config.json
|
|
@ -1,6 +1,12 @@
|
|||
{
|
||||
"Interval": "1s",
|
||||
"States": ["OH", "CA"],
|
||||
"Interval": "1m",
|
||||
"States": [
|
||||
"GA"
|
||||
],
|
||||
"Storage": [
|
||||
"map"
|
||||
],
|
||||
"Client": "breellocaldev@gmail.com",
|
||||
"Once": true,
|
||||
"Brokers": {
|
||||
"NTG": {
|
||||
|
|
@ -10,8 +16,10 @@
|
|||
},
|
||||
"Emailer": {
|
||||
"From": "breellocaldev@gmail.com",
|
||||
"noIMAP": "imap.gmail.com:993",
|
||||
"SMTP": "",
|
||||
"POP3": "",
|
||||
"IMAP": "imap.gmail.com:993",
|
||||
"Password": "gojfkkfrkmtxzyro",
|
||||
"Limit": 10
|
||||
"Limit": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ type Config struct {
|
|||
Interval Duration
|
||||
States []State
|
||||
Storage []string
|
||||
Client string
|
||||
Once bool
|
||||
Brokers struct {
|
||||
NTG struct {
|
||||
|
|
@ -57,7 +58,7 @@ func Get() *Config {
|
|||
}
|
||||
|
||||
func Set(other Config) {
|
||||
b, err := json.Marshal(other)
|
||||
b, err := json.MarshalIndent(other, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ func (d Duration) Get() time.Duration {
|
|||
return time.Duration(d)
|
||||
}
|
||||
|
||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.Get().String())
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
|
|
|
|||
41
main.go
41
main.go
|
|
@ -1,14 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/storage"
|
||||
"local/truckstop/broker"
|
||||
"local/truckstop/config"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var stateFinder = regexp.MustCompile(`[ ^>][A-Z][A-Z][ $<]`)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
email()
|
||||
|
|
@ -22,6 +29,7 @@ func main() {
|
|||
if err := _main(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
|
||||
func email() error {
|
||||
|
|
@ -29,10 +37,37 @@ func email() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
states := map[config.State]struct{}{}
|
||||
var state config.State
|
||||
for email := range ch {
|
||||
log.Printf("%+v", email)
|
||||
if len(states) > 0 {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(email.Header.Get("From"), config.Get().Client) {
|
||||
continue
|
||||
}
|
||||
b, _ := ioutil.ReadAll(email.Body)
|
||||
candidates := stateFinder.FindAll(b, -1)
|
||||
for _, candidate := range candidates {
|
||||
s := fmt.Sprintf(`"%s"`, bytes.TrimSpace(candidate))
|
||||
err := json.Unmarshal([]byte(s), &state)
|
||||
if err != nil {
|
||||
} else {
|
||||
states[state] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.New("not impl")
|
||||
if len(states) == 0 {
|
||||
return nil
|
||||
}
|
||||
conf := *config.Get()
|
||||
conf.States = []config.State{}
|
||||
for k := range states {
|
||||
conf.States = append(conf.States, k)
|
||||
}
|
||||
log.Printf("%+v, %+v", states, conf)
|
||||
config.Set(conf)
|
||||
return nil
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
|
|
|
|||
Loading…
Reference in New Issue