json.go 3.6 KB

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