diff options
author | 2020-06-16 22:52:20 +0200 | |
---|---|---|
committer | 2020-06-21 20:34:59 -0700 | |
commit | e44b4b254036350a2a8e33c68caaf368f13a45b8 (patch) | |
tree | 17eb800d621a2f955ce8d19d9d46cfb57eb6518b /reader/subscription/finder.go | |
parent | 248cb383902538afaf0e16d2d088122a86cd169a (diff) | |
download | v2-e44b4b254036350a2a8e33c68caaf368f13a45b8.tar.gz v2-e44b4b254036350a2a8e33c68caaf368f13a45b8.tar.zst v2-e44b4b254036350a2a8e33c68caaf368f13a45b8.zip |
Try known urls if no link alternate
I came across a few blogs that didn't have a link rel alternate
but offered a RSS/Atom feed.
This aims at solving this issue for "well known" feed urls, since
these urls are often the same.
Diffstat (limited to '')
-rw-r--r-- | reader/subscription/finder.go | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/reader/subscription/finder.go b/reader/subscription/finder.go index 66bbedd2..ab94b807 100644 --- a/reader/subscription/finder.go +++ b/reader/subscription/finder.go @@ -43,7 +43,11 @@ func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscr return subscriptions, nil } - return parseDocument(response.EffectiveURL, strings.NewReader(body)) + subscriptions, err := parseDocument(response.EffectiveURL, strings.NewReader(body)) + if err != nil || subscriptions != nil { + return subscriptions, err + } + return tryWellKnownUrls(websiteURL, userAgent, username, password) } func parseDocument(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) { @@ -86,3 +90,44 @@ func parseDocument(websiteURL string, data io.Reader) (Subscriptions, *errors.Lo return subscriptions, nil } + +func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) { + var subscriptions Subscriptions + knownURLs := map[string]string{ + "/atom.xml": "atom", + "/feed.xml": "atom", + "/feed/": "atom", + "/rss.xml": "rss", + } + + lastCharacter := websiteURL[len(websiteURL)-1:] + if lastCharacter == "/" { + websiteURL = websiteURL[:len(websiteURL)-1] + } + + for knownURL, kind := range knownURLs { + fullURL, err := url.AbsoluteURL(websiteURL, knownURL) + if err != nil { + continue + } + request := client.New(fullURL) + request.WithCredentials(username, password) + request.WithUserAgent(userAgent) + response, err := request.Get() + if err != nil { + continue + } + + if response != nil && response.StatusCode == 200 { + subscription := new(Subscription) + subscription.Type = kind + subscription.Title = fullURL + subscription.URL = fullURL + if subscription.URL != "" { + subscriptions = append(subscriptions, subscription) + } + } + } + + return subscriptions, nil +} |