finder.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/errors"
  11. "miniflux.app/http/client"
  12. "miniflux.app/reader/browser"
  13. "miniflux.app/reader/parser"
  14. "miniflux.app/url"
  15. "github.com/PuerkitoBio/goquery"
  16. )
  17. var (
  18. errUnreadableDoc = "Unable to analyze this page: %v"
  19. youtubeChannelRegex = regexp.MustCompile(`youtube\.com/channel/(.*)`)
  20. youtubeVideoRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  21. )
  22. // FindSubscriptions downloads and try to find one or more subscriptions from an URL.
  23. func FindSubscriptions(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
  24. websiteURL = findYoutubeChannelFeed(websiteURL)
  25. websiteURL = parseYoutubeVideoPage(websiteURL)
  26. request := client.New(websiteURL)
  27. request.WithCredentials(username, password)
  28. request.WithUserAgent(userAgent)
  29. response, err := browser.Exec(request)
  30. if err != nil {
  31. return nil, err
  32. }
  33. body := response.BodyAsString()
  34. if format := parser.DetectFeedFormat(body); format != parser.FormatUnknown {
  35. var subscriptions Subscriptions
  36. subscriptions = append(subscriptions, &Subscription{
  37. Title: response.EffectiveURL,
  38. URL: response.EffectiveURL,
  39. Type: format,
  40. })
  41. return subscriptions, nil
  42. }
  43. subscriptions, err := parseWebPage(response.EffectiveURL, strings.NewReader(body))
  44. if err != nil || subscriptions != nil {
  45. return subscriptions, err
  46. }
  47. return tryWellKnownUrls(websiteURL, userAgent, username, password)
  48. }
  49. func parseWebPage(websiteURL string, data io.Reader) (Subscriptions, *errors.LocalizedError) {
  50. var subscriptions Subscriptions
  51. queries := map[string]string{
  52. "link[type='application/rss+xml']": "rss",
  53. "link[type='application/atom+xml']": "atom",
  54. "link[type='application/json']": "json",
  55. }
  56. doc, err := goquery.NewDocumentFromReader(data)
  57. if err != nil {
  58. return nil, errors.NewLocalizedError(errUnreadableDoc, err)
  59. }
  60. for query, kind := range queries {
  61. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  62. subscription := new(Subscription)
  63. subscription.Type = kind
  64. if title, exists := s.Attr("title"); exists {
  65. subscription.Title = title
  66. } else {
  67. subscription.Title = "Feed"
  68. }
  69. if feedURL, exists := s.Attr("href"); exists {
  70. subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
  71. }
  72. if subscription.Title == "" {
  73. subscription.Title = subscription.URL
  74. }
  75. if subscription.URL != "" {
  76. subscriptions = append(subscriptions, subscription)
  77. }
  78. })
  79. }
  80. return subscriptions, nil
  81. }
  82. func findYoutubeChannelFeed(websiteURL string) string {
  83. matches := youtubeChannelRegex.FindStringSubmatch(websiteURL)
  84. if len(matches) == 2 {
  85. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, matches[1])
  86. }
  87. return websiteURL
  88. }
  89. func parseYoutubeVideoPage(websiteURL string) string {
  90. if !youtubeVideoRegex.MatchString(websiteURL) {
  91. return websiteURL
  92. }
  93. request := client.New(websiteURL)
  94. response, browserErr := browser.Exec(request)
  95. if browserErr != nil {
  96. return websiteURL
  97. }
  98. doc, docErr := goquery.NewDocumentFromReader(response.Body)
  99. if docErr != nil {
  100. return websiteURL
  101. }
  102. if channelID, exists := doc.Find(`meta[itemprop="channelId"]`).First().Attr("content"); exists {
  103. return fmt.Sprintf(`https://www.youtube.com/feeds/videos.xml?channel_id=%s`, channelID)
  104. }
  105. return websiteURL
  106. }
  107. func tryWellKnownUrls(websiteURL, userAgent, username, password string) (Subscriptions, *errors.LocalizedError) {
  108. var subscriptions Subscriptions
  109. knownURLs := map[string]string{
  110. "/atom.xml": "atom",
  111. "/feed.xml": "atom",
  112. "/feed/": "atom",
  113. "/rss.xml": "rss",
  114. }
  115. lastCharacter := websiteURL[len(websiteURL)-1:]
  116. if lastCharacter == "/" {
  117. websiteURL = websiteURL[:len(websiteURL)-1]
  118. }
  119. for knownURL, kind := range knownURLs {
  120. fullURL, err := url.AbsoluteURL(websiteURL, knownURL)
  121. if err != nil {
  122. continue
  123. }
  124. request := client.New(fullURL)
  125. request.WithCredentials(username, password)
  126. request.WithUserAgent(userAgent)
  127. response, err := request.Get()
  128. if err != nil {
  129. continue
  130. }
  131. if response != nil && response.StatusCode == 200 {
  132. subscription := new(Subscription)
  133. subscription.Type = kind
  134. subscription.Title = fullURL
  135. subscription.URL = fullURL
  136. if subscription.URL != "" {
  137. subscriptions = append(subscriptions, subscription)
  138. }
  139. }
  140. }
  141. return subscriptions, nil
  142. }