2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package subscription // import "miniflux.app/reader/subscription"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2017-11-21 02:12:37 +01:00
|
|
|
"io"
|
2018-10-14 20:46:41 +02:00
|
|
|
"strings"
|
2017-11-21 02:12:37 +01:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/errors"
|
|
|
|
"miniflux.app/http/client"
|
2018-10-15 06:43:48 +02:00
|
|
|
"miniflux.app/reader/browser"
|
2018-10-14 20:46:41 +02:00
|
|
|
"miniflux.app/reader/parser"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/url"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-12-27 00:26:23 +01:00
|
|
|
errUnreadableDoc = "Unable to analyze this page: %v"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FindSubscriptions downloads and try to find one or more subscriptions from an URL.
|
2018-10-15 06:43:48 +02:00
|
|
|
func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
|
|
|
|
request := client.New(websiteURL)
|
|
|
|
request.WithCredentials(username, password)
|
|
|
|
request.WithUserAgent(userAgent)
|
|
|
|
response, err := browser.Exec(request)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2018-01-20 07:42:55 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:23 +01:00
|
|
|
body := response.BodyAsString()
|
2018-10-14 20:46:41 +02:00
|
|
|
if format := parser.DetectFeedFormat(body); format != parser.FormatUnknown {
|
2017-11-20 06:10:04 +01:00
|
|
|
var subscriptions Subscriptions
|
|
|
|
subscriptions = append(subscriptions, &Subscription{
|
|
|
|
Title: response.EffectiveURL,
|
|
|
|
URL: response.EffectiveURL,
|
|
|
|
Type: format,
|
|
|
|
})
|
|
|
|
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 20:46:41 +02:00
|
|
|
return parseDocument(response.EffectiveURL, strings.NewReader(body))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 06:43:48 +02:00
|
|
|
func parseDocument(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) {
|
2017-11-20 06:10:04 +01:00
|
|
|
var subscriptions Subscriptions
|
|
|
|
queries := map[string]string{
|
|
|
|
"link[type='application/rss+xml']": "rss",
|
|
|
|
"link[type='application/atom+xml']": "atom",
|
|
|
|
"link[type='application/json']": "json",
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.NewLocalizedError(errUnreadableDoc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for query, kind := range queries {
|
|
|
|
doc.Find(query).Each(func(i int, s *goquery.Selection) {
|
|
|
|
subscription := new(Subscription)
|
|
|
|
subscription.Type = kind
|
|
|
|
|
|
|
|
if title, exists := s.Attr("title"); exists {
|
|
|
|
subscription.Title = title
|
|
|
|
} else {
|
|
|
|
subscription.Title = "Feed"
|
|
|
|
}
|
|
|
|
|
|
|
|
if feedURL, exists := s.Attr("href"); exists {
|
2017-12-02 07:29:18 +01:00
|
|
|
subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if subscription.Title == "" {
|
|
|
|
subscription.Title = subscription.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
if subscription.URL != "" {
|
|
|
|
subscriptions = append(subscriptions, subscription)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|