feat(model): enhance LoadFeed with timeout support and add tests
This commit is contained in:
@@ -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 := ""
|
||||
|
||||
Reference in New Issue
Block a user