feat(model): enhance LoadFeed with timeout support and add tests

This commit is contained in:
2025-04-11 17:05:55 +08:00
parent 5fd117b1d1
commit 4cc6941d95
5 changed files with 114 additions and 11 deletions

View File

@@ -2,8 +2,9 @@
package model
import (
"errors"
"context"
"fmt"
"time"
"github.com/mmcdole/gofeed"
)
@@ -59,16 +60,19 @@ type Podcast struct {
Medium Medium `json:"medium,omitempty"`
}
// LoadFeed 从URL加载播客RSS订阅
func LoadFeed(url string) (*Podcast, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
// LoadFeed 从URL加载播客RSS订阅timeout为超时时间如果timeout<=0则使用默认值30秒
func LoadFeed(url string, timeout time.Duration) (*Podcast, error) {
if timeout <= 0 {
timeout = 30 * time.Second
}
if feed == nil {
return nil, errors.New("解析播客订阅失败: 空订阅")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
fp := gofeed.NewParser()
feed, err := fp.ParseURLWithContext(url, ctx)
if err != nil {
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
}
imageURL := ""