2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-10-15 07:33:19 +02:00
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
package processor
|
2018-10-15 07:33:19 +02:00
|
|
|
|
|
|
|
import (
|
2021-01-27 13:50:34 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-09-25 01:32:09 +02:00
|
|
|
"log/slog"
|
2020-10-16 23:40:56 +02:00
|
|
|
"regexp"
|
2024-02-24 14:12:07 +01:00
|
|
|
"slices"
|
2021-01-27 13:50:34 +01:00
|
|
|
"strconv"
|
2024-07-03 06:03:49 +02:00
|
|
|
"strings"
|
2020-09-28 01:01:06 +02:00
|
|
|
"time"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/metric"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-22 04:50:29 +02:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-10-07 05:57:53 +02:00
|
|
|
"miniflux.app/v2/internal/reader/readingtime"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/reader/rewrite"
|
|
|
|
"miniflux.app/v2/internal/reader/sanitizer"
|
|
|
|
"miniflux.app/v2/internal/reader/scraper"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2020-11-19 02:29:40 +01:00
|
|
|
|
2021-01-27 13:50:34 +01:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2024-03-30 23:01:02 +01:00
|
|
|
"github.com/tdewolff/minify/v2"
|
|
|
|
"github.com/tdewolff/minify/v2/html"
|
2018-10-15 07:33:19 +02:00
|
|
|
)
|
|
|
|
|
2021-01-27 13:50:34 +01:00
|
|
|
var (
|
2024-02-29 05:29:06 +01:00
|
|
|
youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)$`)
|
2024-05-02 01:28:59 +02:00
|
|
|
nebulaRegex = regexp.MustCompile(`^https://nebula\.tv`)
|
2023-03-18 11:13:58 +01:00
|
|
|
odyseeRegex = regexp.MustCompile(`^https://odysee\.com`)
|
2022-07-12 06:12:26 +02:00
|
|
|
iso8601Regex = regexp.MustCompile(`^P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<week>\d+)W)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?$`)
|
|
|
|
customReplaceRuleRegex = regexp.MustCompile(`rewrite\("(.*)"\|"(.*)"\)`)
|
2021-01-27 13:50:34 +01:00
|
|
|
)
|
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
// ProcessFeedEntries downloads original web page for entries and apply filters.
|
2023-08-08 16:12:41 +02:00
|
|
|
func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, user *model.User, forceRefresh bool) {
|
2020-10-20 07:07:35 +02:00
|
|
|
var filteredEntries model.Entries
|
2020-10-16 23:40:56 +02:00
|
|
|
|
2023-03-01 17:58:01 +01:00
|
|
|
// Process older entries first
|
|
|
|
for i := len(feed.Entries) - 1; i >= 0; i-- {
|
|
|
|
entry := feed.Entries[i]
|
|
|
|
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Debug("Processing entry",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
2024-04-05 04:44:58 +02:00
|
|
|
slog.String("entry_hash", entry.Hash),
|
|
|
|
slog.String("entry_title", entry.Title),
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
)
|
2024-07-03 06:03:49 +02:00
|
|
|
if isBlockedEntry(feed, entry, user) || !isAllowedEntry(feed, entry, user) || !isRecentEntry(entry) {
|
2020-10-20 07:07:35 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
websiteURL := getUrlFromEntry(feed, entry)
|
2024-04-05 04:44:58 +02:00
|
|
|
entryIsNew := store.IsNewEntry(feed.ID, entry.Hash)
|
2023-08-08 16:12:41 +02:00
|
|
|
if feed.Crawler && (entryIsNew || forceRefresh) {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Debug("Scraping entry",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
2024-04-05 04:44:58 +02:00
|
|
|
slog.String("entry_hash", entry.Hash),
|
|
|
|
slog.String("entry_title", entry.Title),
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
2024-04-05 04:44:58 +02:00
|
|
|
slog.Bool("entry_is_new", entryIsNew),
|
|
|
|
slog.Bool("force_refresh", forceRefresh),
|
|
|
|
slog.String("website_url", websiteURL),
|
2023-09-25 01:32:09 +02:00
|
|
|
)
|
2021-03-09 05:10:53 +01:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2023-10-22 04:50:29 +02:00
|
|
|
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
2023-11-16 04:12:00 +01:00
|
|
|
requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-22 04:50:29 +02:00
|
|
|
requestBuilder.WithCookie(feed.Cookie)
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.UseProxy(feed.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
|
2024-02-25 07:08:23 +01:00
|
|
|
requestBuilder.DisableHTTP2(feed.DisableHTTP2)
|
2023-10-22 04:50:29 +02:00
|
|
|
|
|
|
|
content, scraperErr := scraper.ScrapeWebsite(
|
|
|
|
requestBuilder,
|
|
|
|
websiteURL,
|
2021-03-09 05:10:53 +01:00
|
|
|
feed.ScraperRules,
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.Opts.HasMetricsCollector() {
|
|
|
|
status := "success"
|
2020-09-28 01:01:06 +02:00
|
|
|
if scraperErr != nil {
|
2021-03-09 05:10:53 +01:00
|
|
|
status = "error"
|
2018-10-15 07:33:19 +02:00
|
|
|
}
|
2021-03-09 05:10:53 +01:00
|
|
|
metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
if scraperErr != nil {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Warn("Unable to scrape entry",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.Any("error", scraperErr),
|
|
|
|
)
|
2021-03-09 05:10:53 +01:00
|
|
|
} else if content != "" {
|
|
|
|
// We replace the entry content only if the scraper doesn't return any error.
|
2024-04-24 05:08:09 +02:00
|
|
|
entry.Content = minifyEntryContent(content)
|
2018-10-15 07:33:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
rewrite.Rewriter(websiteURL, entry, feed.RewriteRules)
|
2018-10-15 07:33:19 +02:00
|
|
|
|
|
|
|
// The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
|
2023-10-22 04:50:29 +02:00
|
|
|
entry.Content = sanitizer.Sanitize(websiteURL, entry.Content)
|
2020-10-20 07:07:35 +02:00
|
|
|
|
2021-08-30 16:53:05 +02:00
|
|
|
updateEntryReadingTime(store, feed, entry, entryIsNew, user)
|
2020-10-20 07:07:35 +02:00
|
|
|
filteredEntries = append(filteredEntries, entry)
|
2018-10-15 07:33:19 +02:00
|
|
|
}
|
2020-10-20 07:07:35 +02:00
|
|
|
|
|
|
|
feed.Entries = filteredEntries
|
2018-10-15 07:33:19 +02:00
|
|
|
}
|
2018-12-03 05:51:06 +01:00
|
|
|
|
2024-07-03 06:03:49 +02:00
|
|
|
func isBlockedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool {
|
|
|
|
if user.BlockFilterEntryRules != "" {
|
|
|
|
rules := strings.Split(user.BlockFilterEntryRules, "\n")
|
|
|
|
for _, rule := range rules {
|
|
|
|
parts := strings.SplitN(rule, "=", 2)
|
|
|
|
|
|
|
|
var match bool
|
|
|
|
switch parts[0] {
|
|
|
|
case "EntryTitle":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Title)
|
|
|
|
case "EntryURL":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.URL)
|
|
|
|
case "EntryCommentsURL":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.CommentsURL)
|
|
|
|
case "EntryContent":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Content)
|
|
|
|
case "EntryAuthor":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Author)
|
|
|
|
case "EntryTag":
|
|
|
|
containsTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
|
|
match, _ = regexp.MatchString(parts[1], tag)
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
if containsTag {
|
|
|
|
match = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match {
|
|
|
|
slog.Debug("Blocking entry based on rule",
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.String("rule", rule),
|
|
|
|
)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 14:38:13 +01:00
|
|
|
if feed.BlocklistRules == "" {
|
|
|
|
return false
|
|
|
|
}
|
2024-01-10 06:15:11 +01:00
|
|
|
|
2024-03-17 14:38:13 +01:00
|
|
|
compiledBlocklist, err := regexp.Compile(feed.BlocklistRules)
|
|
|
|
if err != nil {
|
|
|
|
slog.Debug("Failed on regexp compilation",
|
|
|
|
slog.String("pattern", feed.BlocklistRules),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
containsBlockedTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
|
|
return compiledBlocklist.MatchString(tag)
|
|
|
|
})
|
|
|
|
|
|
|
|
if compiledBlocklist.MatchString(entry.URL) || compiledBlocklist.MatchString(entry.Title) || compiledBlocklist.MatchString(entry.Author) || containsBlockedTag {
|
|
|
|
slog.Debug("Blocking entry based on rule",
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.String("rule", feed.BlocklistRules),
|
|
|
|
)
|
|
|
|
return true
|
2020-10-16 23:40:56 +02:00
|
|
|
}
|
2023-10-26 04:38:08 +02:00
|
|
|
|
2020-10-20 07:07:35 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-03 06:03:49 +02:00
|
|
|
func isAllowedEntry(feed *model.Feed, entry *model.Entry, user *model.User) bool {
|
|
|
|
if user.KeepFilterEntryRules != "" {
|
|
|
|
rules := strings.Split(user.KeepFilterEntryRules, "\n")
|
|
|
|
for _, rule := range rules {
|
|
|
|
parts := strings.SplitN(rule, "=", 2)
|
|
|
|
|
|
|
|
var match bool
|
|
|
|
switch parts[0] {
|
|
|
|
case "EntryTitle":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Title)
|
|
|
|
case "EntryURL":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.URL)
|
|
|
|
case "EntryCommentsURL":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.CommentsURL)
|
|
|
|
case "EntryContent":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Content)
|
|
|
|
case "EntryAuthor":
|
|
|
|
match, _ = regexp.MatchString(parts[1], entry.Author)
|
|
|
|
case "EntryTag":
|
|
|
|
containsTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
|
|
match, _ = regexp.MatchString(parts[1], tag)
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
if containsTag {
|
|
|
|
match = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if match {
|
|
|
|
slog.Debug("Allowing entry based on rule",
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.String("rule", rule),
|
|
|
|
)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-17 14:38:13 +01:00
|
|
|
if feed.KeeplistRules == "" {
|
|
|
|
return true
|
2020-10-16 23:40:56 +02:00
|
|
|
}
|
|
|
|
|
2024-03-17 14:38:13 +01:00
|
|
|
compiledKeeplist, err := regexp.Compile(feed.KeeplistRules)
|
2023-10-26 04:38:08 +02:00
|
|
|
if err != nil {
|
2024-03-17 14:38:13 +01:00
|
|
|
slog.Debug("Failed on regexp compilation",
|
|
|
|
slog.String("pattern", feed.KeeplistRules),
|
2023-10-26 04:38:08 +02:00
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2024-03-17 14:38:13 +01:00
|
|
|
return false
|
2023-10-26 04:38:08 +02:00
|
|
|
}
|
2024-03-17 14:38:13 +01:00
|
|
|
containsAllowedTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
|
|
|
|
return compiledKeeplist.MatchString(tag)
|
|
|
|
})
|
|
|
|
|
|
|
|
if compiledKeeplist.MatchString(entry.URL) || compiledKeeplist.MatchString(entry.Title) || compiledKeeplist.MatchString(entry.Author) || containsAllowedTag {
|
|
|
|
slog.Debug("Allow entry based on rule",
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.String("rule", feed.KeeplistRules),
|
|
|
|
)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-10-26 04:38:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
// ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
|
2021-08-30 16:53:05 +02:00
|
|
|
func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User) error {
|
2020-09-28 01:01:06 +02:00
|
|
|
startTime := time.Now()
|
2023-10-22 04:50:29 +02:00
|
|
|
websiteURL := getUrlFromEntry(feed, entry)
|
|
|
|
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
2023-11-16 04:12:00 +01:00
|
|
|
requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
|
2023-10-22 04:50:29 +02:00
|
|
|
requestBuilder.WithCookie(feed.Cookie)
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
requestBuilder.UseProxy(feed.FetchViaProxy)
|
|
|
|
requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
|
2024-02-25 07:08:23 +01:00
|
|
|
requestBuilder.DisableHTTP2(feed.DisableHTTP2)
|
2023-10-22 04:50:29 +02:00
|
|
|
|
|
|
|
content, scraperErr := scraper.ScrapeWebsite(
|
|
|
|
requestBuilder,
|
|
|
|
websiteURL,
|
|
|
|
feed.ScraperRules,
|
2021-02-21 22:42:49 +01:00
|
|
|
)
|
|
|
|
|
2020-09-28 01:01:06 +02:00
|
|
|
if config.Opts.HasMetricsCollector() {
|
|
|
|
status := "success"
|
|
|
|
if scraperErr != nil {
|
|
|
|
status = "error"
|
|
|
|
}
|
|
|
|
metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
if scraperErr != nil {
|
|
|
|
return scraperErr
|
2018-12-03 05:51:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if content != "" {
|
2024-04-24 05:08:09 +02:00
|
|
|
entry.Content = minifyEntryContent(content)
|
2024-02-29 13:07:36 +01:00
|
|
|
if user.ShowReadingTime {
|
|
|
|
entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
|
|
|
|
}
|
2018-12-03 05:51:06 +01:00
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
rewrite.Rewriter(websiteURL, entry, entry.Feed.RewriteRules)
|
|
|
|
entry.Content = sanitizer.Sanitize(websiteURL, entry.Content)
|
2023-04-08 11:02:36 +02:00
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-19 02:29:40 +01:00
|
|
|
|
2022-07-12 06:12:26 +02:00
|
|
|
func getUrlFromEntry(feed *model.Feed, entry *model.Entry) string {
|
|
|
|
var url = entry.URL
|
|
|
|
if feed.UrlRewriteRules != "" {
|
|
|
|
parts := customReplaceRuleRegex.FindStringSubmatch(feed.UrlRewriteRules)
|
|
|
|
|
|
|
|
if len(parts) >= 3 {
|
2024-05-26 22:57:14 +02:00
|
|
|
re, err := regexp.Compile(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("Failed on regexp compilation",
|
|
|
|
slog.String("url_rewrite_rules", feed.UrlRewriteRules),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
return url
|
|
|
|
}
|
2022-07-12 06:12:26 +02:00
|
|
|
url = re.ReplaceAllString(entry.URL, parts[2])
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Debug("Rewriting entry URL",
|
|
|
|
slog.String("original_entry_url", entry.URL),
|
|
|
|
slog.String("rewritten_entry_url", url),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
)
|
2022-07-12 06:12:26 +02:00
|
|
|
} else {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Debug("Cannot find search and replace terms for replace rule",
|
|
|
|
slog.String("original_entry_url", entry.URL),
|
|
|
|
slog.String("rewritten_entry_url", url),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.String("url_rewrite_rules", feed.UrlRewriteRules),
|
|
|
|
)
|
2022-07-12 06:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:53:05 +02:00
|
|
|
func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) {
|
2024-04-05 06:19:02 +02:00
|
|
|
if !user.ShowReadingTime {
|
|
|
|
slog.Debug("Skip reading time estimation for this user", slog.Int64("user_id", user.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-09 05:10:53 +01:00
|
|
|
if shouldFetchYouTubeWatchTime(entry) {
|
|
|
|
if entryIsNew {
|
|
|
|
watchTime, err := fetchYouTubeWatchTime(entry.URL)
|
|
|
|
if err != nil {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Warn("Unable to fetch YouTube watch time",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.Int64("entry_id", entry.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2021-03-09 05:10:53 +01:00
|
|
|
}
|
|
|
|
entry.ReadingTime = watchTime
|
|
|
|
} else {
|
2024-04-05 06:19:02 +02:00
|
|
|
entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
|
2021-03-09 05:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 01:28:59 +02:00
|
|
|
if shouldFetchNebulaWatchTime(entry) {
|
|
|
|
if entryIsNew {
|
|
|
|
watchTime, err := fetchNebulaWatchTime(entry.URL)
|
|
|
|
if err != nil {
|
|
|
|
slog.Warn("Unable to fetch Nebula watch time",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.Int64("entry_id", entry.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
entry.ReadingTime = watchTime
|
|
|
|
} else {
|
|
|
|
entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 11:13:58 +01:00
|
|
|
if shouldFetchOdyseeWatchTime(entry) {
|
|
|
|
if entryIsNew {
|
|
|
|
watchTime, err := fetchOdyseeWatchTime(entry.URL)
|
|
|
|
if err != nil {
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.Warn("Unable to fetch Odysee watch time",
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.Int64("entry_id", entry.ID),
|
|
|
|
slog.String("entry_url", entry.URL),
|
|
|
|
slog.Int64("feed_id", feed.ID),
|
|
|
|
slog.String("feed_url", feed.FeedURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2023-03-18 11:13:58 +01:00
|
|
|
}
|
|
|
|
entry.ReadingTime = watchTime
|
|
|
|
} else {
|
2024-04-05 06:19:02 +02:00
|
|
|
entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
|
2023-03-18 11:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 06:19:02 +02:00
|
|
|
|
2021-03-09 05:10:53 +01:00
|
|
|
// Handle YT error case and non-YT entries.
|
|
|
|
if entry.ReadingTime == 0 {
|
2024-04-05 06:19:02 +02:00
|
|
|
entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
|
2021-03-09 05:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldFetchYouTubeWatchTime(entry *model.Entry) bool {
|
|
|
|
if !config.Opts.FetchYouTubeWatchTime() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
matches := youtubeRegex.FindStringSubmatch(entry.URL)
|
|
|
|
urlMatchesYouTubePattern := len(matches) == 2
|
|
|
|
return urlMatchesYouTubePattern
|
|
|
|
}
|
|
|
|
|
2024-05-02 01:28:59 +02:00
|
|
|
func shouldFetchNebulaWatchTime(entry *model.Entry) bool {
|
|
|
|
if !config.Opts.FetchNebulaWatchTime() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
matches := nebulaRegex.FindStringSubmatch(entry.URL)
|
|
|
|
return matches != nil
|
|
|
|
}
|
|
|
|
|
2023-03-18 11:13:58 +01:00
|
|
|
func shouldFetchOdyseeWatchTime(entry *model.Entry) bool {
|
|
|
|
if !config.Opts.FetchOdyseeWatchTime() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
matches := odyseeRegex.FindStringSubmatch(entry.URL)
|
|
|
|
return matches != nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func fetchYouTubeWatchTime(websiteURL string) (int, error) {
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch YouTube page", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return 0, localizedError.Error()
|
2021-01-27 13:50:34 +01:00
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
|
2021-01-27 13:50:34 +01:00
|
|
|
if docErr != nil {
|
|
|
|
return 0, docErr
|
|
|
|
}
|
|
|
|
|
|
|
|
durs, exists := doc.Find(`meta[itemprop="duration"]`).First().Attr("content")
|
|
|
|
if !exists {
|
|
|
|
return 0, errors.New("duration has not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
dur, err := parseISO8601(durs)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to parse duration %s: %v", durs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(dur.Minutes()), nil
|
|
|
|
}
|
|
|
|
|
2024-05-02 01:28:59 +02:00
|
|
|
func fetchNebulaWatchTime(websiteURL string) (int, error) {
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch Nebula watch time", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return 0, localizedError.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
|
|
|
|
if docErr != nil {
|
|
|
|
return 0, docErr
|
|
|
|
}
|
|
|
|
|
|
|
|
durs, exists := doc.Find(`meta[property="video:duration"]`).First().Attr("content")
|
|
|
|
// durs contains video watch time in seconds
|
|
|
|
if !exists {
|
|
|
|
return 0, errors.New("duration has not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
dur, err := strconv.ParseInt(durs, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to parse duration %s: %v", durs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(dur / 60), nil
|
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
func fetchOdyseeWatchTime(websiteURL string) (int, error) {
|
|
|
|
requestBuilder := fetcher.NewRequestBuilder()
|
|
|
|
requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
|
|
|
|
requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to fetch Odysee watch time", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return 0, localizedError.Error()
|
2023-03-18 11:13:58 +01:00
|
|
|
}
|
|
|
|
|
2023-10-22 04:50:29 +02:00
|
|
|
doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
|
2023-03-18 11:13:58 +01:00
|
|
|
if docErr != nil {
|
|
|
|
return 0, docErr
|
|
|
|
}
|
|
|
|
|
|
|
|
durs, exists := doc.Find(`meta[property="og:video:duration"]`).First().Attr("content")
|
|
|
|
// durs contains video watch time in seconds
|
|
|
|
if !exists {
|
|
|
|
return 0, errors.New("duration has not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
dur, err := strconv.ParseInt(durs, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to parse duration %s: %v", durs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(dur / 60), nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:50:34 +01:00
|
|
|
// parseISO8601 parses an ISO 8601 duration string.
|
|
|
|
func parseISO8601(from string) (time.Duration, error) {
|
|
|
|
var match []string
|
|
|
|
var d time.Duration
|
|
|
|
|
|
|
|
if iso8601Regex.MatchString(from) {
|
|
|
|
match = iso8601Regex.FindStringSubmatch(from)
|
|
|
|
} else {
|
|
|
|
return 0, errors.New("could not parse duration string")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, name := range iso8601Regex.SubexpNames() {
|
|
|
|
part := match[i]
|
|
|
|
if i == 0 || name == "" || part == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.ParseInt(part, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "hour":
|
2024-03-17 21:26:51 +01:00
|
|
|
d += (time.Duration(val) * time.Hour)
|
2021-01-27 13:50:34 +01:00
|
|
|
case "minute":
|
2024-03-17 21:26:51 +01:00
|
|
|
d += (time.Duration(val) * time.Minute)
|
2021-01-27 13:50:34 +01:00
|
|
|
case "second":
|
2024-03-17 21:26:51 +01:00
|
|
|
d += (time.Duration(val) * time.Second)
|
2021-01-27 13:50:34 +01:00
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unknown field %s", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
2024-03-20 03:58:53 +01:00
|
|
|
|
|
|
|
func isRecentEntry(entry *model.Entry) bool {
|
|
|
|
if config.Opts.FilterEntryMaxAgeDays() == 0 || entry.Date.After(time.Now().AddDate(0, 0, -config.Opts.FilterEntryMaxAgeDays())) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-04-24 05:08:09 +02:00
|
|
|
|
|
|
|
func minifyEntryContent(entryContent string) string {
|
|
|
|
m := minify.New()
|
|
|
|
|
|
|
|
// Options required to avoid breaking the HTML content.
|
|
|
|
m.Add("text/html", &html.Minifier{
|
|
|
|
KeepEndTags: true,
|
|
|
|
KeepQuotes: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if minifiedHTML, err := m.String("text/html", entryContent); err == nil {
|
|
|
|
entryContent = minifiedHTML
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryContent
|
|
|
|
}
|