aboutsummaryrefslogtreecommitdiff
path: root/internal/reader/json/json.go
blob: 6786af745de0b05b9d1c2504113f22e40d4ffea0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package json // import "miniflux.app/v2/internal/reader/json"

import (
	"log/slog"
	"strings"
	"time"

	"miniflux.app/v2/internal/crypto"
	"miniflux.app/v2/internal/model"
	"miniflux.app/v2/internal/reader/date"
	"miniflux.app/v2/internal/reader/sanitizer"
	"miniflux.app/v2/internal/urllib"
)

type jsonFeed struct {
	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"`
}

type jsonAuthor struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

type jsonItem struct {
	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"`
	DatePublished string           `json:"date_published"`
	DateModified  string           `json:"date_modified"`
	Authors       []jsonAuthor     `json:"authors"`
	Author        jsonAuthor       `json:"author"`
	Attachments   []jsonAttachment `json:"attachments"`
	Tags          []string         `json:"tags"`
}

type jsonAttachment struct {
	URL      string `json:"url"`
	MimeType string `json:"mime_type"`
	Title    string `json:"title"`
	Size     int64  `json:"size_in_bytes"`
	Duration int    `json:"duration_in_seconds"`
}

func (j *jsonFeed) GetAuthor() string {
	if len(j.Authors) > 0 {
		return (getAuthor(j.Authors[0]))
	}
	return getAuthor(j.Author)
}

func (j *jsonFeed) Transform(baseURL string) *model.Feed {
	var err error

	feed := new(model.Feed)

	feed.FeedURL, err = urllib.AbsoluteURL(baseURL, j.FeedURL)
	if err != nil {
		feed.FeedURL = j.FeedURL
	}

	feed.SiteURL, err = urllib.AbsoluteURL(baseURL, j.SiteURL)
	if err != nil {
		feed.SiteURL = j.SiteURL
	}

	feed.IconURL = strings.TrimSpace(j.IconURL)

	if feed.IconURL == "" {
		feed.IconURL = strings.TrimSpace(j.FaviconURL)
	}

	feed.Title = strings.TrimSpace(j.Title)
	if feed.Title == "" {
		feed.Title = feed.SiteURL
	}

	for _, item := range j.Items {
		entry := item.Transform()
		entryURL, err := urllib.AbsoluteURL(feed.SiteURL, entry.URL)
		if err == nil {
			entry.URL = entryURL
		}

		if entry.Author == "" {
			entry.Author = j.GetAuthor()
		}

		feed.Entries = append(feed.Entries, entry)
	}

	return feed
}

func (j *jsonItem) GetDate() time.Time {
	for _, value := range []string{j.DatePublished, j.DateModified} {
		if value != "" {
			d, err := date.Parse(value)
			if err != nil {
				slog.Warn("Unable to parse date from JSON feed",
					slog.String("date", value),
					slog.String("url", j.URL),
					slog.Any("error", err),
				)
				return time.Now()
			}

			return d
		}
	}

	return time.Now()
}

func (j *jsonItem) GetAuthor() string {
	if len(j.Authors) > 0 {
		return getAuthor(j.Authors[0])
	}
	return getAuthor(j.Author)
}

func (j *jsonItem) GetHash() string {
	for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
		if value != "" {
			return crypto.Hash(value)
		}
	}

	return ""
}

func (j *jsonItem) GetTitle() string {
	if j.Title != "" {
		return j.Title
	}

	for _, value := range []string{j.Summary, j.Text, j.HTML} {
		if value != "" {
			return sanitizer.TruncateHTML(value, 100)
		}
	}

	return j.URL
}

func (j *jsonItem) GetContent() string {
	for _, value := range []string{j.HTML, j.Text, j.Summary} {
		if value != "" {
			return value
		}
	}

	return ""
}

func (j *jsonItem) GetEnclosures() model.EnclosureList {
	enclosures := make(model.EnclosureList, 0)

	for _, attachment := range j.Attachments {
		if attachment.URL == "" {
			continue
		}

		enclosures = append(enclosures, &model.Enclosure{
			URL:      attachment.URL,
			MimeType: attachment.MimeType,
			Size:     attachment.Size,
		})
	}

	return enclosures
}

func (j *jsonItem) Transform() *model.Entry {
	entry := model.NewEntry()
	entry.URL = j.URL
	entry.Date = j.GetDate()
	entry.Author = j.GetAuthor()
	entry.Hash = j.GetHash()
	entry.Content = j.GetContent()
	entry.Title = strings.TrimSpace(j.GetTitle())
	entry.Enclosures = j.GetEnclosures()
	if len(j.Tags) > 0 {
		entry.Tags = j.Tags
	}

	return entry
}

func getAuthor(author jsonAuthor) string {
	if author.Name != "" {
		return strings.TrimSpace(author.Name)
	}

	return ""
}