diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7db388 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +dorset-binformation +dorset-binformation.exe diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eb3767e --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +.DEFAULT_GOAL := build + +clean: + rm -f dorset-binformation + +fmt: + go fmt ./... +.PHONY:fmt + +lint: fmt + golint ./... +.PHONY:lint + +vet: fmt + go vet ./... +.PHONY:vet + +build: vet + go build +.PHONY:build + +run: build + ./dorset-binformation +.PHONY:run + +install: build + mkdir -p /opt/dorset-binformation/ + useradd dorset-binformation || true + chown dorset-binformation:dorset-binformation /opt/dorset-binformation/ + cp dorset-binformation /opt/dorset-binformation/ + cp dorset-binformation.service /etc/systemd/system/ \ No newline at end of file diff --git a/README.md b/README.md index 9ef9d82..e85f10b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ -# dcbins -A data scraper for the Dorset Council bin collection site +# dorset-binformation + +A data scraper for the Dorset Council bin collection site for bin collection dates + +# Installation +```` +go get -v github.com/stevenhowes/dorset-binformation/ +make install +service dorset-binformation start +```` + +Edit /etc/systemd/system/dorset-binformation.service if an alternate port is required + +# Example Curl +```` +curl http://localhost:8998/uprn/100041115206 + +{"food":1712880000,"recycling":1713484800,"rubbish":1712880000} +```` diff --git a/dorset-binformation.go b/dorset-binformation.go new file mode 100644 index 0000000..db4c0c3 --- /dev/null +++ b/dorset-binformation.go @@ -0,0 +1,134 @@ +package main + +import ( + "encoding/json" + "errors" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" + "time" + + "golang.org/x/net/html" +) + +func getHTML(url string) (*html.Node, error) { + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + doc, err := html.Parse(strings.NewReader(string(b))) + if err != nil { + return nil, err + } + + return doc, nil +} + +func dateToEpoch(date string) int64 { + layout := "Monday 2 January 2006" + t, err := time.Parse(layout, date) + if err != nil { + fmt.Println(err) + } + return t.Unix() +} +func findDates(n *html.Node, dates map[string]int64) error { + + if n.Type == html.TextNode { + // Search for "Your next" in the text nodes + if strings.Contains(n.Data, "No results could be retrieved for this enquiry") { + return errors.New(n.Data) + } + if strings.Contains(n.Data, "Your next recycling collection day is") { + dates["recycling"] = dateToEpoch(n.NextSibling.FirstChild.Data) + } + if strings.Contains(n.Data, "Your next rubbish collection is") { + dates["rubbish"] = dateToEpoch(n.NextSibling.FirstChild.Data) + } + if strings.Contains(n.Data, "Your next food waste collection day is") { + dates["food"] = dateToEpoch(n.NextSibling.FirstChild.Data) + } + if strings.Contains(n.Data, "Your next garden waste collection is") { + dates["garden"] = dateToEpoch(n.NextSibling.FirstChild.Data) + } + } + + for c := n.FirstChild; c != nil; c = c.NextSibling { + err := findDates(c, dates) + if err != nil { + return err + } + } + return nil +} +func handleRequest(w http.ResponseWriter, r *http.Request, logger *log.Logger) { + // Splitting the path by '/' + pathParts := strings.Split(r.URL.Path, "/") + + // Extracting the variable from the path + uprn := pathParts[len(pathParts)-1] + + // Responding with the clients IP and extracted variable + logger.Printf("%s: %s", r.RemoteAddr, uprn) + + // use an http get to retrieve Dorset Council html + doc, err := getHTML("https://gi.dorsetcouncil.gov.uk/mapping/mylocal/viewresults/" + uprn) + + dates := make(map[string]int64) + + // if there is an error, log it and return a 500 + if err != nil { + logger.Println("Error retrieving data: ", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + // If we got an error from the council (in text) pass it on + err = findDates(doc, dates) + if err != nil { + logger.Println("Error retrieving data: ", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + // Convert the map into a JSON string + jsonData, err := json.Marshal(dates) + if err != nil { + logger.Println(err) + } + + // return jsonData to the client + w.Header().Set("Content-Type", "application/json") + w.Write(jsonData) +} + +func main() { + logger := log.New(os.Stderr, "", 0) + + listenPort := "" + + flag.StringVar(&listenPort, "port", ":8998", "Port to listen on") + flag.Parse() + + // Registering the handler for requests to "/" + http.HandleFunc("/uprn/", func(w http.ResponseWriter, r *http.Request) { + handleRequest(w, r, logger) + }) + + // Starting the HTTP server on port 8998 + logger.Println("Listening on " + listenPort) + err := http.ListenAndServe(listenPort, nil) + if err != nil { + logger.Println("Error starting server: ", err) + } +} diff --git a/dorset-binformation.service b/dorset-binformation.service new file mode 100644 index 0000000..50a02ff --- /dev/null +++ b/dorset-binformation.service @@ -0,0 +1,17 @@ +[Unit] +Description=Dorset Binformation Server +ConditionPathExists=/opt/dorset-binformation/dorset-binformation +After=network.target +[Service] +Type=simple +User=dorset-binformation +Group=dorset-binformation +WorkingDirectory=/opt/dorset-binformation/ +ExecStart=/opt/dorset-binformation/dorset-binformation --port localhost:9090 +Restart=on-failure +RestartSec=10 +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=dorset-binformation +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ba4c541 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/stevenhowes/dorset-binformation + +go 1.22.2 + +require golang.org/x/net v0.24.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9e037e0 --- /dev/null +++ b/go.sum @@ -0,0 +1,44 @@ +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=