Skip to content

Commit

Permalink
Support multiple JSON selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ordspilleren committed May 20, 2021
1 parent 80409c9 commit 1380447
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"

Expand All @@ -17,7 +18,7 @@ import (
)

type MonitorClient interface {
GetContent(url string, httpHeaders http.Header, selectors Selectors) []string
GetContent(url string, httpHeaders http.Header, selectors Selectors) string
}

type ChromeClient struct {
Expand All @@ -43,8 +44,8 @@ type Monitor struct {
}

type Selectors struct {
CSS *string `json:"css,omitempty"`
JSON *string `json:"json,omitempty"`
CSS *string `json:"css,omitempty"`
JSON *[]string `json:"json,omitempty"`
}

type Monitors []Monitor
Expand Down Expand Up @@ -108,20 +109,20 @@ func (m *Monitor) check() {
selectorContent := m.client.GetContent(m.URL, m.HTTPHeaders, m.Selectors)
storageContent := m.storage.GetContent()

if m.compareContent(storageContent, selectorContent[0]) {
m.storage.WriteContent(selectorContent[0])
if m.compareContent(storageContent, selectorContent) {
m.storage.WriteContent(selectorContent)
log.Print("Content has changed!")
_ = m.notifier.Send(
context.Background(),
fmt.Sprintf("%s has changed!", m.Name),
fmt.Sprintf("New content: %s\nOld content: %s\nURL: %s", selectorContent[0], storageContent, m.URL),
fmt.Sprintf("New content: %.200s\nOld content: %.200s\nURL: %s", selectorContent, storageContent, m.URL),
)
} else {
log.Printf("Nothing has changed, waiting %s.", m.Interval*time.Minute)
}
}

func (h HttpClient) GetContent(url string, httpHeaders http.Header, selectors Selectors) []string {
func (h HttpClient) GetContent(url string, httpHeaders http.Header, selectors Selectors) string {
responseBody := getHTTPBody(url, httpHeaders)

var selectorContent string
Expand All @@ -131,13 +132,10 @@ func (h HttpClient) GetContent(url string, httpHeaders http.Header, selectors Se
} else if selectors.JSON != nil {
selectorContent = getJSONSelectorContent(responseBody, *selectors.JSON)
} else {
bodyBytes, err := ioutil.ReadAll(responseBody)
if err != nil {
log.Fatal(err)
}
selectorContent = string(bodyBytes)
selectorContent = getHTMLText(responseBody)
}
return []string{selectorContent}

return selectorContent
}

func getHTTPBody(url string, headers http.Header) io.ReadCloser {
Expand All @@ -164,28 +162,41 @@ func getCSSSelectorContent(body io.ReadCloser, selector string) string {
log.Fatal(err)
}

selectorText := doc.Find(selector).Text()
result := doc.Find(selector).Text()

return selectorText
return result
}

func getJSONSelectorContent(body io.ReadCloser, selector string) string {
bodyBytes, err := ioutil.ReadAll(body)
func getHTMLText(body io.ReadCloser) string {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
log.Fatal(err)
}

jsonValue := gjson.GetBytes(bodyBytes, selector)
return jsonValue.String()
doc.Find("script").Remove()

return doc.Text()
}

func (m *Monitor) compareContent(storage string, selector string) bool {
log.Print("Cache content: " + storage)
log.Print("New content: " + selector)
func getJSONSelectorContent(body io.ReadCloser, selector []string) string {
bodyBytes, err := ioutil.ReadAll(body)
if err != nil {
log.Fatal(err)
}

if storage != selector {
return true
var results []string
result := gjson.GetManyBytes(bodyBytes, selector...)

for _, value := range result {
results = append(results, value.String())
}

return false
return strings.Join(results, "")
}

func (m *Monitor) compareContent(storage string, selector string) bool {
log.Printf("Cache content: %s", storage)
log.Printf("New content: %s", selector)

return storage != selector
}

0 comments on commit 1380447

Please sign in to comment.