Created
August 25, 2017 23:13
-
-
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
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 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