Skip to content

Commit

Permalink
wotd script
Browse files Browse the repository at this point in the history
  • Loading branch information
Watt3r committed Dec 14, 2021
0 parents commit 65cd8f4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
43 changes: 43 additions & 0 deletions wotd.go
Original file line number Diff line number Diff line change
@@ -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, "<h1>", "</h1>")
def := findPhrase(pageContent, "<p>", "</p>")

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

0 comments on commit 65cd8f4

Please sign in to comment.