Skip to content

Commit

Permalink
rss feeds have color and a bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nickali committed Aug 4, 2020
1 parent bbb4d75 commit a48c036
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 50 deletions.
15 changes: 6 additions & 9 deletions addons/news/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ var maxItems = 20
var maxLength = 300
var fmtDate string

// News is wrapper for news data.

// NewsPrint just outputs a string.

func NewsPrint(url string, stAPI string) ([]string, []text.WriteOption, []text.WriteOption) {
// Print outupts what is to be printed on the screen.
func Print(url string, stAPI string) ([]string, []text.WriteOption, []text.WriteOption) {
wrappedText := make([]string, 0)
wrappedOpt := make([]text.WriteOption, 0)
wrappedState := make([]text.WriteOption, 0)
Expand Down Expand Up @@ -50,23 +47,23 @@ func NewsPrint(url string, stAPI string) ([]string, []text.WriteOption, []text.W

for i := 0; i < numArticles/2-1; i++ {

descLength := len(resultDesc[i].String())
filterDesc := strings.ReplaceAll(resultDesc[i].String(), "\u00a0", "")
descLength := len(filterDesc)
if descLength < 300 {
if descLength == 0 {
maxLength = 0
} else {
maxLength = len(resultDesc[i].String()) - 1
maxLength = len(filterDesc) - 1
}
} else {
maxLength = 300
}

scrubbedDesc := strings.ReplaceAll(resultDesc[i].String(), "\u00a0", "")
wrappedText = append(wrappedText, resultTitle[i].String()+"\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorGreen)))
wrappedState = append(wrappedState, nil)

wrappedText = append(wrappedText, scrubbedDesc[:maxLength]+"\n")
wrappedText = append(wrappedText, filterDesc[:maxLength]+"\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, nil)

Expand Down
1 change: 1 addition & 0 deletions addons/newsreader/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
github.com/grokify/html-strip-tags-go v0.0.0-20200322061010-ea0c1cf2f119
github.com/microcosm-cc/bluemonday v1.0.3
github.com/mmcdole/gofeed v1.0.0
github.com/mum4k/termdash v0.12.1
)
12 changes: 12 additions & 0 deletions addons/newsreader/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
Expand All @@ -8,21 +9,32 @@ github.com/chris-ramon/douceur v0.2.0 h1:IDMEdxlEUUBYBKE4z/mJnFyVXox+MjuEVDJNN27
github.com/chris-ramon/douceur v0.2.0/go.mod h1:wDW5xjJdeoMm1mRt4sD4c/LbF/mWdEpRXQKjTR8nIBE=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/grokify/html-strip-tags-go v0.0.0-20200322061010-ea0c1cf2f119 h1:h3iGUlU8HyW4baKd6D+h1mwOHnM2kwskSuG6Bv4tSbc=
github.com/grokify/html-strip-tags-go v0.0.0-20200322061010-ea0c1cf2f119/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/microcosm-cc/bluemonday v1.0.3 h1:EjVH7OqbU219kdm8acbveoclh2zZFqPJTJw6VUlTLAQ=
github.com/microcosm-cc/bluemonday v1.0.3/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w=
github.com/mmcdole/gofeed v1.0.0 h1:PHqwr8fsEm8xarj9s53XeEAFYhRM3E9Ib7Ie766/LTE=
github.com/mmcdole/gofeed v1.0.0/go.mod h1:tkVcyzS3qVMlQrQxJoEH1hkTiuo9a8emDzkMi7TZBu0=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8=
github.com/mum4k/termdash v0.12.1 h1:g3WAT602WIYII6Szhn2KsGoT4PffJZQoA4aAFxEllKc=
github.com/mum4k/termdash v0.12.1/go.mod h1:haerPCSO0U8pehROAecmuOHDF+2UXw2KaCTxdWooDFE=
github.com/nsf/termbox-go v0.0.0-20200204031403-4d2b513ad8be/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
53 changes: 44 additions & 9 deletions addons/newsreader/newsreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ import (

"github.com/microcosm-cc/bluemonday"
"github.com/mmcdole/gofeed"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/text"
)

var maxItems = 20

var maxLength = 300
var fmtDate string

// NewsReaderPrint just outputs a string.
func NewsReaderPrint(stURL string) string {
// Print outputs what needs to be drawn on the screen.
func Print(stURL string) ([]string, []text.WriteOption, []text.WriteOption) {
wrappedText := make([]string, 0)
wrappedOpt := make([]text.WriteOption, 0)
wrappedState := make([]text.WriteOption, 0)

stOutput := strings.Builder{}
dt := time.Now()
fp := gofeed.NewParser()
feed, err := fp.ParseURL(stURL)

if err != nil {
stOutput.WriteString("The HTTP request failed with error with feed")
wrappedText = append(wrappedText, "The HTTP request failed with error with feed")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, text.WriteReplace())

} else {
fmtDate = "RSS (" + dt.Format("01-02-2006 15:04:05"+")\n\n")
fmtDate = "Updated (" + dt.Format("01-02-2006 15:04:05"+")\n\n")
wrappedText = append(wrappedText, fmtDate)
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorYellow)))
wrappedState = append(wrappedState, text.WriteReplace())
stOutput.WriteString(fmtDate)

var items = feed.Items
Expand All @@ -31,7 +43,7 @@ func NewsReaderPrint(stURL string) string {
p.AllowElements("p")

for i := 0; i <= (maxItemsInFeed-1) && i <= (maxItems-1); i++ {
stOutput.WriteString(items[i].Title + "\n")
// sanitize description
strippedNewlines2Desc := strings.Replace(items[i].Description, "\r", " ", -1)
strippedNewlines3Desc := strings.Replace(strippedNewlines2Desc, "<!-- SC_OFF --><div>", "", -1)
strippedNewlines4Desc := strings.Replace(strippedNewlines3Desc, "<div>", "", -1)
Expand All @@ -40,13 +52,36 @@ func NewsReaderPrint(stURL string) string {
strippedNewlines7Desc := strings.Replace(strippedNewlines6Desc, "<p>", "", -1)
strippedNewlines8Desc := strings.Replace(strippedNewlines7Desc, "</p>", "", -1)
strippedNewlines9Desc := strings.Replace(strippedNewlines8Desc, "&lt;", "", -1)
strippedDesc := strippedNewlines9Desc[:100]

stOutput.WriteString(p.Sanitize(strippedDesc) + "\n" + items[i].Link + "\n-----\n")
descLength := len(strippedNewlines9Desc)
if descLength < 300 {
if descLength == 0 {
maxLength = 0
} else {
maxLength = len(strippedNewlines9Desc) - 1
}
} else {
maxLength = 300
}

strippedDesc := p.Sanitize(strippedNewlines9Desc[:maxLength])

wrappedText = append(wrappedText, items[i].Title+"\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorGreen)))
wrappedState = append(wrappedState, nil)

wrappedText = append(wrappedText, strippedDesc+"\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorRed)))
wrappedState = append(wrappedState, nil)

wrappedText = append(wrappedText, items[i].Link+"\n\n")
wrappedOpt = append(wrappedOpt, text.WriteCellOpts(cell.FgColor(cell.ColorBlue)))
wrappedState = append(wrappedState, nil)

}

}

return stOutput.String()
return wrappedText, wrappedOpt, wrappedState

}
6 changes: 3 additions & 3 deletions addons/stocks/stocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type QuoteDetail struct {
QuoteChangePer string `json:"10. change percent"`
}

// StockPrint just outputs a string.
// Print just outputs a string.
// See https://www.alphavantage.co/documentation/#latestprice
func StockPrint(stSymbol string, stAPI string) {
func Print(stSymbol string, stAPI string) {
stURL := url + "?function=GLOBAL_QUOTE&symbol=" + stSymbol + "&apikey=" + stAPI
// fmt.Println("Stock URL: %s", stURL)

response, err := http.Get(stURL)
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
Expand Down
12 changes: 6 additions & 6 deletions addons/weather/weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const url = "https://api.openweathermap.org/data/2.5/weather?"

// Weather is wrapper for weather data.
type Weather struct {
WeatherDetail *WeatherDetail `json:"main"`
WeatherDetail *WDetail `json:"main"`
}

// WeatherDetail provides details about the current weather
type WeatherDetail struct {
// WDetail provides details about the current weather
type WDetail struct {
WeatherTemp float32 `json:"temp"`
WeatherFeelsLike float32 `json:"feels_like"`
WeatherTempMin float32 `json:"temp_min"`
Expand All @@ -29,9 +29,9 @@ type WeatherDetail struct {
WeatherHumidity float32 `json:"humidity"`
}

// WeatherPrint just outputs a string.
// Print outputs what needs to be drawn on the screen.
// See https://openweathermap.org/current#one
func WeatherPrint(stZip string, stAPI string) ([]string, []text.WriteOption, []text.WriteOption) {
func Print(stZip string, stAPI string) ([]string, []text.WriteOption, []text.WriteOption) {
dt := time.Now()
var b strings.Builder
wrappedText := make([]string, 0)
Expand All @@ -48,7 +48,7 @@ func WeatherPrint(stZip string, stAPI string) ([]string, []text.WriteOption, []t

data, _ := ioutil.ReadAll(response.Body)
jsonData := &Weather{
WeatherDetail: &WeatherDetail{},
WeatherDetail: &WDetail{},
}
err := json.Unmarshal([]byte(data), jsonData)

Expand Down
46 changes: 23 additions & 23 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func writeUpdate(ctx context.Context, TopLeft *text.Text, BottomLeft *text.Text,
for {
select {
case <-ticker.C:
wrappedText, wrappedOpt, wrappedState := weather.WeatherPrint(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))

wrappedText, wrappedOpt, wrappedState := weather.Print(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
for i, s := range wrappedText {
if wrappedState[i] != nil {
TopLeft.Write(s, wrappedState[i], wrappedOpt[i])
Expand All @@ -72,13 +71,16 @@ func writeUpdate(ctx context.Context, TopLeft *text.Text, BottomLeft *text.Text,
}
}

newsreaderOutput := newsreader.NewsReaderPrint(viper.GetString("newsreader.url"))
if err := BottomLeft.Write(fmt.Sprintf("%s\n", newsreaderOutput), text.WriteReplace()); err != nil {
panic(err)
rsswrappedText, rsswrappedOpt, rsswrappedState := newsreader.Print(viper.GetString("newsreader.url"))
for i, s := range rsswrappedText {
if rsswrappedState[i] != nil {
BottomLeft.Write(s, rsswrappedState[i], rsswrappedOpt[i])
} else {
BottomLeft.Write(s, rsswrappedOpt[i])
}
}

newswrappedText, newswrappedOpt, newswrappedState := news.NewsPrint(viper.GetString("news.url"), viper.GetString("news.api_key"))

newswrappedText, newswrappedOpt, newswrappedState := news.Print(viper.GetString("news.url"), viper.GetString("news.api_key"))
for i, s := range newswrappedText {
if newswrappedState[i] != nil {
TopRight.Write(s, newswrappedState[i], newswrappedOpt[i])
Expand All @@ -97,39 +99,37 @@ func main() {
fmt.Println("Starting the application...")

readConfig()
//printConfig()

var wrappedText []string
var wrappedOpt []text.WriteOption
var wrappedState []text.WriteOption
var newswrappedText []string
var newswrappedOpt []text.WriteOption
var newswrappedState []text.WriteOption
var rsswrappedText []string
var rsswrappedOpt []text.WriteOption
var rsswrappedState []text.WriteOption

var newsreaderOutput string
// var newsOutput string

// Call and wait till all are finished.
var wg sync.WaitGroup
wg.Add(4)

go func() {
wrappedText, wrappedOpt, wrappedState = weather.WeatherPrint(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
wrappedText, wrappedOpt, wrappedState = weather.Print(viper.GetString("weather.zip"), viper.GetString("weather.api_key"))
wg.Done()
}()

go func() {
stocks.StockPrint(viper.GetString("stocks.symbol"), viper.GetString("stocks.api_key"))
stocks.Print(viper.GetString("stocks.symbol"), viper.GetString("stocks.api_key"))
wg.Done()
}()

go func() {
newsreaderOutput = newsreader.NewsReaderPrint(viper.GetString("newsreader.url"))
rsswrappedText, rsswrappedOpt, rsswrappedState = newsreader.Print(viper.GetString("newsreader.url"))
wg.Done()
}()

go func() {
newswrappedText, newswrappedOpt, newswrappedState = news.NewsPrint(viper.GetString("news.url"), viper.GetString("news.api_key"))
newswrappedText, newswrappedOpt, newswrappedState = news.Print(viper.GetString("news.url"), viper.GetString("news.api_key"))
wg.Done()
}()

Expand All @@ -143,6 +143,7 @@ func main() {
defer t.Close()

borderlessTopLeft, err := text.New(text.WrapAtWords())
borderlessBottomLeft, err := text.New(text.WrapAtWords())
borderlessTopRight, err := text.New(text.WrapAtWords())

for i, s := range wrappedText {
Expand All @@ -161,13 +162,12 @@ func main() {
}
}

borderlessBottomLeft, err := text.New(text.WrapAtWords())
if err != nil {
panic(err)
}

if err := borderlessBottomLeft.Write(newsreaderOutput, text.WriteReplace()); err != nil {
panic(err)
for i, s := range rsswrappedText {
if rsswrappedState[i] != nil {
borderlessBottomLeft.Write(s, rsswrappedState[i], rsswrappedOpt[i])
} else {
borderlessBottomLeft.Write(s, rsswrappedOpt[i])
}
}

go writeUpdate(ctx, borderlessTopLeft, borderlessBottomLeft, borderlessTopRight, 10*time.Second)
Expand Down

0 comments on commit a48c036

Please sign in to comment.