Skip to content

Commit

Permalink
Add support for custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
csfrancis committed Nov 18, 2024
1 parent bca689b commit 28d537d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Config struct {
RefreshInterval time.Duration
RefreshIntervalStr string `yaml:"refreshInterval,omitempty" default:"12h"`

UserAgent string `yaml:"userAgent,omitempty" default:""`

Filters []*Filter `yaml:"filters"`
}

Expand Down
20 changes: 16 additions & 4 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,19 @@ func (pl *playlistLoader) OnPlaylistEnd() {
}
}

func loadReader(uri string) io.ReadCloser {
func loadReader(uri string, userAgent string) io.ReadCloser {
var err error
var reader io.ReadCloser
logger := log.WithField("uri", uri)
if isURL(uri) {
resp, err := http.Get(uri)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
logger.WithError(err).Panic("unable to create request")
}
if userAgent != "" {
req.Header.Set("User-Agent", userAgent)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
logger.WithError(err).Panic("unable to load uri")
}
Expand All @@ -158,6 +165,7 @@ type Provider struct {
iptvURL string
epgURL string
baseAddress string
userAgent string
filters []*Filter

playlist *playlistLoader
Expand All @@ -173,6 +181,10 @@ func NewProvider(config *Config) (*Provider, error) {
filters: config.Filters,
}

if len(config.UserAgent) > 0 {
provider.userAgent = config.UserAgent
}

if config.UseFFMPEG {
provider.baseAddress = config.ServerAddress
}
Expand Down Expand Up @@ -266,7 +278,7 @@ func (p *Provider) Refresh() error {
log.WithField("url", p.iptvURL).Info("loading IPTV m3u")

start := time.Now()
iptvReader := loadReader(p.iptvURL)
iptvReader := loadReader(p.iptvURL, p.userAgent)
defer iptvReader.Close()
log.WithField("duration", time.Since(start)).Debug("loaded IPTV m3u")

Expand All @@ -282,7 +294,7 @@ func (p *Provider) Refresh() error {
log.WithField("url", p.epgURL).Info("loading EPG")

start = time.Now()
epgReader := loadReader(p.epgURL)
epgReader := loadReader(p.epgURL, p.userAgent)
defer epgReader.Close()
log.WithField("duration", time.Since(start)).Debug("loaded EPG")

Expand Down

0 comments on commit 28d537d

Please sign in to comment.