1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-11 17:51:01 +00:00

Calculate reading time during feed processing

The goal is to speed up the user interface.

Detecting the language based on the content is pretty slow.
This commit is contained in:
Frédéric Guillot 2020-11-18 17:29:40 -08:00 committed by fguillot
parent b1c9977711
commit de7a613098
12 changed files with 84 additions and 50 deletions

View file

@ -5,8 +5,11 @@
package processor
import (
"math"
"regexp"
"strings"
"time"
"unicode/utf8"
"miniflux.app/config"
"miniflux.app/logger"
@ -16,6 +19,8 @@ import (
"miniflux.app/reader/sanitizer"
"miniflux.app/reader/scraper"
"miniflux.app/storage"
"github.com/rylans/getlang"
)
// ProcessFeedEntries downloads original web page for entries and apply filters.
@ -58,6 +63,7 @@ func ProcessFeedEntries(store *storage.Storage, feed *model.Feed) {
// The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
entry.ReadingTime = calculateReadingTime(entry.Content)
filteredEntries = append(filteredEntries, entry)
}
@ -108,7 +114,23 @@ func ProcessEntryWebPage(entry *model.Entry) error {
if content != "" {
entry.Content = content
entry.ReadingTime = calculateReadingTime(content)
}
return nil
}
func calculateReadingTime(content string) int {
sanitizedContent := sanitizer.StripTags(content)
languageInfo := getlang.FromString(sanitizedContent)
var timeToReadInt int
if languageInfo.LanguageCode() == "ko" || languageInfo.LanguageCode() == "zh" || languageInfo.LanguageCode() == "jp" {
timeToReadInt = int(math.Ceil(float64(utf8.RuneCountInString(sanitizedContent)) / 500))
} else {
nbOfWords := len(strings.Fields(sanitizedContent))
timeToReadInt = int(math.Ceil(float64(nbOfWords) / 265))
}
return timeToReadInt
}