bilibili.go 2.7 KB

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