Skip to content

Instantly share code, notes, and snippets.

@tjs-w
Created January 18, 2016 10:05
Show Gist options
  • Save tjs-w/fef336e68c530e88331d to your computer and use it in GitHub Desktop.
Save tjs-w/fef336e68c530e88331d to your computer and use it in GitHub Desktop.
Quote of the Day
// qotd: Quote of the Day
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/fatih/color"
)
const (
weblink = "http://www.brainyquote.com/link/quotebr.js"
)
func parseQotd(s string) string {
var buffer bytes.Buffer
mag := color.New(color.Bold, color.FgMagenta).SprintFunc()
yel := color.New(color.FgYellow).SprintFunc()
lines := strings.Split(s, ";\n")
q := strings.Split(strings.Split(lines[2], "\"")[1], "<")[0]
buffer.WriteString(mag(q))
a := strings.Split(strings.Split(lines[3], ">")[1], "<")[0]
buffer.WriteString(yel("\n\t- "))
buffer.WriteString(yel(a))
return buffer.String()
}
func getQotd() (string, error) {
rsp, err := http.Get(weblink)
if err != nil {
return "", err
}
defer rsp.Body.Close()
content, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return "", err
}
return parseQotd(string(content)), nil
}
func main() {
s, err := getQotd()
if err != nil {
panic(err)
}
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment