json.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. IconURL string `json:"icon"`
  20. FaviconURL string `json:"favicon"`
  21. FeedURL string `json:"feed_url"`
  22. Authors []jsonAuthor `json:"authors"`
  23. Author jsonAuthor `json:"author"`
  24. Items []jsonItem `json:"items"`
  25. }
  26. type jsonAuthor struct {
  27. Name string `json:"name"`
  28. URL string `json:"url"`
  29. }
  30. type jsonItem struct {
  31. ID string `json:"id"`
  32. URL string `json:"url"`
  33. Title string `json:"title"`
  34. Summary string `json:"summary"`
  35. Text string `json:"content_text"`
  36. HTML string `json:"content_html"`
  37. DatePublished string `json:"date_published"`
  38. DateModified string `json:"date_modified"`
  39. Authors []jsonAuthor `json:"authors"`
  40. Author jsonAuthor `json:"author"`
  41. Attachments []jsonAttachment `json:"attachments"`
  42. Tags []string `json:"tags"`
  43. }
  44. type jsonAttachment struct {
  45. URL string `json:"url"`
  46. MimeType string `json:"mime_type"`
  47. Title string `json:"title"`
  48. Size int64 `json:"size_in_bytes"`
  49. Duration int `json:"duration_in_seconds"`
  50. }
  51. func (j *jsonFeed) GetAuthor() string {
  52. if len(j.Authors) > 0 {
  53. return (getAuthor(j.Authors[0]))
  54. }
  55. return getAuthor(j.Author)
  56. }
  57. func (j *jsonFeed) Transform(baseURL string) *model.Feed {
  58. var err error
  59. feed := new(model.Feed)
  60. feed.FeedURL, err = url.AbsoluteURL(baseURL, j.FeedURL)
  61. if err != nil {
  62. feed.FeedURL = j.FeedURL
  63. }
  64. feed.SiteURL, err = url.AbsoluteURL(baseURL, j.SiteURL)
  65. if err != nil {
  66. feed.SiteURL = j.SiteURL
  67. }
  68. feed.IconURL = strings.TrimSpace(j.IconURL)
  69. if feed.IconURL == "" {
  70. feed.IconURL = strings.TrimSpace(j.FaviconURL)
  71. }
  72. feed.Title = strings.TrimSpace(j.Title)
  73. if feed.Title == "" {
  74. feed.Title = feed.SiteURL
  75. }
  76. for _, item := range j.Items {
  77. entry := item.Transform()
  78. entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
  79. if err == nil {
  80. entry.URL = entryURL
  81. }
  82. if entry.Author == "" {
  83. entry.Author = j.GetAuthor()
  84. }
  85. feed.Entries = append(feed.Entries, entry)
  86. }
  87. return feed
  88. }
  89. func (j *jsonItem) GetDate() time.Time {
  90. for _, value := range []string{j.DatePublished, j.DateModified} {
  91. if value != "" {
  92. d, err := date.Parse(value)
  93. if err != nil {
  94. logger.Error("json: %v", err)
  95. return time.Now()
  96. }
  97. return d
  98. }
  99. }
  100. return time.Now()
  101. }
  102. func (j *jsonItem) GetAuthor() string {
  103. if len(j.Authors) > 0 {
  104. return getAuthor(j.Authors[0])
  105. }
  106. return getAuthor(j.Author)
  107. }
  108. func (j *jsonItem) GetHash() string {
  109. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  110. if value != "" {
  111. return crypto.Hash(value)
  112. }
  113. }
  114. return ""
  115. }
  116. func (j *jsonItem) GetTitle() string {
  117. if j.Title != "" {
  118. return j.Title
  119. }
  120. for _, value := range []string{j.Summary, j.Text, j.HTML} {
  121. if value != "" {
  122. return sanitizer.TruncateHTML(value, 100)
  123. }
  124. }
  125. return j.URL
  126. }
  127. func (j *jsonItem) GetContent() string {
  128. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  129. if value != "" {
  130. return value
  131. }
  132. }
  133. return ""
  134. }
  135. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  136. enclosures := make(model.EnclosureList, 0)
  137. for _, attachment := range j.Attachments {
  138. if attachment.URL == "" {
  139. continue
  140. }
  141. enclosures = append(enclosures, &model.Enclosure{
  142. URL: attachment.URL,
  143. MimeType: attachment.MimeType,
  144. Size: attachment.Size,
  145. })
  146. }
  147. return enclosures
  148. }
  149. func (j *jsonItem) Transform() *model.Entry {
  150. entry := new(model.Entry)
  151. entry.URL = j.URL
  152. entry.Date = j.GetDate()
  153. entry.Author = j.GetAuthor()
  154. entry.Hash = j.GetHash()
  155. entry.Content = j.GetContent()
  156. entry.Title = strings.TrimSpace(j.GetTitle())
  157. entry.Enclosures = j.GetEnclosures()
  158. entry.Tags = j.Tags
  159. return entry
  160. }
  161. func getAuthor(author jsonAuthor) string {
  162. if author.Name != "" {
  163. return strings.TrimSpace(author.Name)
  164. }
  165. return ""
  166. }