Skip to content

Instantly share code, notes, and snippets.

@viktor-evdokimov
Forked from ssimunic/main.go
Created February 11, 2017 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktor-evdokimov/828132b3955adadc0264d3da4de57c6b to your computer and use it in GitHub Desktop.
Save viktor-evdokimov/828132b3955adadc0264d3da4de57c6b to your computer and use it in GitHub Desktop.
Monitor web page changes with Go
package main
import (
"net/http"
"io/ioutil"
"time"
"log"
"os"
)
const LOOP_EVERY_SECONDS = 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))
}
}
func scheduleRequests() {
// Infinite loop to run every LOOP_EVERY_SECONDS seconds
for range time.Tick(time.Second * LOOP_EVERY_SECONDS) {
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 LOOP_EVERY_SECONDS seconds
scheduleRequests()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment