-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
149 lines (123 loc) · 3.42 KB
/
item.go
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
// /home/krylon/go/src/ticker/feed/item.go
// -*- mode: go; coding: utf-8; -*-
// Created on 04. 02. 2021 by Benjamin Walkenhorst
// (c) 2021 Benjamin Walkenhorst
// Time-stamp: <2022-10-15 18:32:30 krylon>
package feed
import (
"errors"
"fmt"
"io/fs"
"math"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/blicero/ticker/common"
"github.com/blicero/ticker/tag"
"github.com/jaytaylor/html2text"
)
var whitespace *regexp.Regexp = regexp.MustCompile(`[\s\t\n\r]+`)
// Item represents a single news item from an RSS Feed.
type Item struct {
ID int64
FeedID int64
URL string
Title string
Description string
Timestamp time.Time
Read bool
Rating float64
ManuallyRated bool
Tags []tag.Tag
tagMap map[string]bool
}
func (i *Item) String() string {
return fmt.Sprintf("Item{ ID: %d, URL: %q, Title: %q, Timestamp: %q, Rating: %d }",
i.ID,
i.URL,
i.Title,
i.Timestamp.Format(common.TimestampFormat),
int(i.Rating*100))
} // func (i *Item) String() string
// IsRated returns true if the Item has a Rating.
func (i *Item) IsRated() bool {
return i.ManuallyRated
} // func (i *Item) IsRated() bool
// IsBoring returns true if the Item has been rated or classified as boring.
func (i *Item) IsBoring() bool {
return i.Rating <= 0
} // func (i *Item) IsBoring() bool
// RatingString returns the Item's rating as a string.
func (i *Item) RatingString() string {
if math.IsNaN(i.Rating) {
return ""
}
return fmt.Sprintf("%.2f", i.Rating)
} // func (i *Item) RatingString() string
// Plaintext returns the complete text of the Item, cleansed of any HTML.
func (i *Item) Plaintext() string {
var tmp = make([]string, 2)
var err error
if tmp[0], err = html2text.FromString(i.Title); err != nil {
tmp[0] = i.Title
}
if tmp[1], err = html2text.FromString(i.Description); err != nil {
tmp[1] = i.Description
}
if tmp[1] == "Comments" { // Hacker News
tmp[1] = ""
}
tmp[0] = whitespace.ReplaceAllString(tmp[0], " ")
tmp[1] = whitespace.ReplaceAllString(tmp[1], " ")
return strings.Join(tmp, " ")
} // func (i *Item) Plaintext() string
// HasTag returns true if the Tag with the given ID is attached to the
// receiver Item.
func (i *Item) HasTag(tagID int64) bool {
for _, t := range i.Tags {
if t.ID == tagID {
return true
}
}
return false
} // func (i *Item) HasTag(tagID int64) bool
// HasTagNamed returns true if the receiver carries the Tag with the given name.
func (i *Item) HasTagNamed(name string) bool {
if len(i.Tags) == 0 {
return false
} else if len(i.tagMap) != len(i.Tags) {
i.tagMap = make(map[string]bool, len(i.Tags))
for _, t := range i.Tags {
i.tagMap[t.Name] = true
}
}
return i.tagMap[name]
} // func (i *Item) HasTagNamed(name string) bool
// IsDownloaded returns true if the Item's linked URL has been downloaded
// to the local archive.
func (i *Item) IsDownloaded() (result bool) {
var (
err error
info os.FileInfo
pageFolder = filepath.Join(
common.ArchiveDir,
strconv.FormatInt(i.ID, 10))
)
defer func() {
if x := recover(); x != nil {
result = false
}
}()
if info, err = os.Stat(pageFolder); err != nil && !errors.Is(err, fs.ErrNotExist) {
fmt.Printf("XXX Error checking if page %d (%q) has been downloaded to %s: %s\n",
i.ID,
i.Title,
pageFolder,
err.Error())
return false
}
return info.IsDir()
} // func (i *Item) IsDownloaded() bool