-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (148 loc) · 3.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"flag"
"fmt"
"os/exec"
"runtime"
"time"
"github.com/gdamore/tcell/v2"
"github.com/go-shiori/go-readability"
"github.com/mmcdole/gofeed"
"github.com/rivo/tview"
"jaytaylor.com/html2text"
)
var articleCache = make(map[string]string)
func fetchWebsiteWithCache(url string, updateTextChan chan<- string) {
if text, ok := articleCache[url]; ok {
updateTextChan <- text
} else {
fetchWebsite(url, updateTextChan)
}
}
func main() {
app := tview.NewApplication()
rssUrl := flag.String("rss", "https://feeds.bbci.co.uk/news/rss.xml", "Specify the rss url")
flag.Parse()
urls, titles := fetchRSSArticles(*rssUrl)
list := tview.NewList().ShowSecondaryText(false).SetShortcutColor(tcell.ColorDarkGray)
for i, title := range titles {
list.AddItem(title, "", rune('1'+i), nil)
}
list.AddItem("Quit", "Press to exit", 'q', func() {
app.Stop()
})
updateTextChan := make(chan string)
list.SetBorder(true)
textView := tview.NewTextView().
SetDynamicColors(true).
SetScrollable(true)
textView.SetBorder(true).SetTitle(titles[0]).SetTitleAlign(0)
go fetchWebsite(urls[0], updateTextChan)
flex := tview.NewFlex().
AddItem(list, 0, 1, true).
AddItem(textView, 0, 1, false)
list.SetChangedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
if index < len(urls) {
// Fetch current website
go fetchWebsiteWithCache(urls[index], updateTextChan)
textView.SetTitle(titles[index])
// Pre-fetch next and previous website if they exists
if index-1 >= 0 {
go fetchWebsite(urls[index-1], nil)
}
if index+1 < len(urls) {
go fetchWebsite(urls[index+1], nil)
}
}
})
offset := 0
go func() {
for {
select {
case response := <-updateTextChan:
app.QueueUpdateDraw(func() {
textView.SetText(response).SetTitleAlign(tview.AlignLeft)
})
}
}
}()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter:
openURL(urls[list.GetCurrentItem()])
return nil
case tcell.KeyRight:
offset += 1
textView.ScrollTo(offset, 0)
return nil
case tcell.KeyLeft:
offset -= 1
if offset < 0 {
offset = 0
}
textView.ScrollTo(offset, 0)
return nil
case tcell.KeyRune:
switch event.Rune() {
case 'j':
return tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone)
case 'k':
return tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone)
case 'l':
offset += 1
textView.ScrollTo(offset, 0)
return nil
case 'h':
offset -= 1
if offset < 0 {
offset = 0
}
textView.ScrollTo(offset, 0)
return nil
default:
return event
}
default:
return event
}
})
app.SetRoot(flex, true)
if err := app.Run(); err != nil {
panic(err)
}
}
// TODO: revisit this in case of unneccessary update to TextChan and make sure text in textview corresponds to selected article
func fetchWebsite(url string, updateTextChan chan<- string) {
sanitized, _ := readability.FromURL(url, 30*time.Second)
what, _ := html2text.FromString(sanitized.Content)
articleCache[url] = string(what)
updateTextChan <- string(what)
}
func fetchRSSArticles(feedURL string) (urls []string, titles []string) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedURL)
if err != nil {
fmt.Println("Error fetching or parsing feed:", err)
return
}
for _, item := range feed.Items {
urls = append(urls, item.Link)
titles = append(titles, item.Title)
}
return
}
func openURL(url string) {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
exec.Command(cmd, args...).Start()
}