finder.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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, allowSelfSignedCertificates 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. clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
  31. if fetchViaProxy {
  32. clt.WithProxy()
  33. }
  34. response, err := browser.Exec(clt)
  35. if err != nil {
  36. return nil, err
  37. }
  38. body := response.BodyAsString()
  39. if format := parser.DetectFeedFormat(body); format != parser.FormatUnknown {
  40. var subscriptions Subscriptions
  41. subscriptions = append(subscriptions, &Subscription{
  42. Title: response.EffectiveURL,
  43. URL: response.EffectiveURL,
  44. Type: format,
  45. })
  46. return subscriptions, nil
  47. }
  48. subscriptions, err := parseWebPage(response.EffectiveURL, strings.NewReader(body))
  49. if err != nil || subscriptions != nil {
  50. return subscriptions, err
  51. }
  52. return tryWellKnownUrls(websiteURL, userAgent, username, password)
  53. }
  54. func parseWebPage(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) {
  55. var subscriptions Subscriptions
  56. queries := map[string]string{
  57. "link[type='application/rss+xml']": "rss",
  58. "link[type='application/atom+xml']": "atom",
  59. "link[type='application/json']": "json",
  60. }
  61. doc, err := goquery.NewDocumentFromReader(data)
  62. if err != nil {
  63. return nil, errors.NewLocalizedError(errUnreadableDoc, err)
  64. }
  65. for query, kind := range queries {
  66. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  67. subscription := new(Subscription)
  68. subscription.Type = kind
  69. if title, exists := s.Attr("title"); exists {
  70. subscription.Title = title
  71. } else {
  72. subscription.Title = "Feed"
  73. }
  74. if feedURL, exists := s.Attr("href"); exists {
  75. subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
  76. }
  77. if subscription.Title == "" {
  78. subscription.Title = subscription.URL
  79. }
  80. if subscription.URL != "" {
  81. subscriptions = append(subscriptions, subscription)
  82. }
  83. })
  84. }
  85. return subscriptions, nil
  86. }
  87. func findYoutubeChannelFeed(websiteURL string) string {
  88. matches := youtubeChannelRegex.FindStringSubmatch(websiteURL)
  89. if len(matches) == 2 {
  90. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, matches[1])
  91. }
  92. return websiteURL
  93. }
  94. func parseYoutubeVideoPage(websiteURL string) string {
  95. if !youtubeVideoRegex.MatchString(websiteURL) {
  96. return websiteURL
  97. }
  98. clt := client.NewClientWithConfig(websiteURL, config.Opts)
  99. response, browserErr := browser.Exec(clt)
  100. if browserErr != nil {
  101. return websiteURL
  102. }
  103. doc, docErr := goquery.NewDocumentFromReader(response.Body)
  104. if docErr != nil {
  105. return websiteURL
  106. }
  107. if channelID, exists := doc.Find(`meta[itemprop="channelId"]`).First().Attr("content"); exists {
  108. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, channelID)
  109. }
  110. return websiteURL
  111. }
  112. func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
  113. var subscriptions Subscriptions
  114. knownURLs := map[string]string{
  115. "/atom.xml": "atom",
  116. "/feed.xml": "atom",
  117. "/feed/": "atom",
  118. "/rss.xml": "rss",
  119. }
  120. lastCharacter := websiteURL[len(websiteURL)-1:]
  121. if lastCharacter == "/" {
  122. websiteURL = websiteURL[:len(websiteURL)-1]
  123. }
  124. for knownURL, kind := range knownURLs {
  125. fullURL, err := url.AbsoluteURL(websiteURL, knownURL)
  126. if err != nil {
  127. continue
  128. }
  129. clt := client.NewClientWithConfig(fullURL, config.Opts)
  130. clt.WithCredentials(username, password)
  131. clt.WithUserAgent(userAgent)
  132. // Some websites redirects unknown URLs to the home page.
  133. // As result, the list of known URLs is returned to the subscription list.
  134. // We don't want the user to choose between invalid feed URLs.
  135. clt.WithoutRedirects()
  136. response, err := clt.Get()
  137. if err != nil {
  138. continue
  139. }
  140. if response != nil && response.StatusCode == 200 {
  141. subscription := new(Subscription)
  142. subscription.Type = kind
  143. subscription.Title = fullURL
  144. subscription.URL = fullURL
  145. if subscription.URL != "" {
  146. subscriptions = append(subscriptions, subscription)
  147. }
  148. }
  149. }
  150. return subscriptions, nil
  151. }