From cf0df07adcfa4345a65d481fcc38262b309c1a22 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 11 Dec 2023 08:58:29 +0100 Subject: [PATCH] email the modifications --- go.mod | 1 + go.sum | 2 ++ wecr.go | 48 ++++++++++++++++++++++++++---------------------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 669b17d..23ccfc4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.1 require github.com/joho/godotenv v1.5.1 require ( + github.com/hexops/gotextdiff v1.0.3 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) diff --git a/go.sum b/go.sum index 2c85102..5609a32 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= diff --git a/wecr.go b/wecr.go index 8e62f88..2a6060e 100644 --- a/wecr.go +++ b/wecr.go @@ -1,7 +1,7 @@ package main import ( - "crypto/sha256" + "fmt" "io" "log" "net/http" @@ -10,6 +10,9 @@ import ( "strings" "time" + "github.com/hexops/gotextdiff" + "github.com/hexops/gotextdiff/myers" + "github.com/hexops/gotextdiff/span" "github.com/joho/godotenv" "gopkg.in/gomail.v2" ) @@ -30,19 +33,13 @@ func getWebsite(url string) []byte { return body } -/* Convert data to sha256 hash. */ -func stringToHash(data []byte) string { - hash := sha256.Sum256(data) - return string(hash[:]) -} - /* Send an email. */ -func sendEmail(identity string, username string, password string, host string, recipient []string, port int) { +func sendEmail(identity string, username string, password string, host string, recipient []string, port int, body string) { message := gomail.NewMessage() message.SetHeader("From", identity) message.SetHeader("To", strings.Join(recipient, ", ")) message.SetHeader("Subject", "Changes on website") - message.SetBody("text/plain", "A changed has been found on the website.") + message.SetBody("text/plain", body) dialer := gomail.NewDialer(host, port, username, password) @@ -97,24 +94,31 @@ func main() { log.Fatal("SMTP port is invalid.") } - last_hash := "" + last_bodypage := "" + last_name := "" sleepDuration := 24 log.Default().Print("Checking: ", url) log.Default().Print("Running each ", sleepDuration, "h, starting now.") for { - body := getWebsite(url) - hash := stringToHash(body) - if last_hash != hash && len(last_hash) > 0 { - sendEmail( - smtp_from, - smtp_user, - smtp_pass, - smtp_host, - strings.Split(recipient_adress, ","), - smtp_port, - ) + bodypage := string(getWebsite(url)) + fname := time.Now().Format(time.DateTime) + if last_bodypage != "" { + edits := myers.ComputeEdits(span.URIFromPath(last_name), last_bodypage, bodypage) + diff := fmt.Sprint(gotextdiff.ToUnified(last_name, fname, last_bodypage, edits)) + if diff != "" { + sendEmail( + smtp_from, + smtp_user, + smtp_pass, + smtp_host, + strings.Split(recipient_adress, ","), + smtp_port, + "A changed has been found on the website:\n"+diff, + ) + } } - last_hash = hash + last_bodypage = bodypage + last_name = fname /* log.Default().Print("Next loop in ", sleepDuration, "h.") */ time.Sleep(time.Duration(sleepDuration) * time.Hour)