finder.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "time"
  10. "github.com/miniflux/miniflux/errors"
  11. "github.com/miniflux/miniflux/http/client"
  12. "github.com/miniflux/miniflux/logger"
  13. "github.com/miniflux/miniflux/reader/feed"
  14. "github.com/miniflux/miniflux/timer"
  15. "github.com/miniflux/miniflux/url"
  16. "github.com/PuerkitoBio/goquery"
  17. )
  18. var (
  19. errConnectionFailure = "Unable to open this link: %v"
  20. errUnreadableDoc = "Unable to analyze this page: %v"
  21. errEmptyBody = "This web page is empty"
  22. errNotAuthorized = "You are not authorized to access this resource (invalid username/password)"
  23. errServerFailure = "Unable to fetch this resource (Status Code = %d)"
  24. )
  25. // FindSubscriptions downloads and try to find one or more subscriptions from an URL.
  26. func FindSubscriptions(websiteURL, username, password string) (Subscriptions, error) {
  27. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[FindSubscriptions] url=%s", websiteURL))
  28. clt := client.New(websiteURL)
  29. clt.WithCredentials(username, password)
  30. response, err := clt.Get()
  31. if err != nil {
  32. if _, ok := err.(errors.LocalizedError); ok {
  33. return nil, err
  34. }
  35. return nil, errors.NewLocalizedError(errConnectionFailure, err)
  36. }
  37. if response.IsNotAuthorized() {
  38. return nil, errors.NewLocalizedError(errNotAuthorized)
  39. }
  40. if response.HasServerFailure() {
  41. return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
  42. }
  43. // Content-Length = -1 when no Content-Length header is sent
  44. if response.ContentLength == 0 {
  45. return nil, errors.NewLocalizedError(errEmptyBody)
  46. }
  47. body, err := response.NormalizeBodyEncoding()
  48. if err != nil {
  49. return nil, err
  50. }
  51. var buffer bytes.Buffer
  52. size, _ := io.Copy(&buffer, body)
  53. if size == 0 {
  54. return nil, errors.NewLocalizedError(errEmptyBody)
  55. }
  56. reader := bytes.NewReader(buffer.Bytes())
  57. if format := feed.DetectFeedFormat(reader); format != feed.FormatUnknown {
  58. var subscriptions Subscriptions
  59. subscriptions = append(subscriptions, &Subscription{
  60. Title: response.EffectiveURL,
  61. URL: response.EffectiveURL,
  62. Type: format,
  63. })
  64. return subscriptions, nil
  65. }
  66. reader.Seek(0, io.SeekStart)
  67. return parseDocument(response.EffectiveURL, bytes.NewReader(buffer.Bytes()))
  68. }
  69. func parseDocument(websiteURL string, data io.Reader) (Subscriptions, error) {
  70. var subscriptions Subscriptions
  71. queries := map[string]string{
  72. "link[type='application/rss+xml']": "rss",
  73. "link[type='application/atom+xml']": "atom",
  74. "link[type='application/json']": "json",
  75. }
  76. doc, err := goquery.NewDocumentFromReader(data)
  77. if err != nil {
  78. return nil, errors.NewLocalizedError(errUnreadableDoc, err)
  79. }
  80. for query, kind := range queries {
  81. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  82. subscription := new(Subscription)
  83. subscription.Type = kind
  84. if title, exists := s.Attr("title"); exists {
  85. subscription.Title = title
  86. } else {
  87. subscription.Title = "Feed"
  88. }
  89. if feedURL, exists := s.Attr("href"); exists {
  90. subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
  91. }
  92. if subscription.Title == "" {
  93. subscription.Title = subscription.URL
  94. }
  95. if subscription.URL != "" {
  96. logger.Debug("[FindSubscriptions] %s", subscription)
  97. subscriptions = append(subscriptions, subscription)
  98. }
  99. })
  100. }
  101. return subscriptions, nil
  102. }