Skip to content

Commit f72fba1

Browse files
committed
Making HTTP User Agent a config option
1 parent 1c23771 commit f72fba1

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ password can then be changed from the web interface
7474
| `GONIC_TRANSCODE_CACHE_SIZE` | `-transcode-cache-size` | **optional** size of the transcode cache in MB (0 = no limit) |
7575
| `GONIC_TRANSCODE_EJECT_INTERVAL` | `-transcode-eject-interval` | **optional** interval (in minutes) to eject transcode cache (0 = never) |
7676
| `GONIC_EXPVAR` | `-expvar` | **optional** enable the /debug/vars endpoint (exposes useful debugging attributes as well as database stats) |
77+
| `GONIC_HTTP_USER_AGENT` | `-http-user-agent` | **optional** sets the HTTP user agent used to fetch podcasts |
7778

7879
## multi valued tags (v0.16+)
7980

cmd/gonic/gonic.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func main() {
5959
confPodcastPurgeAgeDays := flag.Uint("podcast-purge-age", 0, "age (in days) to purge podcast episodes if not accessed (optional)")
6060
confPodcastPath := flag.String("podcast-path", "", "path to podcasts")
6161

62+
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)")
63+
6264
confCachePath := flag.String("cache-path", "", "path to cache")
6365

6466
var confMusicPaths pathAliases
@@ -200,7 +202,7 @@ func main() {
200202
tagReader,
201203
*confExcludePattern,
202204
)
203-
podcast := podcast.New(dbc, *confPodcastPath, tagReader)
205+
podcast := podcast.New(dbc, *confPodcastPath, tagReader, *confHTTPUserAgent)
204206
transcoder := transcode.NewCachingTranscoder(
205207
transcode.NewFFmpegTranscoder(),
206208
cacheDirAudio,

podcast/podcast.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,20 @@ import (
2525

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

28-
const (
29-
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`
30-
)
31-
3228
type Podcasts struct {
3329
httpClient *http.Client
3430
db *db.DB
3531
baseDir string
3632
tagReader tagcommon.Reader
33+
userAgent string
3734
}
3835

39-
func New(db *db.DB, base string, tagReader tagcommon.Reader) *Podcasts {
36+
func New(db *db.DB, base string, tagReader tagcommon.Reader, userAgent string) *Podcasts {
4037
return &Podcasts{
4138
db: db,
4239
baseDir: base,
4340
tagReader: tagReader,
41+
userAgent: userAgent,
4442
httpClient: &http.Client{},
4543
}
4644
}
@@ -331,7 +329,7 @@ func (p *Podcasts) downloadPodcastCover(podcast *db.Podcast) error {
331329
if err != nil {
332330
return fmt.Errorf("create http request: %w", err)
333331
}
334-
req.Header.Add("User-Agent", fetchUserAgent)
332+
req.Header.Add("User-Agent", p.userAgent)
335333

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

480478
resp, err := p.httpClient.Do(req)
481479
if err != nil {

podcast/podcast_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestPodcastsAndEpisodesWithSameName(t *testing.T) {
2424
m := mockfs.New(t)
2525

2626
base := t.TempDir()
27-
podcasts := New(m.DB(), base, m.TagReader())
27+
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")
2828

2929
fp := gofeed.NewParser()
3030
newFeed, err := fp.Parse(bytes.NewReader(testRSS))

0 commit comments

Comments
 (0)