can pull states from email and fill config file

master
Bel LaPointe 2022-01-10 00:21:56 -05:00
parent 366b35d76b
commit 3d76fffdd3
5 changed files with 58 additions and 10 deletions

View File

@ -1,6 +1,12 @@
{ {
"Interval": "1s", "Interval": "1m",
"States": ["OH", "CA"], "States": [
"GA"
],
"Storage": [
"map"
],
"Client": "breellocaldev@gmail.com",
"Once": true, "Once": true,
"Brokers": { "Brokers": {
"NTG": { "NTG": {
@ -10,8 +16,10 @@
}, },
"Emailer": { "Emailer": {
"From": "breellocaldev@gmail.com", "From": "breellocaldev@gmail.com",
"noIMAP": "imap.gmail.com:993", "SMTP": "",
"POP3": "",
"IMAP": "imap.gmail.com:993",
"Password": "gojfkkfrkmtxzyro", "Password": "gojfkkfrkmtxzyro",
"Limit": 10 "Limit": 0
} }
} }

View File

@ -13,6 +13,7 @@ type Config struct {
Interval Duration Interval Duration
States []State States []State
Storage []string Storage []string
Client string
Once bool Once bool
Brokers struct { Brokers struct {
NTG struct { NTG struct {
@ -57,7 +58,7 @@ func Get() *Config {
} }
func Set(other Config) { func Set(other Config) {
b, err := json.Marshal(other) b, err := json.MarshalIndent(other, "", " ")
if err != nil { if err != nil {
return return
} }

View File

@ -11,6 +11,10 @@ func (d Duration) Get() time.Duration {
return time.Duration(d) return time.Duration(d)
} }
func (d *Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Get().String())
}
func (d *Duration) UnmarshalJSON(b []byte) error { func (d *Duration) UnmarshalJSON(b []byte) error {
var s string var s string
if err := json.Unmarshal(b, &s); err != nil { if err := json.Unmarshal(b, &s); err != nil {

41
main.go
View File

@ -1,14 +1,21 @@
package main package main
import ( import (
"errors" "bytes"
"encoding/json"
"fmt"
"io/ioutil"
"local/storage" "local/storage"
"local/truckstop/broker" "local/truckstop/broker"
"local/truckstop/config" "local/truckstop/config"
"log" "log"
"regexp"
"strings"
"time" "time"
) )
var stateFinder = regexp.MustCompile(`[ ^>][A-Z][A-Z][ $<]`)
func main() { func main() {
go func() { go func() {
email() email()
@ -22,6 +29,7 @@ func main() {
if err := _main(); err != nil { if err := _main(); err != nil {
panic(err) panic(err)
} }
time.Sleep(time.Minute)
} }
func email() error { func email() error {
@ -29,10 +37,37 @@ func email() error {
if err != nil { if err != nil {
return err return err
} }
states := map[config.State]struct{}{}
var state config.State
for email := range ch { 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 { func _main() error {

View File

@ -1,5 +1,5 @@
todo: todo:
- read email to state file - read email to state file from local/sandbox/contact/contact
- upload cookies - upload cookies
- download cookies - download cookies
- encrypt cookies - encrypt cookies