1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Add hackernews module

This commit is contained in:
Anand Sudhir Prayaga
2018-08-03 17:24:55 +02:00
parent 568276343c
commit 5c529270eb
55 changed files with 659 additions and 4 deletions

80
hackernews/client.go Normal file
View File

@@ -0,0 +1,80 @@
package hackernews
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
func GetStories(storyType string) ([]int, error) {
var stories []int
switch strings.ToLower(storyType) {
case "new", "top", "job", "ask":
resp, err := apiRequest(storyType + "stories")
if err != nil {
return stories, err
}
parseJson(&stories, resp.Body)
}
return stories, nil
}
func GetStory(id int) (Story, error) {
var story Story
resp, err := apiRequest("item/" + strconv.Itoa(id))
if err != nil {
return story, err
}
parseJson(&story, resp.Body)
return story, nil
}
/* -------------------- Unexported Functions -------------------- */
var (
apiEndpoint = "https://hacker-news.firebaseio.com/v0/"
)
func apiRequest(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", apiEndpoint+path+".json", nil)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf(resp.Status)
}
return resp, nil
}
func parseJson(obj interface{}, text io.Reader) {
jsonStream, err := ioutil.ReadAll(text)
if err != nil {
panic(err)
}
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
for {
if err := decoder.Decode(obj); err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
}

13
hackernews/story.go Normal file
View File

@@ -0,0 +1,13 @@
package hackernews
type Story struct {
By string `json:"by"`
Descendants int `json:"descendants"`
ID int `json:"id"`
Kids []int `json:"kids"`
Score int `json:"score"`
Time int `json:"time"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
}

184
hackernews/widget.go Normal file
View File

@@ -0,0 +1,184 @@
package hackernews
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/logger"
"github.com/senorprogrammer/wtf/wtf"
"net/url"
"strings"
)
const HelpText = `
Keyboard commands for Hacker News:
/: Show/hide this help window
j: Select the next story in the list
k: Select the previous story in the list
r: Refresh the data
arrow down: Select the next story in the list
arrow up: Select the previous story in the list
return: Open the selected story in a browser
`
type Widget struct {
wtf.HelpfulWidget
wtf.TextWidget
stories []Story
selected int
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget("Hacker News", "hackernews", true),
}
widget.HelpfulWidget.SetView(widget.View)
widget.unselect()
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
storyIds, err := GetStories(wtf.Config.UString("wtf.mods.hackernews.storyType", "top"))
widget.UpdateRefreshedAt()
if err != nil {
widget.View.SetWrap(true)
widget.View.SetTitle(widget.Name)
widget.View.SetText(err.Error())
} else {
var stories []Story
numberOfStoriesToDisplay := wtf.Config.UInt("wtf.mods.hackernews.numberOfStories", 10)
for idx := 0; idx <= numberOfStoriesToDisplay; idx++ {
story, e := GetStory(storyIds[idx])
logger.Log(fmt.Sprintf("stories %s", story.Title))
if e != nil {
panic(e)
} else {
stories = append(stories, story)
}
}
widget.stories = stories
}
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
if widget.stories == nil {
return
}
widget.View.SetWrap(false)
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - Stories", widget.Name)))
widget.View.SetText(widget.contentFrom(widget.stories))
}
func (widget *Widget) contentFrom(stories []Story) string {
var str string
for idx, story := range stories {
u, _ := url.Parse(story.URL)
str = str + fmt.Sprintf(
"[%s] [yellow]%d. [%s]%s [blue](%s)\n",
widget.rowColor(idx),
idx+1,
widget.rowColor(idx),
story.Title,
strings.TrimPrefix(u.Host, "www."),
)
}
return str
}
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
}
return wtf.RowColor("hackernews", idx)
}
func (widget *Widget) next() {
widget.selected++
if widget.stories != nil && widget.selected >= len(widget.stories) {
widget.selected = 0
}
widget.display()
}
func (widget *Widget) prev() {
widget.selected--
if widget.selected < 0 && widget.stories != nil {
widget.selected = len(widget.stories) - 1
}
widget.display()
}
func (widget *Widget) openStory() {
sel := widget.selected
if sel >= 0 && widget.stories != nil && sel < len(widget.stories) {
story := &widget.stories[widget.selected]
wtf.OpenFile(story.URL)
}
}
func (widget *Widget) unselect() {
widget.selected = -1
widget.display()
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "/":
widget.ShowHelp()
case "j":
widget.next()
return nil
case "k":
widget.prev()
return nil
case "r":
widget.Refresh()
return nil
}
switch event.Key() {
case tcell.KeyDown:
widget.next()
return nil
case tcell.KeyEnter:
widget.openStory()
return nil
case tcell.KeyEsc:
widget.unselect()
return event
case tcell.KeyUp:
widget.prev()
return nil
default:
return event
}
}