-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
196 lines (170 loc) · 4.38 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"flag"
"fmt"
"net/url"
"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, finally func()) {
// finally is used to warm up the cache for the adjacent article once first article is fetched
defer func() {
if finally != nil {
go finally()
}
}()
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 fetchWebsiteWithCache(urls[0], updateTextChan, func() {
fetchWebsite(urls[1], nil) // warm up below article
})
flex := tview.NewFlex().
AddItem(list, 0, 1, true).
AddItem(textView, 0, 1, false)
offset := 0
list.SetChangedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
if index < len(urls) {
// Reset offset when a new article is selected
offset = 0
textView.ScrollTo(offset, 0)
// Fetch current website
go fetchWebsiteWithCache(urls[index], updateTextChan, nil)
textView.SetTitle(titles[index])
// Pre-fetch next and previous website if they exist
if index-1 >= 0 {
go fetchWebsiteWithCache(urls[index-1], nil, nil)
}
if index+1 < len(urls) {
go fetchWebsiteWithCache(urls[index+1], nil, nil)
}
}
})
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:
index := list.GetCurrentItem()
itemText, _ := list.GetItemText(index)
if index >= 0 && index < len(urls) {
openURL(urls[index])
} else if itemText == "Quit" {
app.Stop()
}
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) {
parsedUrl, err := url.Parse(feedURL)
if err != nil || parsedUrl.Scheme == "" {
feedURL = "https://" + feedURL
}
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()
}