2 go.stargrave.org/feeder -- newsfeeds aggregator
3 Copyright (C) 2022 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/>.
33 "github.com/mmcdole/gofeed"
37 maxEntries := flag.Uint("max-entries", 100, "Max entries to process")
40 fp := gofeed.NewParser()
41 feed, err := fp.Parse(os.Stdin)
48 max := int(*maxEntries) - 1
49 for n, item := range feed.Items {
55 h.Write([]byte(item.Title))
57 if item.UpdatedParsed != nil {
58 when = item.UpdatedParsed
59 h.Write([]byte(when.String()))
60 } else if item.PublishedParsed != nil {
61 when = item.PublishedParsed
62 h.Write([]byte(when.String()))
66 fn := hex.EncodeToString(h.Sum(nil)[:sha512.Size/2])
68 if len(item.Content) == 0 {
69 what = item.Description
74 for _, d := range []string{"cur", "new"} {
75 entries, err := os.ReadDir(path.Join(mdir, d))
79 for _, entry := range entries {
80 if strings.HasPrefix(entry.Name(), fn) {
89 fn = path.Join(mdir, "new", fn)
90 fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
94 fd.WriteString("From: \"" + feed.Title + "\" <feeder@go.stargrave.org>\n")
95 fd.WriteString("Date: " + when.Format(time.RFC1123Z) + "\n")
96 fd.WriteString("Subject: " + mime.BEncoding.Encode("UTF-8", item.Title) + "\n")
97 fd.WriteString("MIME-Version: 1.0\n")
98 fd.WriteString("Content-Type: text/html; charset=utf-8\n")
99 fd.WriteString("Content-Transfer-Encoding: base64\n")
100 for _, link := range item.Links {
101 fd.WriteString("X-URL: " + link + "\n")
103 for _, author := range item.Authors {
104 fd.WriteString("X-Author: " + author.Name + "\n")
106 for _, cat := range item.Categories {
107 fd.WriteString("X-Category: " + cat + "\n")
110 what = base64.StdEncoding.EncodeToString([]byte(what))
111 for i := 0; i < len(what); i += 72 {
116 fd.WriteString(what[i:b] + "\n")
119 if err = os.Chtimes(fn, *when, *when); err != nil {
125 if feed.UpdatedParsed != nil {
126 when = feed.UpdatedParsed
127 } else if feed.PublishedParsed != nil {
128 when = feed.PublishedParsed
131 for _, d := range []string{"cur", "new"} {
132 if err = os.Chtimes(path.Join(mdir, d), *when, *when); err != nil {
137 fmt.Println(feed.Title)