1
0
Fork 0
miniflux/reader/json/json.go

182 lines
3.8 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package json // import "miniflux.app/reader/json"
2017-11-20 00:10:04 -05:00
import (
"strings"
"time"
2018-08-25 00:51:50 -04:00
"miniflux.app/crypto"
"miniflux.app/logger"
"miniflux.app/model"
"miniflux.app/reader/date"
"miniflux.app/reader/sanitizer"
"miniflux.app/url"
2017-11-20 00:10:04 -05:00
)
type jsonFeed struct {
2017-11-20 00:10:04 -05:00
Version string `json:"version"`
Title string `json:"title"`
SiteURL string `json:"home_page_url"`
FeedURL string `json:"feed_url"`
Author jsonAuthor `json:"author"`
Items []jsonItem `json:"items"`
2017-11-20 00:10:04 -05:00
}
type jsonAuthor struct {
2017-11-20 00:10:04 -05:00
Name string `json:"name"`
URL string `json:"url"`
}
type jsonItem struct {
2017-11-20 00:10:04 -05:00
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Summary string `json:"summary"`
Text string `json:"content_text"`
HTML string `json:"content_html"`
2017-11-20 00:10:04 -05:00
DatePublished string `json:"date_published"`
DateModified string `json:"date_modified"`
Author jsonAuthor `json:"author"`
Attachments []jsonAttachment `json:"attachments"`
2017-11-20 00:10:04 -05:00
}
type jsonAttachment struct {
2017-11-20 00:10:04 -05:00
URL string `json:"url"`
MimeType string `json:"mime_type"`
Title string `json:"title"`
2018-03-14 23:09:06 -04:00
Size int64 `json:"size_in_bytes"`
2017-11-20 00:10:04 -05:00
Duration int `json:"duration_in_seconds"`
}
func (j *jsonFeed) GetAuthor() string {
2017-11-20 00:10:04 -05:00
return getAuthor(j.Author)
}
func (j *jsonFeed) Transform() *model.Feed {
2017-11-20 00:10:04 -05:00
feed := new(model.Feed)
feed.FeedURL = j.FeedURL
feed.SiteURL = j.SiteURL
2017-11-22 17:52:31 -05:00
feed.Title = strings.TrimSpace(j.Title)
2017-11-20 00:10:04 -05:00
if feed.Title == "" {
feed.Title = feed.SiteURL
}
for _, item := range j.Items {
entry := item.Transform()
2017-12-13 23:16:15 -05:00
entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
if err == nil {
entry.URL = entryURL
}
2017-11-20 00:10:04 -05:00
if entry.Author == "" {
entry.Author = j.GetAuthor()
}
feed.Entries = append(feed.Entries, entry)
}
return feed
}
func (j *jsonItem) GetDate() time.Time {
2017-11-20 00:10:04 -05:00
for _, value := range []string{j.DatePublished, j.DateModified} {
if value != "" {
d, err := date.Parse(value)
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("json: %v", err)
2017-11-20 00:10:04 -05:00
return time.Now()
}
return d
}
}
return time.Now()
}
func (j *jsonItem) GetAuthor() string {
2017-11-20 00:10:04 -05:00
return getAuthor(j.Author)
}
func (j *jsonItem) GetHash() string {
for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
2017-11-20 00:10:04 -05:00
if value != "" {
2018-01-02 22:15:08 -05:00
return crypto.Hash(value)
2017-11-20 00:10:04 -05:00
}
}
return ""
}
func (j *jsonItem) GetTitle() string {
for _, value := range []string{j.Title, j.Summary, j.Text, j.HTML} {
2017-11-20 00:10:04 -05:00
if value != "" {
2017-11-22 17:52:31 -05:00
return truncate(sanitizer.StripTags(value))
2017-11-20 00:10:04 -05:00
}
}
return j.URL
}
func (j *jsonItem) GetContent() string {
for _, value := range []string{j.HTML, j.Text, j.Summary} {
2017-11-20 00:10:04 -05:00
if value != "" {
return value
}
}
return ""
}
func (j *jsonItem) GetEnclosures() model.EnclosureList {
2017-11-20 00:10:04 -05:00
enclosures := make(model.EnclosureList, 0)
for _, attachment := range j.Attachments {
2020-01-31 00:08:11 -05:00
if attachment.URL == "" {
continue
}
2017-11-20 00:10:04 -05:00
enclosures = append(enclosures, &model.Enclosure{
URL: attachment.URL,
MimeType: attachment.MimeType,
Size: attachment.Size,
})
}
return enclosures
}
func (j *jsonItem) Transform() *model.Entry {
2017-11-20 00:10:04 -05:00
entry := new(model.Entry)
entry.URL = j.URL
entry.Date = j.GetDate()
2017-11-22 17:52:31 -05:00
entry.Author = j.GetAuthor()
2017-11-20 00:10:04 -05:00
entry.Hash = j.GetHash()
entry.Content = j.GetContent()
2017-11-22 17:52:31 -05:00
entry.Title = strings.TrimSpace(j.GetTitle())
2017-11-20 00:10:04 -05:00
entry.Enclosures = j.GetEnclosures()
return entry
}
func getAuthor(author jsonAuthor) string {
2017-11-20 00:10:04 -05:00
if author.Name != "" {
2017-11-22 17:52:31 -05:00
return strings.TrimSpace(author.Name)
2017-11-20 00:10:04 -05:00
}
return ""
}
func truncate(str string) string {
max := 100
2017-11-22 17:52:31 -05:00
str = strings.TrimSpace(str)
2017-11-20 00:10:04 -05:00
if len(str) > max {
return str[:max] + "..."
}
return str
}