mmcdole / gofeed

Parse RSS, Atom and JSON feeds in Go
MIT License
2.59k stars 208 forks source link

Make userAgent changable #172

Closed asxcandrew closed 3 years ago

asxcandrew commented 3 years ago

Seems like one of resources has banned User-Agent "Gofeed/1.0", so I would like to be able to set a custom UserAgent

coveralls commented 3 years ago

Coverage Status

Coverage increased (+0.01%) to 74.08% when pulling 77c666413e5ee9461933211bc3a7d58fdb5c8c3b on asxcandrew:master into 70d9d43ec2ea0e4248f46a31f79a579e6d152b4b on mmcdole:master.

donatj commented 3 years ago

I'd ran into this myself - I'm happy to have this - I'd personally worked around this as:

type userAgentTransport struct {
    http.RoundTripper
}

func (c *userAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    r.Header.Set("User-Agent", "cronrunner-rss-reader:1.0.0")
    return c.RoundTripper.RoundTrip(r)
}

func parser() *gofeed.Parser {
    p := gofeed.NewParser()
    p.Client = &http.Client{}
    p.Client.Transport = &userAgentTransport{
        http.DefaultTransport,
    }

    return p
}