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
  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/reader/fetcher"
  12. )
  13. var (
  14. bilibiliURLRegex = regexp.MustCompile(`bilibili\.com/video/(.*)$`)
  15. bilibiliVideoIdRegex = regexp.MustCompile(`/video/(?:av(\d+)|BV([a-zA-Z0-9]+))`)
  16. )
  17. func shouldFetchBilibiliWatchTime(entry *model.Entry) bool {
  18. if !config.Opts.FetchBilibiliWatchTime() {
  19. return false
  20. }
  21. matches := bilibiliURLRegex.FindStringSubmatch(entry.URL)
  22. urlMatchesBilibiliPattern := len(matches) == 2
  23. return urlMatchesBilibiliPattern
  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.WithProxy(config.Opts.HTTPClientProxy())
  42. idType, videoID, extractErr := extractBilibiliVideoID(websiteURL)
  43. if extractErr != nil {
  44. return 0, extractErr
  45. }
  46. bilibiliApiURL := fmt.Sprintf("https://api.bilibili.com/x/web-interface/view?%s=%s", 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]interface{}
  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]interface{})
  65. if !ok {
  66. return 0, fmt.Errorf("data field not found or not an object")
  67. }
  68. duration, ok := data["duration"].(float64)
  69. if !ok {
  70. return 0, fmt.Errorf("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. }