reading_time.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "errors"
  6. "fmt"
  7. "log/slog"
  8. "strconv"
  9. "github.com/PuerkitoBio/goquery"
  10. "miniflux.app/v2/internal/config"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/proxyrotator"
  13. "miniflux.app/v2/internal/reader/fetcher"
  14. "miniflux.app/v2/internal/reader/readingtime"
  15. "miniflux.app/v2/internal/storage"
  16. "miniflux.app/v2/internal/urllib"
  17. )
  18. func fetchWatchTime(websiteURL, query string, isoDate bool) (int, error) {
  19. requestBuilder := fetcher.NewRequestBuilder().
  20. WithTimeout(config.Opts.HTTPClientTimeout()).
  21. WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  22. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
  23. defer responseHandler.Close()
  24. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  25. slog.Warn("Unable to fetch watch time", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
  26. return 0, localizedError.Error()
  27. }
  28. doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
  29. if docErr != nil {
  30. return 0, docErr
  31. }
  32. duration, exists := doc.FindMatcher(goquery.Single(query)).Attr("content")
  33. if !exists {
  34. return 0, errors.New("duration not found")
  35. }
  36. ret := 0
  37. if isoDate {
  38. parsedDuration, err := parseISO8601Duration(duration)
  39. if err != nil {
  40. return 0, fmt.Errorf("unable to parse iso duration %s: %v", duration, err)
  41. }
  42. ret = int(parsedDuration.Minutes())
  43. } else {
  44. parsedDuration, err := strconv.Atoi(duration)
  45. if err != nil {
  46. return 0, fmt.Errorf("unable to parse duration %s: %v", duration, err)
  47. }
  48. ret = parsedDuration / 60
  49. }
  50. return ret, nil
  51. }
  52. func updateEntryReadingTime(store *storage.Storage, feed *model.Feed, entry *model.Entry, entryIsNew bool, user *model.User) {
  53. if !user.ShowReadingTime {
  54. slog.Debug("Skip reading time estimation for this user", slog.Int64("user_id", user.ID))
  55. return
  56. }
  57. // Define watch time fetching scenarios
  58. watchTimeScenarios := [...]struct {
  59. shouldFetch func(*model.Entry) bool
  60. fetchFunc func(string) (int, error)
  61. platform string
  62. }{
  63. {shouldFetchYouTubeWatchTimeForSingleEntry, fetchYouTubeWatchTimeForSingleEntry, "YouTube"},
  64. {shouldFetchNebulaWatchTime, fetchNebulaWatchTime, "Nebula"},
  65. {shouldFetchOdyseeWatchTime, fetchOdyseeWatchTime, "Odysee"},
  66. {shouldFetchBilibiliWatchTime, fetchBilibiliWatchTime, "Bilibili"},
  67. }
  68. // Iterate through scenarios and attempt to fetch watch time
  69. for _, scenario := range watchTimeScenarios {
  70. if scenario.shouldFetch(entry) {
  71. if entryIsNew {
  72. if watchTime, err := scenario.fetchFunc(entry.URL); err != nil {
  73. slog.Warn("Unable to fetch watch time",
  74. slog.String("platform", scenario.platform),
  75. slog.Int64("user_id", user.ID),
  76. slog.Int64("entry_id", entry.ID),
  77. slog.String("entry_url", entry.URL),
  78. slog.Int64("feed_id", feed.ID),
  79. slog.String("feed_url", urllib.RedactedURL(feed.FeedURL)),
  80. slog.Any("error", err),
  81. )
  82. } else {
  83. entry.ReadingTime = watchTime
  84. }
  85. } else {
  86. entry.ReadingTime = store.GetReadTime(feed.ID, entry.Hash)
  87. }
  88. break
  89. }
  90. }
  91. // Fallback to text-based reading time estimation
  92. if entry.ReadingTime == 0 && entry.Content != "" {
  93. entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
  94. }
  95. }