Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit 6ea26e0

Browse files
committed
refactor -- more go idiomatic
1 parent e9ae6ea commit 6ea26e0

File tree

6 files changed

+48
-57
lines changed

6 files changed

+48
-57
lines changed

gutils/scheduler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ var SchdFreq = time.Minute * 1
1212

1313
func init() {
1414
if v, ok := os.LookupEnv("SCHEDULE_FREQ"); ok {
15-
if vi, err := strconv.Atoi(v); err == nil {
16-
SchdFreq = time.Minute * time.Duration(vi)
17-
} else {
15+
vi, err := strconv.Atoi(v)
16+
if err != nil {
1817
log.Println("WARN: SCHEDULE_FREQ is not int but ", v)
18+
return
1919
}
20+
SchdFreq = time.Minute * time.Duration(vi)
2021
}
2122
}
2223

gutils/tasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func processEntity(ent *models.Entity) {
8080
if ent.Options.AlertType == "onChange" && content != last.Price {
8181
subject := fmt.Sprintf("[%s] <%s> Alert: price changes to %s!", email.Identity, ent.Name, content)
8282
ent.SendEmail(&subject)
83-
} else if ent.Options.AlertType == "threshold" && ent.Options.Threshold >= float32(thisP) {
83+
}
84+
if ent.Options.AlertType == "threshold" && ent.Options.Threshold >= float32(thisP) {
8485
subject := fmt.Sprintf("[%s] <%s> Alert: price drops to %s!", email.Identity, ent.Name, content)
8586
ent.SendEmail(&subject)
8687
}

handlers/create.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8-
"strings"
98

109
"github.com/labstack/echo"
1110
"github.com/xiahongze/pricetracker/email"
@@ -30,21 +29,19 @@ func Create(c echo.Context) error {
3029
ok bool
3130
)
3231

32+
if !req.Options.UseChrome {
33+
content, ok = trackers.SimpleTracker(&req.URL, &req.XPATH)
34+
}
35+
if !ok {
36+
req.Options.UseChrome = true
37+
log.Println("INFO: Resorting to Chrome")
38+
}
3339
if req.Options.UseChrome {
3440
if content, ok = trackers.ChromeTracker(&req.URL, &req.XPATH); !ok {
3541
return c.String(http.StatusBadRequest, content)
3642
}
37-
} else {
38-
if content, ok = trackers.SimpleTracker(&req.URL, &req.XPATH); !ok {
39-
req.Options.UseChrome = true
40-
log.Println("Resorting to Chrome")
41-
if content, ok = trackers.ChromeTracker(&req.URL, &req.XPATH); !ok {
42-
return c.String(http.StatusBadRequest, content)
43-
}
44-
}
4543
}
4644

47-
content = strings.TrimSpace(content)
4845
// check content as expected
4946
if content != req.ExpectedPrice {
5047
return c.String(http.StatusExpectationFailed,

models/entity.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,33 @@ func (entity *Entity) Save(ctx context.Context, entTypName string, dsClient *dat
3434
defer func() {
3535
k, _ := json.Marshal(entity.K)
3636
if err != nil {
37-
log.Printf("ERROR: failed to save entity with %s\n", err)
38-
} else {
39-
log.Printf("INFO: saved K=%s\n", k)
37+
log.Printf("ERROR: failed to save entity (K=%s) with %s\n", k, err)
38+
return
4039
}
40+
log.Printf("INFO: saved K=%s\n", k)
4141
}()
4242

43-
if entity.K == nil {
44-
k := datastore.IncompleteKey(entTypName, nil)
45-
var key *datastore.Key
46-
key, err = dsClient.Put(ctx, k, entity)
47-
if err != nil {
48-
return err
49-
}
50-
entity.K = key
51-
} else {
43+
if entity.K != nil {
5244
_, err = dsClient.Put(ctx, entity.K, entity)
53-
if err != nil {
54-
return err
55-
}
45+
return
5646
}
57-
return nil
47+
entity.K = datastore.IncompleteKey(entTypName, nil)
48+
entity.K, err = dsClient.Put(ctx, entity.K, entity)
49+
if err != nil {
50+
return
51+
}
52+
53+
return
5854
}
5955

6056
// SendEmail does what the name says
6157
func (entity *Entity) SendEmail(subject *string) {
62-
if b, err := json.MarshalIndent(entity, "", " "); err == nil {
63-
if err := email.Send(string(b), *subject, entity.Options.Email); err != nil {
64-
log.Print("failed to send email", err)
65-
}
66-
} else {
58+
b, err := json.MarshalIndent(entity, "", " ")
59+
if err != nil {
6760
log.Print("failed to marshal entity", err)
61+
return
62+
}
63+
if err := email.Send(string(b), *subject, entity.Options.Email); err != nil {
64+
log.Print("failed to send email", err)
6865
}
6966
}

trackers/chrome_tracker.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ var (
1818
)
1919

2020
func init() {
21+
if v, ok := os.LookupEnv("CHROME_PATH"); ok {
22+
chromePath = v
23+
}
2124
if v, ok := os.LookupEnv("CHROME_TIMEOUT"); ok {
22-
if vi, err := strconv.Atoi(v); err == nil {
23-
chromeTimeout = time.Second * time.Duration(vi)
24-
} else {
25+
vi, err := strconv.Atoi(v)
26+
if err != nil {
2527
log.Println("WARN: CHROME_TIMEOUT is not int but ", v)
28+
return
2629
}
27-
}
28-
if v, ok := os.LookupEnv("CHROME_PATH"); ok {
29-
chromePath = v
30+
chromeTimeout = time.Second * time.Duration(vi)
3031
}
3132
}
3233

@@ -35,17 +36,11 @@ func init() {
3536
func ChromeTracker(url, xpath *string) (string, bool) {
3637
ctx, cancel := context.WithTimeout(context.Background(), chromeTimeout)
3738
defer cancel()
38-
var runnerOpt chromedp.Option
39-
if chromePath == "" {
40-
runnerOpt = chromedp.WithRunnerOptions(
41-
runner.Flag("headless", true),
42-
)
43-
} else {
44-
runnerOpt = chromedp.WithRunnerOptions(
45-
runner.Path(chromePath),
46-
runner.Flag("headless", true),
47-
)
39+
opts := []runner.CommandLineOption{runner.Flag("headless", true)}
40+
if chromePath != "" {
41+
opts = append(opts, runner.Path(chromePath))
4842
}
43+
runnerOpt := chromedp.WithRunnerOptions(opts...)
4944

5045
// create chrome instance
5146
c, err := chromedp.New(ctx, runnerOpt)

trackers/simple_tracker.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ func SimpleTracker(url, xpath *string) (content string, ok bool) {
3434
// create closure
3535
extractHelper := func(reader io.Reader) {
3636
xmlRoot, xmlErr := xmlpath.ParseHTML(reader)
37-
3837
if xmlErr != nil {
3938
content = "ERROR: parse xml error: " + xmlErr.Error()
4039
log.Println(content)
4140
ok = false
4241
return
4342
}
44-
if value, found := xpExec.String(xmlRoot); found {
45-
log.Println("INFO: Found", value, "from", *url)
46-
content = value
47-
ok = true
48-
} else {
43+
value, found := xpExec.String(xmlRoot)
44+
if !found {
4945
ok = false
5046
content = "value not found"
47+
return
5148
}
49+
log.Println("INFO: Found", value, "from", *url)
50+
content = value
51+
ok = true
5252
}
5353

5454
// step 1. read directly from body

0 commit comments

Comments
 (0)