json.go 4.2 KB

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