email the modifications
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2023-12-11 08:58:29 +01:00
parent 8f4fe78358
commit cf0df07adc
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 29 additions and 22 deletions

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.21.1
require github.com/joho/godotenv v1.5.1 require github.com/joho/godotenv v1.5.1
require ( require (
github.com/hexops/gotextdiff v1.0.3
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
) )

2
go.sum
View file

@ -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 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 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= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=

48
wecr.go
View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"crypto/sha256" "fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -10,6 +10,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"gopkg.in/gomail.v2" "gopkg.in/gomail.v2"
) )
@ -30,19 +33,13 @@ func getWebsite(url string) []byte {
return body return body
} }
/* Convert data to sha256 hash. */
func stringToHash(data []byte) string {
hash := sha256.Sum256(data)
return string(hash[:])
}
/* Send an email. */ /* 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 := gomail.NewMessage()
message.SetHeader("From", identity) message.SetHeader("From", identity)
message.SetHeader("To", strings.Join(recipient, ", ")) message.SetHeader("To", strings.Join(recipient, ", "))
message.SetHeader("Subject", "Changes on website") 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) dialer := gomail.NewDialer(host, port, username, password)
@ -97,24 +94,31 @@ func main() {
log.Fatal("SMTP port is invalid.") log.Fatal("SMTP port is invalid.")
} }
last_hash := "" last_bodypage := ""
last_name := ""
sleepDuration := 24 sleepDuration := 24
log.Default().Print("Checking: ", url) log.Default().Print("Checking: ", url)
log.Default().Print("Running each ", sleepDuration, "h, starting now.") log.Default().Print("Running each ", sleepDuration, "h, starting now.")
for { for {
body := getWebsite(url) bodypage := string(getWebsite(url))
hash := stringToHash(body) fname := time.Now().Format(time.DateTime)
if last_hash != hash && len(last_hash) > 0 { if last_bodypage != "" {
sendEmail( edits := myers.ComputeEdits(span.URIFromPath(last_name), last_bodypage, bodypage)
smtp_from, diff := fmt.Sprint(gotextdiff.ToUnified(last_name, fname, last_bodypage, edits))
smtp_user, if diff != "" {
smtp_pass, sendEmail(
smtp_host, smtp_from,
strings.Split(recipient_adress, ","), smtp_user,
smtp_port, 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.") */ /* log.Default().Print("Next loop in ", sleepDuration, "h.") */
time.Sleep(time.Duration(sleepDuration) * time.Hour) time.Sleep(time.Duration(sleepDuration) * time.Hour)