4
0

json.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 json
  5. import (
  6. "strings"
  7. "time"
  8. "github.com/miniflux/miniflux/crypto"
  9. "github.com/miniflux/miniflux/logger"
  10. "github.com/miniflux/miniflux/model"
  11. "github.com/miniflux/miniflux/reader/date"
  12. "github.com/miniflux/miniflux/reader/sanitizer"
  13. "github.com/miniflux/miniflux/url"
  14. )
  15. type jsonFeed struct {
  16. Version string `json:"version"`
  17. Title string `json:"title"`
  18. SiteURL string `json:"home_page_url"`
  19. FeedURL string `json:"feed_url"`
  20. Author jsonAuthor `json:"author"`
  21. Items []jsonItem `json:"items"`
  22. }
  23. type jsonAuthor struct {
  24. Name string `json:"name"`
  25. URL string `json:"url"`
  26. }
  27. type jsonItem struct {
  28. ID string `json:"id"`
  29. URL string `json:"url"`
  30. Title string `json:"title"`
  31. Summary string `json:"summary"`
  32. Text string `json:"content_text"`
  33. HTML string `json:"content_html"`
  34. DatePublished string `json:"date_published"`
  35. DateModified string `json:"date_modified"`
  36. Author jsonAuthor `json:"author"`
  37. Attachments []jsonAttachment `json:"attachments"`
  38. }
  39. type jsonAttachment struct {
  40. URL string `json:"url"`
  41. MimeType string `json:"mime_type"`
  42. Title string `json:"title"`
  43. Size int64 `json:"size_in_bytes"`
  44. Duration int `json:"duration_in_seconds"`
  45. }
  46. func (j *jsonFeed) GetAuthor() string {
  47. return getAuthor(j.Author)
  48. }
  49. func (j *jsonFeed) Transform() *model.Feed {
  50. feed := new(model.Feed)
  51. feed.FeedURL = j.FeedURL
  52. feed.SiteURL = j.SiteURL
  53. feed.Title = strings.TrimSpace(j.Title)
  54. if feed.Title == "" {
  55. feed.Title = feed.SiteURL
  56. }
  57. for _, item := range j.Items {
  58. entry := item.Transform()
  59. entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
  60. if err == nil {
  61. entry.URL = entryURL
  62. }
  63. if entry.Author == "" {
  64. entry.Author = j.GetAuthor()
  65. }
  66. feed.Entries = append(feed.Entries, entry)
  67. }
  68. return feed
  69. }
  70. func (j *jsonItem) GetDate() time.Time {
  71. for _, value := range []string{j.DatePublished, j.DateModified} {
  72. if value != "" {
  73. d, err := date.Parse(value)
  74. if err != nil {
  75. logger.Error("json: %v", err)
  76. return time.Now()
  77. }
  78. return d
  79. }
  80. }
  81. return time.Now()
  82. }
  83. func (j *jsonItem) GetAuthor() string {
  84. return getAuthor(j.Author)
  85. }
  86. func (j *jsonItem) GetHash() string {
  87. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  88. if value != "" {
  89. return crypto.Hash(value)
  90. }
  91. }
  92. return ""
  93. }
  94. func (j *jsonItem) GetTitle() string {
  95. for _, value := range []string{j.Title, j.Summary, j.Text, j.HTML} {
  96. if value != "" {
  97. return truncate(sanitizer.StripTags(value))
  98. }
  99. }
  100. return j.URL
  101. }
  102. func (j *jsonItem) GetContent() string {
  103. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  104. if value != "" {
  105. return value
  106. }
  107. }
  108. return ""
  109. }
  110. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  111. enclosures := make(model.EnclosureList, 0)
  112. for _, attachment := range j.Attachments {
  113. enclosures = append(enclosures, &model.Enclosure{
  114. URL: attachment.URL,
  115. MimeType: attachment.MimeType,
  116. Size: attachment.Size,
  117. })
  118. }
  119. return enclosures
  120. }
  121. func (j *jsonItem) Transform() *model.Entry {
  122. entry := new(model.Entry)
  123. entry.URL = j.URL
  124. entry.Date = j.GetDate()
  125. entry.Author = j.GetAuthor()
  126. entry.Hash = j.GetHash()
  127. entry.Content = j.GetContent()
  128. entry.Title = strings.TrimSpace(j.GetTitle())
  129. entry.Enclosures = j.GetEnclosures()
  130. return entry
  131. }
  132. func getAuthor(author jsonAuthor) string {
  133. if author.Name != "" {
  134. return strings.TrimSpace(author.Name)
  135. }
  136. return ""
  137. }
  138. func truncate(str string) string {
  139. max := 100
  140. str = strings.TrimSpace(str)
  141. if len(str) > max {
  142. return str[:max] + "..."
  143. }
  144. return str
  145. }