Skip to content

Commit

Permalink
fix(podcast): add user agent to avoid 403s with some remotes
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
brian-doherty authored and sentriz committed Aug 23, 2022
1 parent 6c94f19 commit 0f80ae2
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions podcasts/podcasts.go
Expand Up @@ -25,6 +25,7 @@ import (
)

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 {
db *db.DB
Expand Down Expand Up @@ -355,8 +356,14 @@ func (p *Podcasts) DownloadEpisode(episodeID int) error {
}
podcastEpisode.Status = db.PodcastEpisodeStatusDownloading
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
resp, err := http.Get(podcastEpisode.AudioURL)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("fetch podcast audio: %w", err)
}
Expand Down Expand Up @@ -420,11 +427,17 @@ func (p *Podcasts) downloadPodcastCover(podPath string, podcast *db.Podcast) err
return fmt.Errorf("parse image url: %w", err)
}
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 {
return fmt.Errorf("fetch image url: %w", err)
}
defer resp.Body.Close()
if ext == "" {
contentHeader := resp.Header.Get("content-disposition")
filename, _ := getContentDispositionFilename(contentHeader)
Expand Down

0 comments on commit 0f80ae2

Please sign in to comment.