2 feeder -- newsfeeds aggregator
3 Copyright (C) 2022-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 "github.com/mmcdole/gofeed"
38 maxEntries := flag.Uint("max-entries", 0, "Max entries to process (0=unlimited)")
41 fp := gofeed.NewParser()
42 feed, err := fp.Parse(os.Stdin)
47 guids := make(map[string]struct{}, len(feed.Items))
49 for _, item := range feed.Items {
50 if _, exists := guids[item.GUID]; exists {
54 guids[item.GUID] = struct{}{}
58 feedTitle := feed.Title
59 if len(feedTitle) == 0 {
60 feedTitle, err = filepath.Abs(mdir)
64 feedTitle = path.Base(feedTitle)
71 latest := &time.Time{}
72 for n, item := range feed.Items {
73 if *maxEntries > 0 && n == int(*maxEntries) {
77 if item.PublishedParsed != nil {
78 when = item.PublishedParsed
79 } else if item.UpdatedParsed != nil {
80 when = item.UpdatedParsed
84 if latest.Before(*when) {
88 if len(item.Content) > len(item.Description) {
91 what = item.Description
93 what = strings.TrimPrefix(what, "<![CDATA[")
94 what = strings.TrimSuffix(what, "]]>")
97 h.Write([]byte(item.GUID))
99 h.Write([]byte(item.Title))
101 h.Write([]byte(what))
103 fn := hex.EncodeToString(h.Sum(nil)[:sha512.Size/2])
105 for _, d := range []string{"cur", "new"} {
106 entries, err := os.ReadDir(path.Join(mdir, d))
110 for _, entry := range entries {
111 if strings.HasPrefix(entry.Name(), fn) {
120 fn = path.Join(mdir, "new", fn)
121 fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
125 fd.WriteString("From: \"" + feedTitle + "\" <feeder@localhost>\n")
126 fd.WriteString("Date: " + when.UTC().Format(time.RFC1123Z) + "\n")
127 fd.WriteString("Subject: " + mime.BEncoding.Encode("UTF-8", item.Title) + "\n")
128 fd.WriteString("MIME-Version: 1.0\n")
129 fd.WriteString("Content-Type: text/html; charset=utf-8\n")
130 fd.WriteString("Content-Transfer-Encoding: base64\n")
131 for _, author := range item.Authors {
132 if len(author.Name) > 0 {
133 fd.WriteString("X-Author: " + author.Name + "\n")
136 for _, link := range item.Links {
137 fd.WriteString("X-URL: " + link + "\n")
139 for _, enc := range item.Enclosures {
140 fd.WriteString("X-Enclosure: " + enc.URL + "\n")
142 if len(item.Categories) > 0 {
143 fd.WriteString("X-Categories: " + strings.Join(item.Categories, ", ") + "\n")
146 what = base64.StdEncoding.EncodeToString([]byte(what))
147 for i := 0; i < len(what); i += 72 {
152 fd.WriteString(what[i:b] + "\n")
155 if err = os.Chtimes(fn, *when, *when); err != nil {
160 for _, d := range []string{"cur", "new"} {
161 if err = os.Chtimes(path.Join(mdir, d), *latest, *latest); err != nil {
165 fmt.Println(feedTitle)