From e44b4b254036350a2a8e33c68caaf368f13a45b8 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Tue, 16 Jun 2020 22:52:20 +0200 Subject: [PATCH] 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. --- reader/subscription/finder.go | 47 ++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) 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 +}