This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofetchmyfeeds.go
188 lines (174 loc) · 5.03 KB
/
gofetchmyfeeds.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
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
package main
import (
"flag"
"fmt"
"os"
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
"github.com/SlyMarbo/rss"
"github.com/sgoertzen/html2text"
"github.com/syndtr/goleveldb/leveldb"
"gopkg.in/gomail.v2"
)
var (
err error
logf *os.File // log file
conf config // TOML config
mailsender gomail.SendCloser // Mail sender
db *leveldb.DB // database for seen items
feed *rss.Feed // RSS feed
)
// Default configuration parameters.
const (
dMailHost = "localhost"
dMailPort = 25
dMailRecipient = "root"
)
// TOML configuration structures.
type mailConfig struct {
Host string `toml:"host,omitempty"`
Port int `toml:"port,omitempty"`
User string `toml:"user,omitempty"`
Password string `toml:"password,omitempty"`
Recipient string `toml:"recipient,omitempty"`
}
type rssConfig struct {
Feeds [][]string `toml:"feeds"`
}
type config struct {
MailConfig mailConfig `toml:"mail"`
RssConfig rssConfig `toml:"rss"`
}
// Mail body builder.
func buildBody(i *rss.Item) string {
var t string
b := `
link: %s
%s
`
// HTML to text conversion
if t, err = html2text.Textify(i.Summary); err != nil {
log.Error("can not convert body to html")
t = i.Summary
}
return fmt.Sprintf(b, i.Link, t)
}
// Mail sender.
func sendMail(i *rss.Item, ftag string) error {
m := gomail.NewMessage()
m.SetHeader("From", "gofetchmyfeeds@nowhere.com")
m.SetAddressHeader("To", conf.MailConfig.Recipient, conf.MailConfig.Recipient)
m.SetHeader("Subject", ftag+" "+i.Title)
m.SetBody("text/plain", buildBody(i))
if err = gomail.Send(mailsender, m); err != nil {
return err
}
return nil
}
// Store the item to the database.
func markSeen(i *rss.Item) error {
// We use the item Link as a key.
// No need for a value.
return db.Put([]byte(i.Link), []byte(""), nil)
}
func main() {
// Getting the program parameters.
debug := flag.Bool("debug", false, "debug (verbose log), default is error")
logfile := flag.String("logfile", "", "log to the given file")
flag.Parse()
// Logging to file if logfile parameter specified.
if *logfile != "" {
if logf, err = os.OpenFile(*logfile, os.O_WRONLY|os.O_CREATE, 0755); err != nil {
log.Panic(err)
} else {
log.SetOutput(logf)
}
}
// Setting the log level.
if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Info("Opening the configuration file.")
// Decoding the configuration.
var md toml.MetaData
if md, err = toml.DecodeFile("./configuration.toml", &conf); err != nil {
log.Fatal("error opening the configuration file:", err)
}
// Setting default parameters if not defined in the toml file.
if !md.IsDefined("mail", "host") {
conf.MailConfig.Host = dMailHost
}
if !md.IsDefined("mail", "port") {
conf.MailConfig.Port = dMailPort
}
if !md.IsDefined("mail", "recipient") {
conf.MailConfig.Recipient = dMailRecipient
}
log.WithFields(log.Fields{
"c.MailConfig.Host": conf.MailConfig.Host,
"c.MailConfig.Port": conf.MailConfig.Port,
"c.MailConfig.User": conf.MailConfig.User,
"c.MailConfig.Password": conf.MailConfig.Password,
"c.MailConfig.Recipient": conf.MailConfig.Recipient,
"c.RssConfig.Feeds": conf.RssConfig.Feeds,
}).Debug("main:configuration")
// Opening the DB file.
if db, err = leveldb.OpenFile("./seen.db", nil); err != nil {
log.Fatal("error opening the database file:", err)
}
defer func() {
err = db.Close()
if err != nil {
log.Error("error closing the database file:", err)
}
}()
// Connecting to the remote SMTP server.
dial := gomail.NewDialer(conf.MailConfig.Host, conf.MailConfig.Port, conf.MailConfig.User, conf.MailConfig.Password)
if mailsender, err = dial.Dial(); err != nil {
log.Fatal("can not open connection to mail server:", err)
}
defer func() {
err = mailsender.Close()
if err != nil {
log.Error("error closing the mailsender connection:", err)
}
}()
// Gathering the feeds from the configuration file.
for _, fconfig := range conf.RssConfig.Feeds {
// Fetching the feed.
furl := fconfig[0]
ftag := fconfig[1]
log.Info("Fetching " + furl)
if feed, err = rss.Fetch(furl); err != nil {
log.Errorf("error fetching %s: %v", furl, err)
continue
}
log.Debugf("fetched:%s", feed.Title)
// Parsing the feed items.
for _, item := range feed.Items {
log.Infof("- %s", item.Title)
log.Debugf("item:%s link:", item.Title, item.Link)
// Looking for the feed into the DB - the link is the key
if _, err = db.Get([]byte(item.Link), nil); err != nil {
switch err {
case leveldb.ErrNotFound:
log.Debugf("NOT already sent")
// Sending the item by mail.
if err = sendMail(item, ftag); err != nil {
log.Error("error sending mail:", err)
} else if err = markSeen(item); err != nil {
log.Error("error marking item as seen:", err)
}
// Storing the item into the DB.
default:
log.Errorf("error getting key %s from the DB: %v", feed.Link, err)
continue
}
} else {
log.Debug("already sent")
}
}
}
}