nebula.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package processor
  4. import (
  5. "errors"
  6. "fmt"
  7. "log/slog"
  8. "regexp"
  9. "strconv"
  10. "github.com/PuerkitoBio/goquery"
  11. "miniflux.app/v2/internal/config"
  12. "miniflux.app/v2/internal/model"
  13. "miniflux.app/v2/internal/reader/fetcher"
  14. )
  15. var nebulaRegex = regexp.MustCompile(`^https://nebula\.tv`)
  16. func shouldFetchNebulaWatchTime(entry *model.Entry) bool {
  17. if !config.Opts.FetchNebulaWatchTime() {
  18. return false
  19. }
  20. matches := nebulaRegex.FindStringSubmatch(entry.URL)
  21. return matches != nil
  22. }
  23. func fetchNebulaWatchTime(websiteURL string) (int, error) {
  24. requestBuilder := fetcher.NewRequestBuilder()
  25. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  26. requestBuilder.WithProxy(config.Opts.HTTPClientProxy())
  27. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
  28. defer responseHandler.Close()
  29. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  30. slog.Warn("Unable to fetch Nebula watch time", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
  31. return 0, localizedError.Error()
  32. }
  33. doc, docErr := goquery.NewDocumentFromReader(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
  34. if docErr != nil {
  35. return 0, docErr
  36. }
  37. durs, exists := doc.Find(`meta[property="video:duration"]`).First().Attr("content")
  38. // durs contains video watch time in seconds
  39. if !exists {
  40. return 0, errors.New("duration has not found")
  41. }
  42. dur, err := strconv.ParseInt(durs, 10, 64)
  43. if err != nil {
  44. return 0, fmt.Errorf("unable to parse duration %s: %v", durs, err)
  45. }
  46. return int(dur / 60), nil
  47. }