finder.go 2.9 KB

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