json.go 4.1 KB

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