finder.go 3.0 KB

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