json.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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
  5. import (
  6. "log"
  7. "strings"
  8. "time"
  9. "github.com/miniflux/miniflux2/reader/sanitizer"
  10. "github.com/miniflux/miniflux2/helper"
  11. "github.com/miniflux/miniflux2/model"
  12. "github.com/miniflux/miniflux2/reader/date"
  13. "github.com/miniflux/miniflux2/reader/processor"
  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. 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. Author jsonAuthor `json:"author"`
  37. Attachments []jsonAttachment `json:"attachments"`
  38. }
  39. type jsonAttachment struct {
  40. URL string `json:"url"`
  41. MimeType string `json:"mime_type"`
  42. Title string `json:"title"`
  43. Size int `json:"size_in_bytes"`
  44. Duration int `json:"duration_in_seconds"`
  45. }
  46. func (j *jsonFeed) GetAuthor() string {
  47. return getAuthor(j.Author)
  48. }
  49. func (j *jsonFeed) Transform() *model.Feed {
  50. feed := new(model.Feed)
  51. feed.FeedURL = j.FeedURL
  52. feed.SiteURL = j.SiteURL
  53. feed.Title = strings.TrimSpace(j.Title)
  54. if feed.Title == "" {
  55. feed.Title = feed.SiteURL
  56. }
  57. for _, item := range j.Items {
  58. entry := item.Transform()
  59. if entry.Author == "" {
  60. entry.Author = j.GetAuthor()
  61. }
  62. feed.Entries = append(feed.Entries, entry)
  63. }
  64. return feed
  65. }
  66. func (j *jsonItem) GetDate() time.Time {
  67. for _, value := range []string{j.DatePublished, j.DateModified} {
  68. if value != "" {
  69. d, err := date.Parse(value)
  70. if err != nil {
  71. log.Println(err)
  72. return time.Now()
  73. }
  74. return d
  75. }
  76. }
  77. return time.Now()
  78. }
  79. func (j *jsonItem) GetAuthor() string {
  80. return getAuthor(j.Author)
  81. }
  82. func (j *jsonItem) GetHash() string {
  83. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  84. if value != "" {
  85. return helper.Hash(value)
  86. }
  87. }
  88. return ""
  89. }
  90. func (j *jsonItem) GetTitle() string {
  91. for _, value := range []string{j.Title, j.Summary, j.Text, j.HTML} {
  92. if value != "" {
  93. return truncate(sanitizer.StripTags(value))
  94. }
  95. }
  96. return j.URL
  97. }
  98. func (j *jsonItem) GetContent() string {
  99. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  100. if value != "" {
  101. return value
  102. }
  103. }
  104. return ""
  105. }
  106. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  107. enclosures := make(model.EnclosureList, 0)
  108. for _, attachment := range j.Attachments {
  109. enclosures = append(enclosures, &model.Enclosure{
  110. URL: attachment.URL,
  111. MimeType: attachment.MimeType,
  112. Size: attachment.Size,
  113. })
  114. }
  115. return enclosures
  116. }
  117. func (j *jsonItem) Transform() *model.Entry {
  118. entry := new(model.Entry)
  119. entry.URL = j.URL
  120. entry.Date = j.GetDate()
  121. entry.Author = j.GetAuthor()
  122. entry.Hash = j.GetHash()
  123. entry.Content = processor.ItemContentProcessor(entry.URL, j.GetContent())
  124. entry.Title = strings.TrimSpace(j.GetTitle())
  125. entry.Enclosures = j.GetEnclosures()
  126. return entry
  127. }
  128. func getAuthor(author jsonAuthor) string {
  129. if author.Name != "" {
  130. return strings.TrimSpace(author.Name)
  131. }
  132. return ""
  133. }
  134. func truncate(str string) string {
  135. max := 100
  136. str = strings.TrimSpace(str)
  137. if len(str) > max {
  138. return str[:max] + "..."
  139. }
  140. return str
  141. }