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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
package item
import (
"encoding/base64"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
goose "github.com/advancedlogic/GoOse"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"adammathes.com/neko/config"
"adammathes.com/neko/internal/vlog"
"adammathes.com/neko/models"
)
type ContentExtractor interface {
Extract(url string) (*goose.Article, error)
}
type GooseExtractor struct{}
func (ge GooseExtractor) Extract(url string) (*goose.Article, error) {
g := goose.New()
return g.ExtractFromURL(url)
}
var Extractor ContentExtractor = GooseExtractor{}
type Item struct {
Id int64 `json:"_id,string,omitempty"`
Title string `json:"title"`
Url string `json:"url"`
Description string `json:"description"`
PublishDate string `json:"publish_date"`
FeedId int64
FeedTitle string `json:"feed_title"`
FeedUrl string `json:"feed_url"`
FeedCategory string `json:"feed_category"`
ReadState bool `json:"read"`
Starred bool `json:"starred"`
FullContent string `json:"full_content"`
HeaderImage string `json:"header_image"`
}
func (i *Item) Print() {
fmt.Printf("id: %d\n", i.Id)
fmt.Printf("title: %s\n", i.Title)
fmt.Printf("ReadState: %t\n", i.ReadState)
}
func (i *Item) Create() error {
res, err := models.DB.Exec(`INSERT INTO
item(title, url, description, publish_date, feed_id, read_state, starred)
VALUES(?, ?, ?, ?, ?, ?, ?)`, i.Title, i.Url, i.Description, i.PublishDate, i.FeedId, i.ReadState, i.Starred)
if err != nil {
vlog.Printf("Error on item.Create\n%v\n%v\n", i.Url, err)
return err
}
id, _ := res.LastInsertId()
i.Id = id
return nil
}
func (i *Item) Save() {
_, err := models.DB.Exec(`UPDATE item
SET read_state=?, starred=?
WHERE id=?`, i.ReadState, i.Starred, i.Id)
if err != nil {
vlog.Printf("Error on item.Save\n%v\n%v\n", i, err)
}
}
func (i *Item) FullSave() {
_, err := models.DB.Exec(`UPDATE item
SET title=?, url=?, description=?, feed_id=?
WHERE id=?`, i.Title, i.Url, i.Description, i.FeedId, i.Id)
if err != nil {
vlog.Printf("Error on item.fullSave\n%v\n%v\n", i, err)
}
}
func filterPolicy() *bluemonday.Policy {
p := bluemonday.NewPolicy()
p.AllowElements("ul", "ol", "li", "blockquote", "a", "img", "p", "h1", "h2", "h3", "h4", "b", "i", "em", "strong", "pre", "code")
p.AllowAttrs("href").OnElements("a")
p.AllowAttrs("src", "alt").OnElements("img")
return p
}
func ItemById(id int64) *Item {
items, err := Filter(0, nil, "", false, false, id, "")
if err != nil || len(items) == 0 {
return nil
}
return items[0]
}
func (i *Item) GetFullContent() {
fmt.Printf("fetching from %s\n", i.Url)
article, err := Extractor.Extract(i.Url)
if err != nil {
vlog.Println(err)
return
}
// i.FullContent and i.HeaderImage will be updated during extraction
if article.CleanedText != "" {
i.FullContent = string(blackfriday.Run([]byte(article.CleanedText)))
}
if article.TopNode != nil {
ht, err := article.TopNode.Html()
if err == nil {
p := filterPolicy()
i.FullContent = p.Sanitize(ht)
}
}
i.HeaderImage = article.TopImage
_, err = models.DB.Exec(`UPDATE item
SET full_content=?, header_image=?
WHERE id=?`, i.FullContent, i.HeaderImage, i.Id)
if err != nil {
vlog.Println(err)
}
}
func Filter(max_id int64, feed_ids []int64, category string, unread_only bool, starred_only bool, item_id int64, search_query string) ([]*Item, error) {
var args []interface{}
tables := " feed,item"
if search_query != "" {
tables = tables + ",fts_item"
}
query := `SELECT item.id, item.feed_id, item.title,
item.url, item.description,
item.read_state, item.starred, item.publish_date,
item.full_content, item.header_image,
feed.url, feed.title, feed.category
FROM `
query = query + tables + ` WHERE item.feed_id=feed.id AND item.id!=0 `
if max_id != 0 {
query = query + "AND item.id < ? "
args = append(args, max_id)
}
if len(feed_ids) > 0 {
placeholders := make([]string, len(feed_ids))
for i := range feed_ids {
placeholders[i] = "?"
args = append(args, feed_ids[i])
}
query = query + " AND feed.id IN (" + strings.Join(placeholders, ",") + ") "
}
if category != "" {
query = query + " AND feed.category=? "
args = append(args, category)
}
if unread_only {
query = query + " AND item.read_state=0 "
}
if item_id != 0 {
query = query + " AND item.id=? "
args = append(args, item_id)
}
if search_query != "" {
query = query + " AND fts_item match ? AND fts_item.rowid=item.id "
args = append(args, search_query)
}
// this is kind of dumb, but to keep the logic the same
// we kludge it this way for a "by id" select
if starred_only {
query = query + " AND item.starred=1 "
}
query = query + "ORDER BY item.id DESC LIMIT 15"
// vlog.Println(query)
// vlog.Println(args...)
rows, err := models.DB.Query(query, args...)
if err != nil {
vlog.Println(err)
return nil, err
}
defer func() { _ = rows.Close() }()
p := filterPolicy()
items := make([]*Item, 0)
for rows.Next() {
i := new(Item)
var feed_id int64
err := rows.Scan(&i.Id, &feed_id, &i.Title, &i.Url, &i.Description, &i.ReadState, &i.Starred, &i.PublishDate, &i.FullContent, &i.HeaderImage, &i.FeedUrl, &i.FeedTitle, &i.FeedCategory)
if err != nil {
vlog.Println(err)
return nil, err
}
// sanitize all fields from external input
// should do this at ingest time, probably, for efficiency
// but still may need to adjust rules
i.Title = p.Sanitize(i.Title)
i.Description = p.Sanitize(i.Description)
if config.Config.ProxyImages {
i.Description = rewriteImages(i.Description)
}
i.Url = p.Sanitize(i.Url)
i.FeedTitle = p.Sanitize(i.FeedTitle)
i.FeedUrl = p.Sanitize(i.FeedUrl)
i.FullContent = p.Sanitize(i.FullContent)
i.HeaderImage = p.Sanitize(i.HeaderImage)
i.CleanHeaderImage()
items = append(items, i)
}
if err = rows.Err(); err != nil {
return nil, err
}
return items, nil
}
// Purge deletes items older than the specified number of days.
// By default it only deletes read items.
// If allItems is true, it also deletes unread items.
// Starred items are NEVER deleted.
func Purge(days int, allItems bool) (int64, error) {
query := `DELETE FROM item WHERE datetime(publish_date) < datetime('now', ?) AND starred == 0`
if !allItems {
query = query + ` AND read_state == 1`
}
vlog.Printf("Purge query: %s with param %s\n", query, fmt.Sprintf("-%d days", days))
res, err := models.DB.Exec(query, fmt.Sprintf("-%d days", days))
if err != nil {
return 0, err
}
affected, _ := res.RowsAffected()
vlog.Printf("Purge affected rows: %d\n", affected)
// Cleanup FTS table - SQLite FTS4 doesn't automatically cleanup content table rows
// if we are using "content=item" (which we are).
// Actually we have triggers, so it should be fine.
// But VACUUM is good to reclaim space.
// _, _ = models.DB.Exec("VACUUM")
return affected, nil
}
func (i *Item) CleanHeaderImage() {
// TODO: blacklist of bad imgs
if i.HeaderImage == "https://s0.wp.com/i/blank.jpg" {
i.HeaderImage = ""
}
}
// rewrite images to use local proxy
func rewriteImages(s string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(s))
if err != nil {
vlog.Println(err)
return s
}
doc.Find("img").Each(func(i int, img *goquery.Selection) {
if src, ok := img.Attr("src"); ok {
img.SetAttr("src", proxyURL(src))
}
if srcset, ok := img.Attr("srcset"); ok {
// srcset is a comma-separated list of "url descriptor"
parts := strings.Split(srcset, ",")
for i, part := range parts {
part = strings.TrimSpace(part)
subparts := strings.Fields(part)
if len(subparts) > 0 {
subparts[0] = proxyURL(subparts[0])
}
parts[i] = strings.Join(subparts, " ")
}
img.SetAttr("srcset", strings.Join(parts, ", "))
}
})
output, _ := doc.Html()
return output
}
func proxyURL(url string) string {
return "/image/" + base64.URLEncoding.EncodeToString([]byte(url))
}
|