2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package json // import "miniflux.app/v2/internal/reader/json"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2017-11-21 03:57:54 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/logger"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/reader/date"
|
|
|
|
"miniflux.app/v2/internal/reader/sanitizer"
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
type jsonFeed struct {
|
2023-06-05 00:01:59 +02:00
|
|
|
Version string `json:"version"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
SiteURL string `json:"home_page_url"`
|
|
|
|
IconURL string `json:"icon"`
|
|
|
|
FaviconURL string `json:"favicon"`
|
|
|
|
FeedURL string `json:"feed_url"`
|
|
|
|
Authors []jsonAuthor `json:"authors"`
|
|
|
|
Author jsonAuthor `json:"author"`
|
|
|
|
Items []jsonItem `json:"items"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
type jsonAuthor struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
type jsonItem struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Summary string `json:"summary"`
|
|
|
|
Text string `json:"content_text"`
|
2017-11-21 03:57:54 +01:00
|
|
|
HTML string `json:"content_html"`
|
2017-11-20 06:10:04 +01:00
|
|
|
DatePublished string `json:"date_published"`
|
|
|
|
DateModified string `json:"date_modified"`
|
2021-07-21 17:14:25 +02:00
|
|
|
Authors []jsonAuthor `json:"authors"`
|
2017-11-21 03:57:54 +01:00
|
|
|
Author jsonAuthor `json:"author"`
|
|
|
|
Attachments []jsonAttachment `json:"attachments"`
|
2023-02-25 05:52:45 +01:00
|
|
|
Tags []string `json:"tags"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
type jsonAttachment struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
URL string `json:"url"`
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
Title string `json:"title"`
|
2018-03-15 04:09:06 +01:00
|
|
|
Size int64 `json:"size_in_bytes"`
|
2017-11-20 06:10:04 +01:00
|
|
|
Duration int `json:"duration_in_seconds"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonFeed) GetAuthor() string {
|
2021-07-21 17:14:25 +02:00
|
|
|
if len(j.Authors) > 0 {
|
|
|
|
return (getAuthor(j.Authors[0]))
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return getAuthor(j.Author)
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
func (j *jsonFeed) Transform(baseURL string) *model.Feed {
|
|
|
|
var err error
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
feed := new(model.Feed)
|
|
|
|
|
2023-08-14 04:09:01 +02:00
|
|
|
feed.FeedURL, err = urllib.AbsoluteURL(baseURL, j.FeedURL)
|
2020-12-03 05:47:11 +01:00
|
|
|
if err != nil {
|
|
|
|
feed.FeedURL = j.FeedURL
|
|
|
|
}
|
|
|
|
|
2023-08-14 04:09:01 +02:00
|
|
|
feed.SiteURL, err = urllib.AbsoluteURL(baseURL, j.SiteURL)
|
2020-12-03 05:47:11 +01:00
|
|
|
if err != nil {
|
|
|
|
feed.SiteURL = j.SiteURL
|
|
|
|
}
|
|
|
|
|
2023-06-05 00:01:59 +02:00
|
|
|
feed.IconURL = strings.TrimSpace(j.IconURL)
|
|
|
|
|
|
|
|
if feed.IconURL == "" {
|
|
|
|
feed.IconURL = strings.TrimSpace(j.FaviconURL)
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
feed.Title = strings.TrimSpace(j.Title)
|
2017-11-20 06:10:04 +01:00
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range j.Items {
|
|
|
|
entry := item.Transform()
|
2023-08-14 04:09:01 +02:00
|
|
|
entryURL, err := urllib.AbsoluteURL(feed.SiteURL, entry.URL)
|
2017-12-14 05:16:15 +01:00
|
|
|
if err == nil {
|
|
|
|
entry.URL = entryURL
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
if entry.Author == "" {
|
|
|
|
entry.Author = j.GetAuthor()
|
|
|
|
}
|
|
|
|
|
|
|
|
feed.Entries = append(feed.Entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetDate() time.Time {
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, value := range []string{j.DatePublished, j.DateModified} {
|
|
|
|
if value != "" {
|
|
|
|
d, err := date.Parse(value)
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("json: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetAuthor() string {
|
2021-07-21 17:14:25 +02:00
|
|
|
if len(j.Authors) > 0 {
|
|
|
|
return getAuthor(j.Authors[0])
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return getAuthor(j.Author)
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetHash() string {
|
|
|
|
for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
|
2017-11-20 06:10:04 +01:00
|
|
|
if value != "" {
|
2018-01-03 04:15:08 +01:00
|
|
|
return crypto.Hash(value)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetTitle() string {
|
2022-03-05 01:49:44 +01:00
|
|
|
if j.Title != "" {
|
|
|
|
return j.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range []string{j.Summary, j.Text, j.HTML} {
|
2017-11-20 06:10:04 +01:00
|
|
|
if value != "" {
|
2022-03-05 01:49:44 +01:00
|
|
|
return sanitizer.TruncateHTML(value, 100)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return j.URL
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetContent() string {
|
|
|
|
for _, value := range []string{j.HTML, j.Text, j.Summary} {
|
2017-11-20 06:10:04 +01:00
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) GetEnclosures() model.EnclosureList {
|
2017-11-20 06:10:04 +01:00
|
|
|
enclosures := make(model.EnclosureList, 0)
|
|
|
|
|
|
|
|
for _, attachment := range j.Attachments {
|
2020-01-31 06:08:11 +01:00
|
|
|
if attachment.URL == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: attachment.URL,
|
|
|
|
MimeType: attachment.MimeType,
|
|
|
|
Size: attachment.Size,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return enclosures
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func (j *jsonItem) Transform() *model.Entry {
|
2023-09-09 07:45:17 +02:00
|
|
|
entry := model.NewEntry()
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.URL = j.URL
|
|
|
|
entry.Date = j.GetDate()
|
2017-11-22 23:52:31 +01:00
|
|
|
entry.Author = j.GetAuthor()
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Hash = j.GetHash()
|
2017-12-12 07:16:32 +01:00
|
|
|
entry.Content = j.GetContent()
|
2017-11-22 23:52:31 +01:00
|
|
|
entry.Title = strings.TrimSpace(j.GetTitle())
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Enclosures = j.GetEnclosures()
|
2023-09-09 07:45:17 +02:00
|
|
|
if len(j.Tags) > 0 {
|
|
|
|
entry.Tags = j.Tags
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:57:54 +01:00
|
|
|
func getAuthor(author jsonAuthor) string {
|
2017-11-20 06:10:04 +01:00
|
|
|
if author.Name != "" {
|
2017-11-22 23:52:31 +01:00
|
|
|
return strings.TrimSpace(author.Name)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|