2023-06-19 14:42:47 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2023-08-10 19:46:45 -07:00
|
|
|
package subscription // import "miniflux.app/v2/internal/reader/subscription"
|
2017-11-19 21:10:04 -08:00
|
|
|
|
|
|
|
import (
|
2023-10-21 19:50:29 -07:00
|
|
|
"bytes"
|
2020-08-02 11:24:02 -07:00
|
|
|
"fmt"
|
2017-11-20 17:12:37 -08:00
|
|
|
"io"
|
2023-10-21 19:50:29 -07:00
|
|
|
"log/slog"
|
2024-06-13 16:30:15 +02:00
|
|
|
"net/url"
|
2020-08-02 11:24:02 -07:00
|
|
|
"regexp"
|
2024-06-13 16:30:15 +02:00
|
|
|
"strings"
|
2017-11-20 17:12:37 -08:00
|
|
|
|
2023-08-10 19:46:45 -07:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-10-22 14:10:56 -04:00
|
|
|
"miniflux.app/v2/internal/integration/rssbridge"
|
2023-10-21 19:50:29 -07:00
|
|
|
"miniflux.app/v2/internal/locale"
|
2023-10-22 16:07:06 -07:00
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-21 19:50:29 -07:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-10 19:46:45 -07:00
|
|
|
"miniflux.app/v2/internal/reader/parser"
|
2023-08-13 19:09:01 -07:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2017-11-19 21:10:04 -08:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2024-03-20 20:44:41 +01:00
|
|
|
"golang.org/x/net/html/charset"
|
2017-11-19 21:10:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-06-13 16:30:15 +02:00
|
|
|
youtubeHostRegex = regexp.MustCompile(`youtube\.com$`)
|
|
|
|
youtubeChannelRegex = regexp.MustCompile(`channel/(.*)$`)
|
2017-11-19 21:10:04 -08:00
|
|
|
)
|
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
type SubscriptionFinder struct {
|
|
|
|
requestBuilder *fetcher.RequestBuilder
|
|
|
|
feedDownloaded bool
|
|
|
|
feedResponseInfo *model.FeedCreationRequestFromSubscriptionDiscovery
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSubscriptionFinder(requestBuilder *fetcher.RequestBuilder) *SubscriptionFinder {
|
|
|
|
return &SubscriptionFinder{
|
|
|
|
requestBuilder: requestBuilder,
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 11:24:02 -07:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
func (f *SubscriptionFinder) IsFeedAlreadyDownloaded() bool {
|
|
|
|
return f.feedDownloaded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SubscriptionFinder) FeedResponseInfo() *model.FeedCreationRequestFromSubscriptionDiscovery {
|
|
|
|
return f.feedResponseInfo
|
|
|
|
}
|
2023-10-21 19:50:29 -07:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
func (f *SubscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
|
|
|
responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(websiteURL))
|
2023-10-21 19:50:29 -07:00
|
|
|
defer responseHandler.Close()
|
2020-09-10 14:28:54 +08:00
|
|
|
|
2023-10-21 19:50:29 -07:00
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to find subscriptions", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return nil, localizedError
|
2020-09-10 14:28:54 +08:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:50:29 -07:00
|
|
|
responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
|
|
|
|
if localizedError != nil {
|
|
|
|
slog.Warn("Unable to find subscriptions", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return nil, localizedError
|
2018-01-19 22:42:55 -08:00
|
|
|
}
|
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
f.feedResponseInfo = &model.FeedCreationRequestFromSubscriptionDiscovery{
|
|
|
|
Content: bytes.NewReader(responseBody),
|
|
|
|
ETag: responseHandler.ETag(),
|
|
|
|
LastModified: responseHandler.LastModified(),
|
|
|
|
}
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 1) Check if the website URL is already a feed.
|
2024-03-12 13:11:56 +01:00
|
|
|
if feedFormat, _ := parser.DetectFeedFormat(f.feedResponseInfo.Content); feedFormat != parser.FormatUnknown {
|
2023-10-22 16:07:06 -07:00
|
|
|
f.feedDownloaded = true
|
|
|
|
return Subscriptions{NewSubscription(responseHandler.EffectiveURL(), responseHandler.EffectiveURL(), feedFormat)}, nil
|
|
|
|
}
|
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 2) Check if the website URL is a YouTube channel.
|
|
|
|
slog.Debug("Try to detect feeds from YouTube channel page", slog.String("website_url", websiteURL))
|
|
|
|
if subscriptions, localizedError := f.FindSubscriptionsFromYouTubeChannelPage(websiteURL); localizedError != nil {
|
|
|
|
return nil, localizedError
|
|
|
|
} else if len(subscriptions) > 0 {
|
|
|
|
slog.Debug("Subscriptions found from YouTube channel page", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 3) Check if the website URL is a YouTube playlist.
|
|
|
|
slog.Debug("Try to detect feeds from YouTube playlist page", slog.String("website_url", websiteURL))
|
|
|
|
if subscriptions, localizedError := f.FindSubscriptionsFromYouTubePlaylistPage(websiteURL); localizedError != nil {
|
|
|
|
return nil, localizedError
|
|
|
|
} else if len(subscriptions) > 0 {
|
|
|
|
slog.Debug("Subscriptions found from YouTube playlist page", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
|
|
|
|
return subscriptions, nil
|
2024-03-30 03:05:50 -05:00
|
|
|
}
|
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 4) Parse web page to find feeds from HTML meta tags.
|
2023-12-01 16:27:18 -08:00
|
|
|
slog.Debug("Try to detect feeds from HTML meta tags",
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("content_type", responseHandler.ContentType()),
|
|
|
|
)
|
2024-07-13 11:52:54 -07:00
|
|
|
if subscriptions, localizedError := f.FindSubscriptionsFromWebPage(websiteURL, responseHandler.ContentType(), bytes.NewReader(responseBody)); localizedError != nil {
|
2023-10-22 16:07:06 -07:00
|
|
|
return nil, localizedError
|
2024-07-13 11:52:54 -07:00
|
|
|
} else if len(subscriptions) > 0 {
|
2023-10-22 16:07:06 -07:00
|
|
|
slog.Debug("Subscriptions found from web page", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
2023-10-21 19:50:29 -07:00
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 5) Check if the website URL can use RSS-Bridge.
|
2023-10-22 16:07:06 -07:00
|
|
|
if rssBridgeURL != "" {
|
|
|
|
slog.Debug("Try to detect feeds with RSS-Bridge", slog.String("website_url", websiteURL))
|
2024-07-13 11:52:54 -07:00
|
|
|
if subscriptions, localizedError := f.FindSubscriptionsFromRSSBridge(websiteURL, rssBridgeURL); localizedError != nil {
|
2023-10-22 16:07:06 -07:00
|
|
|
return nil, localizedError
|
2024-07-13 11:52:54 -07:00
|
|
|
} else if len(subscriptions) > 0 {
|
2023-10-22 16:07:06 -07:00
|
|
|
slog.Debug("Subscriptions found from RSS-Bridge", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
|
2023-10-21 19:50:29 -07:00
|
|
|
return subscriptions, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
// Step 6) Check if the website has a known feed URL.
|
2023-10-22 16:07:06 -07:00
|
|
|
slog.Debug("Try to detect feeds from well-known URLs", slog.String("website_url", websiteURL))
|
2024-07-13 11:52:54 -07:00
|
|
|
if subscriptions, localizedError := f.FindSubscriptionsFromWellKnownURLs(websiteURL); localizedError != nil {
|
2023-10-22 16:07:06 -07:00
|
|
|
return nil, localizedError
|
2024-07-13 11:52:54 -07:00
|
|
|
} else if len(subscriptions) > 0 {
|
2023-10-22 16:07:06 -07:00
|
|
|
slog.Debug("Subscriptions found with well-known URLs", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2023-12-01 16:27:18 -08:00
|
|
|
func (f *SubscriptionFinder) FindSubscriptionsFromWebPage(websiteURL, contentType string, body io.Reader) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
2017-11-19 21:10:04 -08:00
|
|
|
queries := map[string]string{
|
2023-10-22 16:07:06 -07:00
|
|
|
"link[type='application/rss+xml']": parser.FormatRSS,
|
|
|
|
"link[type='application/atom+xml']": parser.FormatAtom,
|
|
|
|
"link[type='application/json']": parser.FormatJSON,
|
|
|
|
"link[type='application/feed+json']": parser.FormatJSON,
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
2024-03-20 20:44:41 +01:00
|
|
|
htmlDocumentReader, err := charset.NewReader(body, contentType)
|
2023-12-01 16:27:18 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(err, "error.unable_to_parse_html_document", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(htmlDocumentReader)
|
2017-11-19 21:10:04 -08:00
|
|
|
if err != nil {
|
2023-10-21 19:50:29 -07:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(err, "error.unable_to_parse_html_document", err)
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
2024-07-25 20:31:30 -07:00
|
|
|
|
|
|
|
if hrefValue, exists := doc.Find("head base").First().Attr("href"); exists {
|
|
|
|
hrefValue = strings.TrimSpace(hrefValue)
|
|
|
|
if urllib.IsAbsoluteURL(hrefValue) {
|
|
|
|
websiteURL = hrefValue
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 21:10:04 -08:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
var subscriptions Subscriptions
|
2023-12-01 13:35:24 -08:00
|
|
|
subscriptionURLs := make(map[string]bool)
|
2017-11-19 21:10:04 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if feedURL, exists := s.Attr("href"); exists {
|
2023-02-26 17:09:50 -08:00
|
|
|
if feedURL != "" {
|
2023-12-01 13:35:24 -08:00
|
|
|
subscription.URL, err = urllib.AbsoluteURL(websiteURL, feedURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-26 17:09:50 -08:00
|
|
|
}
|
2017-11-19 21:10:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if subscription.Title == "" {
|
|
|
|
subscription.Title = subscription.URL
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:35:24 -08:00
|
|
|
if subscription.URL != "" && !subscriptionURLs[subscription.URL] {
|
|
|
|
subscriptionURLs[subscription.URL] = true
|
2017-11-19 21:10:04 -08:00
|
|
|
subscriptions = append(subscriptions, subscription)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
2020-06-16 22:52:20 +02:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
func (f *SubscriptionFinder) FindSubscriptionsFromWellKnownURLs(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
2020-06-16 22:52:20 +02:00
|
|
|
knownURLs := map[string]string{
|
2024-02-26 16:58:39 +01:00
|
|
|
"atom.xml": parser.FormatAtom,
|
|
|
|
"feed.xml": parser.FormatAtom,
|
|
|
|
"feed/": parser.FormatAtom,
|
|
|
|
"rss.xml": parser.FormatRSS,
|
|
|
|
"rss/": parser.FormatRSS,
|
|
|
|
"index.rss": parser.FormatRSS,
|
|
|
|
"index.xml": parser.FormatRSS,
|
|
|
|
"feed.atom": parser.FormatAtom,
|
2020-06-16 22:52:20 +02:00
|
|
|
}
|
|
|
|
|
2023-08-13 19:09:01 -07:00
|
|
|
websiteURLRoot := urllib.RootURL(websiteURL)
|
2023-03-28 02:28:13 +08:00
|
|
|
baseURLs := []string{
|
|
|
|
// Look for knownURLs in the root.
|
|
|
|
websiteURLRoot,
|
|
|
|
}
|
2023-10-21 19:50:29 -07:00
|
|
|
|
2023-03-28 02:28:13 +08:00
|
|
|
// Look for knownURLs in current subdirectory, such as 'example.com/blog/'.
|
2023-08-13 19:09:01 -07:00
|
|
|
websiteURL, _ = urllib.AbsoluteURL(websiteURL, "./")
|
2023-03-28 02:28:13 +08:00
|
|
|
if websiteURL != websiteURLRoot {
|
|
|
|
baseURLs = append(baseURLs, websiteURL)
|
2020-06-16 22:52:20 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
var subscriptions Subscriptions
|
2023-03-28 02:28:13 +08:00
|
|
|
for _, baseURL := range baseURLs {
|
|
|
|
for knownURL, kind := range knownURLs {
|
2023-08-13 19:09:01 -07:00
|
|
|
fullURL, err := urllib.AbsoluteURL(baseURL, knownURL)
|
2023-03-28 02:28:13 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-21 19:50:29 -07:00
|
|
|
|
2023-03-28 02:28:13 +08:00
|
|
|
// Some websites redirects unknown URLs to the home page.
|
|
|
|
// As result, the list of known URLs is returned to the subscription list.
|
|
|
|
// We don't want the user to choose between invalid feed URLs.
|
2023-10-22 16:07:06 -07:00
|
|
|
f.requestBuilder.WithoutRedirects()
|
2023-03-28 02:28:13 +08:00
|
|
|
|
2023-10-22 16:07:06 -07:00
|
|
|
responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(fullURL))
|
2024-02-29 00:30:54 +01:00
|
|
|
localizedError := responseHandler.LocalizedError()
|
|
|
|
responseHandler.Close()
|
2023-10-21 19:50:29 -07:00
|
|
|
|
2024-02-29 00:30:54 +01:00
|
|
|
if localizedError != nil {
|
2024-02-26 17:07:28 +01:00
|
|
|
slog.Debug("Unable to subscribe", slog.String("fullURL", fullURL), slog.Any("error", localizedError.Error()))
|
2023-03-28 02:28:13 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-06-16 22:52:20 +02:00
|
|
|
|
2024-02-26 17:07:28 +01:00
|
|
|
subscriptions = append(subscriptions, &Subscription{
|
|
|
|
Type: kind,
|
|
|
|
Title: fullURL,
|
|
|
|
URL: fullURL,
|
|
|
|
})
|
2020-06-16 22:52:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
2023-10-22 16:07:06 -07:00
|
|
|
|
|
|
|
func (f *SubscriptionFinder) FindSubscriptionsFromRSSBridge(websiteURL, rssBridgeURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
|
|
|
slog.Debug("Trying to detect feeds using RSS-Bridge",
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("rssbridge_url", rssBridgeURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
bridges, err := rssbridge.DetectBridges(rssBridgeURL, websiteURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(err, "error.unable_to_detect_rssbridge", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug("RSS-Bridge results",
|
|
|
|
slog.String("website_url", websiteURL),
|
|
|
|
slog.String("rssbridge_url", rssBridgeURL),
|
|
|
|
slog.Int("nb_bridges", len(bridges)),
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(bridges) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 17:07:28 +01:00
|
|
|
subscriptions := make(Subscriptions, 0, len(bridges))
|
2023-10-22 16:07:06 -07:00
|
|
|
for _, bridge := range bridges {
|
|
|
|
subscriptions = append(subscriptions, &Subscription{
|
|
|
|
Title: bridge.BridgeMeta.Name,
|
|
|
|
URL: bridge.URL,
|
|
|
|
Type: parser.FormatAtom,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SubscriptionFinder) FindSubscriptionsFromYouTubeChannelPage(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
2024-07-13 11:52:54 -07:00
|
|
|
decodedUrl, err := url.Parse(websiteURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, locale.NewLocalizedErrorWrapper(err, "error.invalid_site_url", err)
|
2023-10-22 16:07:06 -07:00
|
|
|
}
|
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
if !youtubeHostRegex.MatchString(decodedUrl.Host) {
|
|
|
|
slog.Debug("This website is not a YouTube page, the regex doesn't match", slog.String("website_url", websiteURL))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-03-30 03:05:50 -05:00
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
if matches := youtubeChannelRegex.FindStringSubmatch(decodedUrl.Path); len(matches) == 2 {
|
|
|
|
feedURL := fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, matches[1])
|
2024-03-30 03:05:50 -05:00
|
|
|
return Subscriptions{NewSubscription(websiteURL, feedURL, parser.FormatAtom)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-06-13 16:30:15 +02:00
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
func (f *SubscriptionFinder) FindSubscriptionsFromYouTubePlaylistPage(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
|
2024-06-13 16:30:15 +02:00
|
|
|
decodedUrl, err := url.Parse(websiteURL)
|
|
|
|
if err != nil {
|
2024-07-13 11:52:54 -07:00
|
|
|
return nil, locale.NewLocalizedErrorWrapper(err, "error.invalid_site_url", err)
|
2024-06-13 16:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !youtubeHostRegex.MatchString(decodedUrl.Host) {
|
|
|
|
slog.Debug("This website is not a YouTube page, the regex doesn't match", slog.String("website_url", websiteURL))
|
2024-07-13 11:52:54 -07:00
|
|
|
return nil, nil
|
2024-06-13 16:30:15 +02:00
|
|
|
}
|
|
|
|
|
2024-07-13 11:52:54 -07:00
|
|
|
if (strings.HasPrefix(decodedUrl.Path, "/watch") && decodedUrl.Query().Has("list")) || strings.HasPrefix(decodedUrl.Path, "/playlist") {
|
|
|
|
playlistID := decodedUrl.Query().Get("list")
|
|
|
|
feedURL := fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?playlist_id=%s`, playlistID)
|
|
|
|
return Subscriptions{NewSubscription(websiteURL, feedURL, parser.FormatAtom)}, nil
|
2024-06-13 16:30:15 +02:00
|
|
|
}
|
2024-07-13 11:52:54 -07:00
|
|
|
|
|
|
|
return nil, nil
|
2024-06-13 16:30:15 +02:00
|
|
|
}
|