fix(podcast): add user agent to avoid 403s with some remotes
* Found a podcast that returns 403 without a UserAgent string so I added one. URL is https://feeds.buzzsprout.com/132359.rss * Fixed lint issues including needed error checking.
This commit is contained in:
@@ -25,6 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const downloadAllWaitInterval = 3 * time.Second
|
const downloadAllWaitInterval = 3 * time.Second
|
||||||
|
const fetchUserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`
|
||||||
|
|
||||||
type Podcasts struct {
|
type Podcasts struct {
|
||||||
db *db.DB
|
db *db.DB
|
||||||
@@ -355,8 +356,14 @@ func (p *Podcasts) DownloadEpisode(episodeID int) error {
|
|||||||
}
|
}
|
||||||
podcastEpisode.Status = db.PodcastEpisodeStatusDownloading
|
podcastEpisode.Status = db.PodcastEpisodeStatusDownloading
|
||||||
p.db.Save(&podcastEpisode)
|
p.db.Save(&podcastEpisode)
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("GET", podcastEpisode.AudioURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create http request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Add("User-Agent", fetchUserAgent)
|
||||||
// nolint: bodyclose
|
// nolint: bodyclose
|
||||||
resp, err := http.Get(podcastEpisode.AudioURL)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("fetch podcast audio: %w", err)
|
return fmt.Errorf("fetch podcast audio: %w", err)
|
||||||
}
|
}
|
||||||
@@ -420,11 +427,17 @@ func (p *Podcasts) downloadPodcastCover(podPath string, podcast *db.Podcast) err
|
|||||||
return fmt.Errorf("parse image url: %w", err)
|
return fmt.Errorf("parse image url: %w", err)
|
||||||
}
|
}
|
||||||
ext := path.Ext(imageURL.Path)
|
ext := path.Ext(imageURL.Path)
|
||||||
resp, err := http.Get(podcast.ImageURL)
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("GET", podcast.ImageURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create http request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Add("User-Agent", fetchUserAgent)
|
||||||
|
// nolint: bodyclose
|
||||||
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("fetch image url: %w", err)
|
return fmt.Errorf("fetch image url: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
if ext == "" {
|
if ext == "" {
|
||||||
contentHeader := resp.Header.Get("content-disposition")
|
contentHeader := resp.Header.Get("content-disposition")
|
||||||
filename, _ := getContentDispositionFilename(contentHeader)
|
filename, _ := getContentDispositionFilename(contentHeader)
|
||||||
|
|||||||
Reference in New Issue
Block a user