Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom user agent support on rss.Parser #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type HTTPError struct {
Status string
}

const DefaultUserAgent = "Gofeed/1.0"

func (err HTTPError) Error() string {
return fmt.Sprintf("http error: %s", err.Status)
}
Expand All @@ -33,6 +35,7 @@ type Parser struct {
AtomTranslator Translator
RSSTranslator Translator
Client *http.Client
UserAgent string
rp *rss.Parser
ap *atom.Parser
}
Expand Down Expand Up @@ -82,7 +85,7 @@ func (f *Parser) ParseURL(feedURL string) (feed *Feed, err error) {
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Gofeed/1.0")
req.Header.Set("User-Agent", f.userAgent())
resp, err := client.Do(req)

if err != nil {
Expand Down Expand Up @@ -154,3 +157,10 @@ func (f *Parser) httpClient() *http.Client {
f.Client = &http.Client{}
return f.Client
}

func (f *Parser) userAgent() string {
if f.UserAgent != "" {
return f.UserAgent
}
return DefaultUserAgent
}