finder.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. )
  23. // FindSubscriptions downloads and try to find one or more subscriptions from an URL.
  24. func FindSubscriptions(websiteURL, username, password string) (Subscriptions, error) {
  25. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[FindSubscriptions] url=%s", websiteURL))
  26. clt := client.New(websiteURL)
  27. clt.WithCredentials(username, password)
  28. response, err := clt.Get()
  29. if err != nil {
  30. if _, ok := err.(errors.LocalizedError); ok {
  31. return nil, err
  32. }
  33. return nil, errors.NewLocalizedError(errConnectionFailure, err)
  34. }
  35. // Content-Length = -1 when no Content-Length header is sent
  36. if response.ContentLength == 0 {
  37. return nil, errors.NewLocalizedError(errEmptyBody)
  38. }
  39. body, err := response.NormalizeBodyEncoding()
  40. if err != nil {
  41. return nil, err
  42. }
  43. var buffer bytes.Buffer
  44. size, _ := io.Copy(&buffer, body)
  45. if size == 0 {
  46. return nil, errors.NewLocalizedError(errEmptyBody)
  47. }
  48. reader := bytes.NewReader(buffer.Bytes())
  49. if format := feed.DetectFeedFormat(reader); format != feed.FormatUnknown {
  50. var subscriptions Subscriptions
  51. subscriptions = append(subscriptions, &Subscription{
  52. Title: response.EffectiveURL,
  53. URL: response.EffectiveURL,
  54. Type: format,
  55. })
  56. return subscriptions, nil
  57. }
  58. reader.Seek(0, io.SeekStart)
  59. return parseDocument(response.EffectiveURL, bytes.NewReader(buffer.Bytes()))
  60. }
  61. func parseDocument(websiteURL string, data io.Reader) (Subscriptions, error) {
  62. var subscriptions Subscriptions
  63. queries := map[string]string{
  64. "link[type='application/rss+xml']": "rss",
  65. "link[type='application/atom+xml']": "atom",
  66. "link[type='application/json']": "json",
  67. }
  68. doc, err := goquery.NewDocumentFromReader(data)
  69. if err != nil {
  70. return nil, errors.NewLocalizedError(errUnreadableDoc, err)
  71. }
  72. for query, kind := range queries {
  73. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  74. subscription := new(Subscription)
  75. subscription.Type = kind
  76. if title, exists := s.Attr("title"); exists {
  77. subscription.Title = title
  78. } else {
  79. subscription.Title = "Feed"
  80. }
  81. if feedURL, exists := s.Attr("href"); exists {
  82. subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL)
  83. }
  84. if subscription.Title == "" {
  85. subscription.Title = subscription.URL
  86. }
  87. if subscription.URL != "" {
  88. logger.Debug("[FindSubscriptions] %s", subscription)
  89. subscriptions = append(subscriptions, subscription)
  90. }
  91. })
  92. }
  93. return subscriptions, nil
  94. }