iTranslated by AI
A Go Package for Fetching Feeds
I have been looking into various things to create a simple bot, inspired by "[Introduction to Creating Twitter Bots with Shell Scripts](https://zenn.dev/mattn/books/bb181f3f4731920f29a5 "シェルスクリプトで作る Twitter bot 作成入門 | Zenn")"[1]. While searching for a Go package with a simple structure to retrieve RSS/Atom feeds published by blogs and other sites, I found a perfect one.
What makes mmcdole/gofeed excellent is that it consumes feeds regardless of their type and maps them into a unified structure. For example, it looks like this:
package main
import (
"fmt"
"os"
"time"
"github.com/mmcdole/gofeed"
)
func main() {
feed, err := gofeed.NewParser().ParseURL("https://zenn.dev/spiegel/feed")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(feed.Title)
fmt.Println(feed.FeedType, feed.FeedVersion)
for _, item := range feed.Items {
if item == nil {
break
}
fmt.Println(item.Title)
fmt.Println("\t-2", item.Link)
fmt.Println("\t-2", item.PublishedParsed.Format(time.RFC3339))
}
}
The execution result of this code looks like this:
$ go run sample.go
Spiegel's feed
rss 2.0
Running a full suite of CI tasks for Go packages with GitHub Actions
-> https://zenn.dev/spiegel/articles/20200929-using-golangci-lint-action
-> 2020-09-29T10:27:34Z
errors.Is and errors.As are not (just) comparison functions
-> https://zenn.dev/spiegel/articles/20200926-error-handling-with-golang
-> 2020-09-26T06:54:49Z
...
It also supports the standard context package, so you can write it with cancellation events, for example, under concurrent processing (goroutine) like this:
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
feed, err := gofeed.NewParser().ParseURLWithContext("https://zenn.dev/spiegel/feed", ctx)
if err != nil {
return
}
...
All right. This looks useful.
References
-
I've been using IFTTT to feed blog update information to Twitter, but even though I barely use it, they've been [persistently telling me to upgrade and pay](https://forest.watch.impress.co.jp/docs/news/1278901.html "無償版“IFTTT”で利用可能なアプレットは3つまでに ~超過分は10月8日にアーカイブ - 窓の杜"). They send emails almost every day. I'm not interested in spammy services. ↩︎
Discussion