@@ -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")
2324var lat , lon float64
2425var instapaper bool
2526var openaiApiKey string
27+ var reading_time bool
2628var myMap []RSS
2729var 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+
5775func 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