Skip to content

Commit

Permalink
Merge pull request #24 from piqoni/add_option_to_see_reading_time
Browse files Browse the repository at this point in the history
Show estimated reading time of given article
  • Loading branch information
piqoni committed Feb 25, 2023
2 parents 4c5a007 + 7a8d38e commit 937e6d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ terminal_mode: false
opml_file_path:
markdown_file_prefix:
markdown_file_suffix:
reading_time: false
openai_api_key:
summary_feeds:
```
Expand Down
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ weather_longitude: 122.41
terminal_mode: false
opml_file_path:
markdown_file_prefix:
markdown_file_suffix:
markdown_file_suffix:
reading_time: false
openai_api_key:
summary_feeds: `

Expand Down Expand Up @@ -165,6 +166,7 @@ func bootstrapConfig() {
}

instapaper = viper.GetBool("instapaper")
reading_time = viper.GetBool("reading_time")

// Overwrite terminal_mode from config file only if its not set through -t flag
if !terminal_mode {
Expand Down
44 changes: 34 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

readability "github.com/go-shiori/go-readability"
"github.com/mmcdole/gofeed"
"github.com/savioxavier/termlink"
_ "modernc.org/sqlite"
Expand All @@ -23,6 +24,7 @@ var currentDate = time.Now().Format("2006-01-02")
var lat, lon float64
var instapaper bool
var openaiApiKey string
var reading_time bool
var myMap []RSS
var db *sql.DB

Expand All @@ -38,22 +40,38 @@ func check(e error) {
}
}

func writeLink(title string, url string, newline bool) string {
func writeLink(title string, url string, newline bool, readingTime string) string {
var content string
if terminal_mode {
content = termlink.Link(title, url)
if newline {
content += "\n"
}
} else {
content = "[" + title + "](" + url + ")"
if newline {
content += "\n"
}
}
if readingTime != "" {
content += " (" + readingTime + ")"
}
if newline {
content += "\n"
}
return content
}

func getReadingTime(link string) string {
article, err := readability.FromURL(link, 30*time.Second)
if err != nil {
return "" // Just dont display any reading time if can't get the article text
}

// get number of words in a string
words := strings.Fields(article.TextContent)

// assuming average reading time is 200 words per minute calculate reading time of the article
readingTime := float64(len(words)) / float64(200)
minutes := int(readingTime)

return strconv.Itoa(minutes) + " min"
}

func writeSummary(content string, newline bool) string {
if content == "" {
return content
Expand Down Expand Up @@ -147,6 +165,7 @@ func main() {
var date string
var summary sql.NullString
var summaryValue string
var timeInMin string

title := item.Title
link := item.Link
Expand Down Expand Up @@ -191,9 +210,9 @@ func main() {
comments_number := strings.Replace(item.Description[first_comments_index:], "</p>\n", "", -1)
comments_number_int, _ := strconv.Atoi(comments_number)
if comments_number_int < 100 {
items += writeLink("💬 ", comments_url, false)
items += writeLink("💬 ", comments_url, false, "")
} else {
items += writeLink("🔥 ", comments_url, false)
items += writeLink("🔥 ", comments_url, false, "")
}
}
if instapaper && !terminal_mode {
Expand All @@ -204,7 +223,12 @@ func main() {
if title == "" {
title = stripHtmlRegex(item.Description)
}
items += writeLink(title, link, true)
if reading_time {
timeInMin = getReadingTime(link)
} else {
timeInMin = ""
}
items += writeLink(title, link, true, timeInMin)
if rss.summarize {
items += writeSummary(summaryValue, true)
}
Expand Down

0 comments on commit 937e6d0

Please sign in to comment.