Skip to content

Instantly share code, notes, and snippets.

@JohnCoogan
Created August 25, 2017 23:13
Show Gist options
  • Save JohnCoogan/d0c6e1a99d22b14f3932887307bd7d82 to your computer and use it in GitHub Desktop.
Save JohnCoogan/d0c6e1a99d22b14f3932887307bd7d82 to your computer and use it in GitHub Desktop.
Monitor web page changes with Go, forked and minified from https://gist.github.com/ssimunic/47834a8fcc3d66d6d7502e916f7ef613
package main
import (
"net/http"
"io/ioutil"
"time"
"log"
"os"
)
const LoopEverySeconds = 5
var client = &http.Client{}
var url string
var lastData string
func checkChanges(newData string) {
if newData == lastData {
log.Println("No changes.")
return
}
log.Println("Changes noticed!")
os.Exit(0)
}
func makeRequest() {
req, err := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
data, err := ioutil.ReadAll(resp.Body)
if lastData == "" {
lastData = string(data)
} else {
checkChanges(string(data))
lastData = string(data)
}
}
func scheduleRequests() {
for range time.Tick(time.Second * LoopEverySeconds) {
makeRequest()
}
}
func main() {
url = os.Args[1]
makeRequest()
scheduleRequests()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment