json.go 3.9 KB

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