reading_time.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package processor // import "miniflux.app/v2/internal/reader/processor"
  4. import (
  5. "log/slog"
  6. "miniflux.app/v2/internal/model"
  7. "miniflux.app/v2/internal/reader/readingtime"
  8. "miniflux.app/v2/internal/storage"
  9. )
  10. func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) {
  11. if !user.ShowReadingTime {
  12. slog.Debug("Skip reading time estimation for this user", slog.Int64("user_id", user.ID))
  13. return
  14. }
  15. // Define a type for watch time fetching functions
  16. type watchTimeFetcher func(string) (int, error)
  17. // Define watch time fetching scenarios
  18. watchTimeScenarios := []struct {
  19. shouldFetch func(*model.Entry) bool
  20. fetchFunc watchTimeFetcher
  21. platform string
  22. }{
  23. {shouldFetchYouTubeWatchTimeForSingleEntry, fetchYouTubeWatchTimeForSingleEntry, "YouTube"},
  24. {shouldFetchNebulaWatchTime, fetchNebulaWatchTime, "Nebula"},
  25. {shouldFetchOdyseeWatchTime, fetchOdyseeWatchTime, "Odysee"},
  26. {shouldFetchBilibiliWatchTime, fetchBilibiliWatchTime, "Bilibili"},
  27. }
  28. // Iterate through scenarios and attempt to fetch watch time
  29. for _, scenario := range watchTimeScenarios {
  30. if scenario.shouldFetch(entry) {
  31. if entryIsNew {
  32. if watchTime, err := scenario.fetchFunc(entry.URL); err != nil {
  33. slog.Warn("Unable to fetch watch time",
  34. slog.String("platform", scenario.platform),
  35. slog.Int64("user_id", user.ID),
  36. slog.Int64("entry_id", entry.ID),
  37. slog.String("entry_url", entry.URL),
  38. slog.Int64("feed_id", feed.ID),
  39. slog.String("feed_url", feed.FeedURL),
  40. slog.Any("error", err),
  41. )
  42. } else {
  43. entry.ReadingTime = watchTime
  44. }
  45. } else {
  46. entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
  47. }
  48. break
  49. }
  50. }
  51. // Fallback to text-based reading time estimation
  52. if entry.ReadingTime == 0 && entry.Content != "" {
  53. entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
  54. }
  55. }