json.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 // import "miniflux.app/reader/json"
  5. import (
  6. "strings"
  7. "time"
  8. "miniflux.app/crypto"
  9. "miniflux.app/logger"
  10. "miniflux.app/model"
  11. "miniflux.app/reader/date"
  12. "miniflux.app/reader/sanitizer"
  13. "miniflux.app/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(baseURL string) *model.Feed {
  50. var err error
  51. feed := new(model.Feed)
  52. feed.FeedURL, err = url.AbsoluteURL(baseURL, j.FeedURL)
  53. if err != nil {
  54. feed.FeedURL = j.FeedURL
  55. }
  56. feed.SiteURL, err = url.AbsoluteURL(baseURL, j.SiteURL)
  57. if err != nil {
  58. feed.SiteURL = j.SiteURL
  59. }
  60. feed.Title = strings.TrimSpace(j.Title)
  61. if feed.Title == "" {
  62. feed.Title = feed.SiteURL
  63. }
  64. for _, item := range j.Items {
  65. entry := item.Transform()
  66. entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
  67. if err == nil {
  68. entry.URL = entryURL
  69. }
  70. if entry.Author == "" {
  71. entry.Author = j.GetAuthor()
  72. }
  73. feed.Entries = append(feed.Entries, entry)
  74. }
  75. return feed
  76. }
  77. func (j *jsonItem) GetDate() time.Time {
  78. for _, value := range []string{j.DatePublished, j.DateModified} {
  79. if value != "" {
  80. d, err := date.Parse(value)
  81. if err != nil {
  82. logger.Error("json: %v", err)
  83. return time.Now()
  84. }
  85. return d
  86. }
  87. }
  88. return time.Now()
  89. }
  90. func (j *jsonItem) GetAuthor() string {
  91. return getAuthor(j.Author)
  92. }
  93. func (j *jsonItem) GetHash() string {
  94. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  95. if value != "" {
  96. return crypto.Hash(value)
  97. }
  98. }
  99. return ""
  100. }
  101. func (j *jsonItem) GetTitle() string {
  102. for _, value := range []string{j.Title, j.Summary, j.Text, j.HTML} {
  103. if value != "" {
  104. return truncate(sanitizer.StripTags(value))
  105. }
  106. }
  107. return j.URL
  108. }
  109. func (j *jsonItem) GetContent() string {
  110. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  111. if value != "" {
  112. return value
  113. }
  114. }
  115. return ""
  116. }
  117. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  118. enclosures := make(model.EnclosureList, 0)
  119. for _, attachment := range j.Attachments {
  120. if attachment.URL == "" {
  121. continue
  122. }
  123. enclosures = append(enclosures, &model.Enclosure{
  124. URL: attachment.URL,
  125. MimeType: attachment.MimeType,
  126. Size: attachment.Size,
  127. })
  128. }
  129. return enclosures
  130. }
  131. func (j *jsonItem) Transform() *model.Entry {
  132. entry := new(model.Entry)
  133. entry.URL = j.URL
  134. entry.Date = j.GetDate()
  135. entry.Author = j.GetAuthor()
  136. entry.Hash = j.GetHash()
  137. entry.Content = j.GetContent()
  138. entry.Title = strings.TrimSpace(j.GetTitle())
  139. entry.Enclosures = j.GetEnclosures()
  140. return entry
  141. }
  142. func getAuthor(author jsonAuthor) string {
  143. if author.Name != "" {
  144. return strings.TrimSpace(author.Name)
  145. }
  146. return ""
  147. }
  148. func truncate(str string) string {
  149. max := 100
  150. str = strings.TrimSpace(str)
  151. if len(str) > max {
  152. return str[:max] + "..."
  153. }
  154. return str
  155. }