Merge pull request #24 from piqoni/add_option_to_see_reading_time · piqoni/matcha@937e6d0 · GitHub
Skip to content

Commit 937e6d0

Browse files
authored
Merge pull request #24 from piqoni/add_option_to_see_reading_time
Show estimated reading time of given article
2 parents 4c5a007 + 7a8d38e commit 937e6d0

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ terminal_mode: false
5757
opml_file_path:
5858
markdown_file_prefix:
5959
markdown_file_suffix:
60+
reading_time: false
6061
openai_api_key:
6162
summary_feeds:
6263
```

config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ weather_longitude: 122.41
3131
terminal_mode: false
3232
opml_file_path:
3333
markdown_file_prefix:
34-
markdown_file_suffix:
34+
markdown_file_suffix:
35+
reading_time: false
3536
openai_api_key:
3637
summary_feeds: `
3738

@@ -165,6 +166,7 @@ func bootstrapConfig() {
165166
}
166167

167168
instapaper = viper.GetBool("instapaper")
169+
reading_time = viper.GetBool("reading_time")
168170

169171
// Overwrite terminal_mode from config file only if its not set through -t flag
170172
if !terminal_mode {

main.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
readability "github.com/go-shiori/go-readability"
1415
"github.com/mmcdole/gofeed"
1516
"github.com/savioxavier/termlink"
1617
_ "modernc.org/sqlite"
@@ -23,6 +24,7 @@ var currentDate = time.Now().Format("2006-01-02")
2324
var lat, lon float64
2425
var instapaper bool
2526
var openaiApiKey string
27+
var reading_time bool
2628
var myMap []RSS
2729
var db *sql.DB
2830

@@ -38,22 +40,38 @@ func check(e error) {
3840
}
3941
}
4042

41-
func writeLink(title string, url string, newline bool) string {
43+
func writeLink(title string, url string, newline bool, readingTime string) string {
4244
var content string
4345
if terminal_mode {
4446
content = termlink.Link(title, url)
45-
if newline {
46-
content += "\n"
47-
}
4847
} else {
4948
content = "[" + title + "](" + url + ")"
50-
if newline {
51-
content += "\n"
52-
}
49+
}
50+
if readingTime != "" {
51+
content += " (" + readingTime + ")"
52+
}
53+
if newline {
54+
content += "\n"
5355
}
5456
return content
5557
}
5658

59+
func getReadingTime(link string) string {
60+
article, err := readability.FromURL(link, 30*time.Second)
61+
if err != nil {
62+
return "" // Just dont display any reading time if can't get the article text
63+
}
64+
65+
// get number of words in a string
66+
words := strings.Fields(article.TextContent)
67+
68+
// assuming average reading time is 200 words per minute calculate reading time of the article
69+
readingTime := float64(len(words)) / float64(200)
70+
minutes := int(readingTime)
71+
72+
return strconv.Itoa(minutes) + " min"
73+
}
74+
5775
func writeSummary(content string, newline bool) string {
5876
if content == "" {
5977
return content
@@ -147,6 +165,7 @@ func main() {
147165
var date string
148166
var summary sql.NullString
149167
var summaryValue string
168+
var timeInMin string
150169

151170
title := item.Title
152171
link := item.Link
@@ -191,9 +210,9 @@ func main() {
191210
comments_number := strings.Replace(item.Description[first_comments_index:], "</p>\n", "", -1)
192211
comments_number_int, _ := strconv.Atoi(comments_number)
193212
if comments_number_int < 100 {
194-
items += writeLink("💬 ", comments_url, false)
213+
items += writeLink("💬 ", comments_url, false, "")
195214
} else {
196-
items += writeLink("🔥 ", comments_url, false)
215+
items += writeLink("🔥 ", comments_url, false, "")
197216
}
198217
}
199218
if instapaper && !terminal_mode {
@@ -204,7 +223,12 @@ func main() {
204223
if title == "" {
205224
title = stripHtmlRegex(item.Description)
206225
}
207-
items += writeLink(title, link, true)
226+
if reading_time {
227+
timeInMin = getReadingTime(link)
228+
} else {
229+
timeInMin = ""
230+
}
231+
items += writeLink(title, link, true, timeInMin)
208232
if rss.summarize {
209233
items += writeSummary(summaryValue, true)
210234
}

0 commit comments

Comments
 (0)