From 65cd8f4335cf51001720285a1b6b8557817e77ca Mon Sep 17 00:00:00 2001 From: Watt3r Date: Mon, 13 Dec 2021 17:06:40 -0800 Subject: [PATCH] wotd script --- go.mod | 10 ++++++++++ go.sum | 11 +++++++++++ wotd.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 wotd.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..288bda2 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module watt3r/wotd + +go 1.17 + +require ( + github.com/fatih/color v1.13.0 // indirect + github.com/mattn/go-colorable v0.1.9 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6ab7ee4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/wotd.go b/wotd.go new file mode 100644 index 0000000..d9a004e --- /dev/null +++ b/wotd.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "log" + "strings" + "regexp" + "github.com/fatih/color" +) + +func findPhrase(pageContent string, start string, end string) []byte { + wordIndex := strings.Index(pageContent, start) + if wordIndex == -1 { + log.Fatal("Word not found") + } + wordIndex += len(start) + wordEndIndex := strings.Index(pageContent[wordIndex:], end) + wordIndex + word := []byte(pageContent[wordIndex:wordEndIndex]) + return word +} +func main() { + // Color output + blue := color.New(color.FgBlue).Add(color.Bold).SprintFunc() + green := color.New(color.FgGreen).Add(color.Bold).SprintFunc() + + // Get Word of the Day from Merriam-Webster + resp, err := http.Get("https://www.merriam-webster.com/word-of-the-day/") + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + + // Simple parse HTML + pageContent := string(data) + word := findPhrase(pageContent, "

", "

") + def := findPhrase(pageContent, "

", "

") + + // Print result + fmt.Printf("Word of the Day: %s\nDefinition: %s\n", blue(fmt.Sprintf("%s", word)), green(regexp.MustCompile("").ReplaceAllString(fmt.Sprintf("%s", def), ""))) +}