Last active
February 16, 2020 22:12
-
-
Save ssimunic/47834a8fcc3d66d6d7502e916f7ef613 to your computer and use it in GitHub Desktop.
Monitor web page changes with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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