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)
46 guids := make(map[string]struct{}, len(feed.Items))
48 for _, item := range feed.Items {
49 if _, exists := guids[item.GUID]; exists {
53 guids[item.GUID] = struct{}{}
61 for n, item := range feed.Items {
62 if n == int(*maxEntries) {
66 if item.PublishedParsed != nil {
67 when = item.PublishedParsed
68 } else if item.UpdatedParsed != nil {
69 when = item.UpdatedParsed
74 if len(item.Content) == 0 {
75 what = item.Description
79 what = strings.TrimPrefix(what, "<![CDATA[")
80 what = strings.TrimSuffix(what, "]]>")
83 h.Write([]byte(item.GUID))
85 h.Write([]byte(item.Title))
89 fn := hex.EncodeToString(h.Sum(nil)[:sha512.Size/2])
91 for _, d := range []string{"cur", "new"} {
92 entries, err := os.ReadDir(path.Join(mdir, d))
96 for _, entry := range entries {
97 if strings.HasPrefix(entry.Name(), fn) {
106 fn = path.Join(mdir, "new", fn)
107 fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
111 fd.WriteString("From: \"" + feed.Title + "\" <feeder@go.stargrave.org>\n")
112 fd.WriteString("Date: " + when.UTC().Format(time.RFC1123Z) + "\n")
113 fd.WriteString("Subject: " + mime.BEncoding.Encode("UTF-8", item.Title) + "\n")
114 fd.WriteString("MIME-Version: 1.0\n")
115 fd.WriteString("Content-Type: text/html; charset=utf-8\n")
116 fd.WriteString("Content-Transfer-Encoding: base64\n")
117 for _, link := range item.Links {
118 fd.WriteString("X-URL: " + link + "\n")
120 for _, author := range item.Authors {
121 fd.WriteString("X-Author: " + author.Name + "\n")
123 for _, cat := range item.Categories {
124 fd.WriteString("X-Category: " + cat + "\n")
127 what = base64.StdEncoding.EncodeToString([]byte(what))
128 for i := 0; i < len(what); i += 72 {
133 fd.WriteString(what[i:b] + "\n")
136 if err = os.Chtimes(fn, *when, *when); err != nil {
142 if feed.PublishedParsed != nil {
143 when = feed.PublishedParsed
144 } else if feed.UpdatedParsed != nil {
145 when = feed.UpdatedParsed
148 for _, d := range []string{"cur", "new"} {
149 if err = os.Chtimes(path.Join(mdir, d), *when, *when); err != nil {
154 fmt.Println(feed.Title)