add proxy option
This commit is contained in:
@@ -4,9 +4,12 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Podcast 表示一个播客及其剧集
|
||||
@@ -61,7 +64,7 @@ type Podcast struct {
|
||||
}
|
||||
|
||||
// LoadFeed 从URL加载播客RSS订阅,timeout为超时时间,如果timeout<=0则使用默认值30秒
|
||||
func LoadFeed(url string, timeout time.Duration) (*Podcast, error) {
|
||||
func LoadFeed(feedURL, proxy string, timeout time.Duration) (*Podcast, error) {
|
||||
if timeout <= 0 {
|
||||
timeout = 30 * time.Second
|
||||
}
|
||||
@@ -70,7 +73,24 @@ func LoadFeed(url string, timeout time.Duration) (*Podcast, error) {
|
||||
defer cancel()
|
||||
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURLWithContext(url, ctx)
|
||||
|
||||
fp.Client = &http.Client{}
|
||||
// setup proxy
|
||||
if proxy != "" {
|
||||
proxyURL, err := url.Parse(proxy)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("Failed to parse proxy url: %s", proxy)
|
||||
return nil, err
|
||||
}
|
||||
fp.Client = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
},
|
||||
}
|
||||
logrus.Infof("Using proxy: %s", proxy)
|
||||
}
|
||||
|
||||
feed, err := fp.ParseURLWithContext(feedURL, ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user