json.go 4.2 KB

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