feat: Much Faster edition

This commit is contained in:
decentral1se 2023-05-21 11:02:59 +02:00
parent 0a2753dbf0
commit b951bfe9fd
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
3 changed files with 39 additions and 73 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
akmd
*.md
*.txt
akl

View File

@ -1,9 +1,9 @@
# akl
All links posted within the last month on the Anarchokrant RSS feed.
All links posted since last month on the Anarchokrant RSS feed.
```
go run akl.go | xclip
go run akl.go
```
Handy for quickly gathering everything and curating from there.

104
akl.go
View File

@ -4,15 +4,11 @@ import (
"context"
"fmt"
"log"
"net/url"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/mmcdole/gofeed"
)
const layoutUS = "January 2, 2006"
var feeds = []string{
"http://anarchief.org/w/index.php?title=Special:NewPages&feed=rss",
"http://www.christianarchy.nl/feeds/posts/default?alt=rss",
@ -65,17 +61,7 @@ var feeds = []string{
"https://zwartetulp.noblogs.org/rss",
}
func htmlToMarkdown(content string) (string, error) {
converter := md.NewConverter("", true, nil)
markdown, err := converter.ConvertString(content)
if err != nil {
return markdown, fmt.Errorf("htmlToMarkdown: unable to convert html to markdown: %w", err)
}
return markdown, nil
}
// parseRSSFeed parses a RSS feed.
func parseRSSFeed(url string) (gofeed.Feed, error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
@ -89,77 +75,55 @@ func parseRSSFeed(url string) (gofeed.Feed, error) {
return *feed, nil
}
// item represents a single RSS feed item.
type item struct {
authors []string
content string
description string
link string
host string
published *time.Time
title string
link string
published *time.Time
title string
}
func (i item) Print() {
fmt.Println(fmt.Sprintf("%s\n%s", i.title, i.link))
}
func linksFromFeed(feed string, ch chan<- []item) {
var items []item
type items []item
func (i items) LastMonth() items {
var filtered []item
parsed, err := parseRSSFeed(feed)
if err != nil {
log.Fatal(err)
}
now := time.Now()
lastMonth := now.AddDate(0, -1, 0)
for _, item := range i {
if item.published.After(lastMonth) {
filtered = append(filtered, item)
for _, i := range parsed.Items {
if i.PublishedParsed.Before(lastMonth) {
continue
}
items = append(items, item{
link: i.Link,
published: i.PublishedParsed,
title: i.Title,
})
}
return filtered
ch <- items
}
// show outputs an item.
func (i item) show() {
fmt.Printf("%s\n%s\n\n", i.title, i.link)
}
func main() {
var allItems items
ch := make(chan []item, len(feeds))
for _, f := range feeds {
parsed, err := parseRSSFeed(f)
if err != nil {
log.Fatal(err)
}
for _, i := range parsed.Items {
markd, err := htmlToMarkdown(i.Content)
if err != nil {
log.Fatal(err)
}
var authors []string
for _, author := range i.Authors {
authors = append(authors, author.Name)
}
parsedLink, err := url.Parse(i.Link)
if err != nil {
log.Fatal(err)
}
newItem := item{
authors: authors,
content: markd,
description: i.Description,
link: i.Link,
host: parsedLink.Host,
published: i.PublishedParsed,
title: i.Title,
}
allItems = append(allItems, newItem)
}
for _, feed := range feeds {
go linksFromFeed(feed, ch)
}
for _, i := range allItems.LastMonth() {
i.Print()
for range feeds {
items := <-ch
for _, item := range items {
item.show()
}
}
}