Skip to content

Commit

Permalink
Making HTTP User Agent a config option
Browse files Browse the repository at this point in the history
  • Loading branch information
precurse committed Jan 11, 2025
1 parent 1c23771 commit f72fba1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ password can then be changed from the web interface
| `GONIC_TRANSCODE_CACHE_SIZE` | `-transcode-cache-size` | **optional** size of the transcode cache in MB (0 = no limit) |
| `GONIC_TRANSCODE_EJECT_INTERVAL` | `-transcode-eject-interval` | **optional** interval (in minutes) to eject transcode cache (0 = never) |
| `GONIC_EXPVAR` | `-expvar` | **optional** enable the /debug/vars endpoint (exposes useful debugging attributes as well as database stats) |
| `GONIC_HTTP_USER_AGENT` | `-http-user-agent` | **optional** sets the HTTP user agent used to fetch podcasts |

## multi valued tags (v0.16+)

Expand Down
4 changes: 3 additions & 1 deletion cmd/gonic/gonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func main() {
confPodcastPurgeAgeDays := flag.Uint("podcast-purge-age", 0, "age (in days) to purge podcast episodes if not accessed (optional)")
confPodcastPath := flag.String("podcast-path", "", "path to podcasts")

confHTTPUserAgent := flag.String("http-user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", "user agent to fetch podcasts (optional)")

confCachePath := flag.String("cache-path", "", "path to cache")

var confMusicPaths pathAliases
Expand Down Expand Up @@ -200,7 +202,7 @@ func main() {
tagReader,
*confExcludePattern,
)
podcast := podcast.New(dbc, *confPodcastPath, tagReader)
podcast := podcast.New(dbc, *confPodcastPath, tagReader, *confHTTPUserAgent)
transcoder := transcode.NewCachingTranscoder(
transcode.NewFFmpegTranscoder(),
cacheDirAudio,
Expand Down
12 changes: 5 additions & 7 deletions podcast/podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@ import (

var ErrNoAudioInFeedItem = errors.New("no audio in feed item")

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 {
httpClient *http.Client
db *db.DB
baseDir string
tagReader tagcommon.Reader
userAgent string
}

func New(db *db.DB, base string, tagReader tagcommon.Reader) *Podcasts {
func New(db *db.DB, base string, tagReader tagcommon.Reader, userAgent string) *Podcasts {
return &Podcasts{
db: db,
baseDir: base,
tagReader: tagReader,
userAgent: userAgent,
httpClient: &http.Client{},
}
}
Expand Down Expand Up @@ -331,7 +329,7 @@ func (p *Podcasts) downloadPodcastCover(podcast *db.Podcast) error {
if err != nil {
return fmt.Errorf("create http request: %w", err)
}
req.Header.Add("User-Agent", fetchUserAgent)
req.Header.Add("User-Agent", p.userAgent)

resp, err := p.httpClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -475,7 +473,7 @@ func (p *Podcasts) doPodcastDownload(podcast *db.Podcast, podcastEpisode *db.Pod
if err != nil {
return fmt.Errorf("create http request: %w", err)
}
req.Header.Add("User-Agent", fetchUserAgent)
req.Header.Add("User-Agent", p.userAgent)

resp, err := p.httpClient.Do(req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion podcast/podcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestPodcastsAndEpisodesWithSameName(t *testing.T) {
m := mockfs.New(t)

base := t.TempDir()
podcasts := New(m.DB(), base, m.TagReader())
podcasts := New(m.DB(), base, m.TagReader(), "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")

fp := gofeed.NewParser()
newFeed, err := fp.Parse(bytes.NewReader(testRSS))
Expand Down

0 comments on commit f72fba1

Please sign in to comment.