bilibili.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "encoding/json"
  6. "fmt"
  7. "log/slog"
  8. "regexp"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/model"
  11. "miniflux.app/v2/internal/proxyrotator"
  12. "miniflux.app/v2/internal/reader/fetcher"
  13. )
  14. var (
  15. bilibiliURLRegex = regexp.MustCompile(`bilibili\.com/video/(.*)$`)
  16. bilibiliVideoIdRegex = regexp.MustCompile(`/video/(?:av(\d+)|BV([a-zA-Z0-9]+))`)
  17. )
  18. func shouldFetchBilibiliWatchTime(entry *model.Entry) bool {
  19. if !config.Opts.FetchBilibiliWatchTime() {
  20. return false
  21. }
  22. matches := bilibiliURLRegex.FindStringSubmatch(entry.URL)
  23. urlMatchesBilibiliPattern := len(matches) == 2
  24. return urlMatchesBilibiliPattern
  25. }
  26. func extractBilibiliVideoID(websiteURL string) (string, string, error) {
  27. matches := bilibiliVideoIdRegex.FindStringSubmatch(websiteURL)
  28. if matches == nil {
  29. return "", "", fmt.Errorf("no video ID found in URL: %s", websiteURL)
  30. }
  31. if matches[1] != "" {
  32. return "aid", matches[1], nil
  33. }
  34. if matches[2] != "" {
  35. return "bvid", matches[2], nil
  36. }
  37. return "", "", fmt.Errorf("unexpected regex match result for URL: %s", websiteURL)
  38. }
  39. func fetchBilibiliWatchTime(websiteURL string) (int, error) {
  40. requestBuilder := fetcher.NewRequestBuilder()
  41. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  42. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  43. idType, videoID, extractErr := extractBilibiliVideoID(websiteURL)
  44. if extractErr != nil {
  45. return 0, extractErr
  46. }
  47. bilibiliApiURL := fmt.Sprintf("https://api.bilibili.com/x/web-interface/view?%s=%s", idType, videoID)
  48. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(bilibiliApiURL))
  49. defer responseHandler.Close()
  50. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  51. slog.Warn("Unable to fetch Bilibili API",
  52. slog.String("website_url", websiteURL),
  53. slog.String("api_url", bilibiliApiURL),
  54. slog.Any("error", localizedError.Error()))
  55. return 0, localizedError.Error()
  56. }
  57. var result map[string]interface{}
  58. doc := json.NewDecoder(responseHandler.Body(config.Opts.HTTPClientMaxBodySize()))
  59. if docErr := doc.Decode(&result); docErr != nil {
  60. return 0, fmt.Errorf("failed to decode API response: %v", docErr)
  61. }
  62. if code, ok := result["code"].(float64); !ok || code != 0 {
  63. return 0, fmt.Errorf("API returned error code: %v", result["code"])
  64. }
  65. data, ok := result["data"].(map[string]interface{})
  66. if !ok {
  67. return 0, fmt.Errorf("data field not found or not an object")
  68. }
  69. duration, ok := data["duration"].(float64)
  70. if !ok {
  71. return 0, fmt.Errorf("duration not found or not a number")
  72. }
  73. intDuration := int(duration)
  74. durationMin := intDuration / 60
  75. if intDuration%60 != 0 {
  76. durationMin++
  77. }
  78. return durationMin, nil
  79. }