1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/twitch/widget.go
Chris Cummer f0ca3b8a58
Another actions test (#889)
* Another actions test

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add BuildTest action

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove lint check for the time being (so many issues)

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Fix issues found by errcheck

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Fix errors found by staticcheck

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Fix issues found by goimports

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Comment out the  action for the time being

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Fix shadowed variables

Signed-off-by: Chris Cummer <chriscummer@me.com>

* go mod tidy

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove buildtest.yml

Signed-off-by: Chris Cummer <chriscummer@me.com>

* go mod tidy

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-05-09 12:51:08 -07:00

135 lines
3.1 KiB
Go

package twitch
import (
"errors"
"fmt"
"github.com/nicklaw5/helix"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.KeyboardWidget
view.ScrollableWidget
settings *Settings
err error
twitch *Twitch
topStreams []*Stream
}
type Stream struct {
Streamer string
ViewerCount int
Language string
GameID string
Title string
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
ScrollableWidget: view.NewScrollableWidget(app, settings.common),
settings: settings,
twitch: NewClient(settings.clientId),
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.KeyboardWidget.SetView(widget.View)
return widget
}
func (widget *Widget) Refresh() {
response, err := widget.twitch.TopStreams(&helix.StreamsParams{
First: widget.settings.numberOfResults,
GameIDs: widget.settings.gameIds,
Language: widget.settings.languages,
Type: widget.settings.streamType,
UserIDs: widget.settings.gameIds,
UserLogins: widget.settings.userLogins,
})
if err != nil {
handleError(widget, err)
} else if response.ErrorMessage != "" {
handleError(widget, errors.New(response.ErrorMessage))
} else {
streams := makeStreams(response)
widget.topStreams = streams
widget.err = nil
if len(streams) <= widget.settings.numberOfResults {
widget.SetItemCount(len(widget.topStreams))
} else {
widget.topStreams = streams[:widget.settings.numberOfResults]
widget.SetItemCount(len(widget.topStreams))
}
}
widget.Render()
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
func makeStreams(response *helix.StreamsResponse) []*Stream {
streams := make([]*Stream, 0)
for _, b := range response.Data.Streams {
streams = append(streams, &Stream{
b.UserName,
b.ViewerCount,
b.Language,
b.GameID,
b.Title,
})
}
return streams
}
func handleError(widget *Widget, err error) {
widget.err = err
widget.topStreams = nil
widget.SetItemCount(0)
}
func (widget *Widget) content() (string, string, bool) {
var title = "Top Streams"
if widget.CommonSettings().Title != "" {
title = widget.CommonSettings().Title
}
if widget.err != nil {
return title, widget.err.Error(), true
}
if len(widget.topStreams) == 0 {
return title, "No data", false
}
var str string
for idx, stream := range widget.topStreams {
row := fmt.Sprintf(
"[%s]%2d. [red]%s [white]%s",
widget.RowColor(idx),
idx+1,
utils.PrettyNumber(float64(stream.ViewerCount)),
stream.Streamer,
)
str += utils.HighlightableHelper(widget.View, row, idx, len(stream.Streamer))
}
return title, str, false
}
// Opens stream in the browser
func (widget *Widget) openTwitch() {
sel := widget.GetSelected()
if sel >= 0 && widget.topStreams != nil && sel < len(widget.topStreams) {
stream := widget.topStreams[sel]
fullLink := "https://twitch.com/" + stream.Streamer
utils.OpenFile(fullLink)
}
}