Skip to content

Instantly share code, notes, and snippets.

@ssimunic ssimunic/main.go
Last active Feb 26, 2017

Embed
What would you like to do?
Monitor web page changes with Go
package main
import (
"net/http"
"io/ioutil"
"time"
"log"
"os"
)
const LoopEverySeconds = 5
var client = &http.Client{}
var url string
var cookies 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)
if err != nil {
log.Println(err)
return
}
// Set cookies if they were passed as argument
if cookies != "" {
req.Header.Set("Cookie", cookies)
}
// Send request
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
// Save response body into data variable
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
// If lastData is equal to "", it means that it is
// the first request and we set lastData to current
// response body
// Otherwise, we compare previous and current HTML
if lastData == "" {
lastData = string(data)
} else {
checkChanges(string(data))
lastData = string(data)
}
}
func scheduleRequests() {
// Infinite loop to run every LoopEverySeconds seconds
for range time.Tick(time.Second * LoopEverySeconds) {
makeRequest()
}
}
func main() {
// Exit program if URL is missing
if len(os.Args) < 2 {
log.Println("Missing URL.")
os.Exit(0)
}
// Set url variable
url = os.Args[1]
// Set cookie variable if passed as argument
if len(os.Args) == 3 {
cookies = os.Args[2]
}
// Make initial request to set first lastData
makeRequest()
// Continue with requests, loop every LoopEverySeconds seconds
scheduleRequests()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.