finder.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package subscription // import "miniflux.app/reader/subscription"
  5. import (
  6. "fmt"
  7. "io"
  8. "regexp"
  9. "strings"
  10. "miniflux.app/config"
  11. "miniflux.app/errors"
  12. "miniflux.app/http/client"
  13. "miniflux.app/reader/browser"
  14. "miniflux.app/reader/parser"
  15. "miniflux.app/url"
  16. "github.com/PuerkitoBio/goquery"
  17. )
  18. var (
  19. errUnreadableDoc = "Unable to analyze this page: %v"
  20. youtubeChannelRegex = regexp.MustCompile(`youtube\.com/channel/(.*)`)
  21. youtubeVideoRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  22. )
  23. // FindSubscriptions downloads and try to find one or more subscriptions from an URL.
  24. func FindSubscriptions(websiteURL, userAgent, username, password string, fetchViaProxy bool) (Subscriptions, *errors.LocalizedError) {
  25. websiteURL = findYoutubeChannelFeed(websiteURL)
  26. websiteURL = parseYoutubeVideoPage(websiteURL)
  27. clt := client.NewClientWithConfig(websiteURL, config.Opts)
  28. clt.WithCredentials(username, password)
  29. clt.WithUserAgent(userAgent)
  30. if fetchViaProxy {
  31. clt.WithProxy()
  32. }
  33. response, err := browser.Exec(clt)
  34. if err != nil {
  35. return nil, err
  36. }
  37. body := response.BodyAsString()
  38. if format := parser.DetectFeedFormat(body); format != parser.FormatUnknown {
  39. var subscriptions Subscriptions
  40. subscriptions = append(subscriptions, &Subscription{
  41. Title: response.EffectiveURL,
  42. URL: response.EffectiveURL,
  43. Type: format,
  44. })
  45. return subscriptions, nil
  46. }
  47. subscriptions, err := parseWebPage(response.EffectiveURL, strings.NewReader(body))
  48. if err != nil || subscriptions != nil {
  49. return subscriptions, err
  50. }
  51. return tryWellKnownUrls(websiteURL, userAgent, username, password)
  52. }
  53. func parseWebPage(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) {
  54. var subscriptions Subscriptions
  55. queries := map[string]string{
  56. "link[type='application/rss+xml']": "rss",
  57. "link[type='application/atom+xml']": "atom",
  58. "link[type='application/json']": "json",
  59. }
  60. doc, err := goquery.NewDocumentFromReader(data)
  61. if err != nil {
  62. return nil, errors.NewLocalizedError(errUnreadableDoc, err)
  63. }
  64. for query, kind := range queries {
  65. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  66. subscription := new(Subscription)
  67. subscription.Type = kind
  68. if title, exists := s.Attr("title"); exists {
  69. subscription.Title = title
  70. } else {
  71. subscription.Title = "Feed"
  72. }
  73. if feedURL, exists := s.Attr("href"); exists {
  74. subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
  75. }
  76. if subscription.Title == "" {
  77. subscription.Title = subscription.URL
  78. }
  79. if subscription.URL != "" {
  80. subscriptions = append(subscriptions, subscription)
  81. }
  82. })
  83. }
  84. return subscriptions, nil
  85. }
  86. func findYoutubeChannelFeed(websiteURL string) string {
  87. matches := youtubeChannelRegex.FindStringSubmatch(websiteURL)
  88. if len(matches) == 2 {
  89. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, matches[1])
  90. }
  91. return websiteURL
  92. }
  93. func parseYoutubeVideoPage(websiteURL string) string {
  94. if !youtubeVideoRegex.MatchString(websiteURL) {
  95. return websiteURL
  96. }
  97. clt := client.NewClientWithConfig(websiteURL, config.Opts)
  98. response, browserErr := browser.Exec(clt)
  99. if browserErr != nil {
  100. return websiteURL
  101. }
  102. doc, docErr := goquery.NewDocumentFromReader(response.Body)
  103. if docErr != nil {
  104. return websiteURL
  105. }
  106. if channelID, exists := doc.Find(`meta[itemprop="channelId"]`).First().Attr("content"); exists {
  107. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, channelID)
  108. }
  109. return websiteURL
  110. }
  111. func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
  112. var subscriptions Subscriptions
  113. knownURLs := map[string]string{
  114. "/atom.xml": "atom",
  115. "/feed.xml": "atom",
  116. "/feed/": "atom",
  117. "/rss.xml": "rss",
  118. }
  119. lastCharacter := websiteURL[len(websiteURL)-1:]
  120. if lastCharacter == "/" {
  121. websiteURL = websiteURL[:len(websiteURL)-1]
  122. }
  123. for knownURL, kind := range knownURLs {
  124. fullURL, err := url.AbsoluteURL(websiteURL, knownURL)
  125. if err != nil {
  126. continue
  127. }
  128. clt := client.NewClientWithConfig(fullURL, config.Opts)
  129. clt.WithCredentials(username, password)
  130. clt.WithUserAgent(userAgent)
  131. // Some websites redirects unknown URLs to the home page.
  132. // As result, the list of known URLs is returned to the subscription list.
  133. // We don't want the user to choose between invalid feed URLs.
  134. clt.WithoutRedirects()
  135. response, err := clt.Get()
  136. if err != nil {
  137. continue
  138. }
  139. if response != nil && response.StatusCode == 200 {
  140. subscription := new(Subscription)
  141. subscription.Type = kind
  142. subscription.Title = fullURL
  143. subscription.URL = fullURL
  144. if subscription.URL != "" {
  145. subscriptions = append(subscriptions, subscription)
  146. }
  147. }
  148. }
  149. return subscriptions, nil
  150. }