rf

package module
v0.3.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 29 Imported by: 0

README

rss-feeds-go

A go utility package for handling RSS/Atom feeds.

Features

  • Fetch feeds from URLs
    • RSS feeds (0.90 to 2.0)
    • Atom feeds (0.3, 1.0)
    • JSON feeds (1.0, 1.1)
    • Manually
    • Periodically
  • Cache fetched feeds locally
    • In memory
    • In SQLite3 file
  • Summarize contents of fetched feed items with Google Gemini API
    • Save summarized contents locally
      • In memory
      • In SQLite3 file
    • Transfer summarized contents to somewhere else
  • Convert cached feeds as RSS XML

Installation

$ go get github.com/meinside/rss-feeds-go

Usage

package main

import (
  "context"
  "log"
  "time"

  rf "github.com/meinside/rss-feeds-go"
)

func main() {
  client, err := rf.NewClientWithDB(
    []string{"your-google-ai-api-key"}, // google ai api keys (for rotation)
    []string{ // feeds' urls
      "https://hnrss.org/newest?points=50",
      "https://www.hackster.io/news.atom",
    },
    "./database.sqlite3", // sqlite3 db filepath
  )
  if err != nil {
    log.Fatalf("failed to create client: %s", err)
  }

  // (optional) configure client
  client.SetGoogleAIModels([]string{"gemini-2.5-flash"})
  client.SetDesiredLanguage("Korean")
  client.SetVerbose(true)

  // fetch feeds
  ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  defer cancel()

  feeds, err := client.FetchFeeds(ctx, true, 7) // ignore cached, ignore older than 7 days
  if err != nil {
    log.Printf("fetch error: %s", err)
  }

  // summarize and cache
  if err := client.SummarizeAndCacheFeeds(feeds); err != nil {
    log.Printf("summarize error: %s", err)
  }

  // list cached items
  items := client.ListCachedItems(false) // unread only

  // publish as RSS XML
  bytes, err := client.PublishXML(
    "My Feed", "https://example.com", "My summarized feeds",
    "author", "email@example.com",
    items,
  )
  if err != nil {
    log.Fatalf("failed to publish: %s", err)
  }

  log.Printf("RSS XML: %s", string(bytes))

  // mark as read and cleanup
  client.MarkCachedItemsAsRead(items)
  client.DeleteOldCachedItems()
}

Other sample applications are in the ./samples/ directory.

Documentation

Overview

Package rf for handling RSS feeds

Index

Constants

View Source
const (
	ErrorPrefixSummaryFailedWithError = `Summary failed with error`

	PublishContentType = `application/rss+xml`
)

Variables

This section is empty.

Functions

func Prettify

func Prettify(v any) string

Prettify prettifies given thing in JSON format.

func StandardizeJSON

func StandardizeJSON(b []byte) ([]byte, error)

StandardizeJSON standardizes given JSON (JWCC) bytes.

Types

type CachedItem

type CachedItem struct {
	gorm.Model

	Title       string
	Link        string // url to the original article
	Comments    string // url to the community comments
	GUID        string `gorm:"uniqueIndex"`
	Author      string
	PublishDate string
	Description string

	Summary      string
	MarkedAsRead bool `gorm:"index"`
}

CachedItem is a struct for a cached item

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client struct

func NewClient

func NewClient(
	googleAIAPIKeys []string,
	feedsURLs []string,
) *Client

NewClient returns a new client with memory cache.

func NewClientWithDB

func NewClientWithDB(
	googleAIAPIKeys []string,
	feedsURLs []string,
	dbFilepath string,
) (client *Client, err error)

NewClientWithDB returns a new client with SQLite DB cache.

func (*Client) DeleteOldCachedItems

func (c *Client) DeleteOldCachedItems() error

DeleteOldCachedItems deletes old cached items.

func (*Client) FetchFeeds

func (c *Client) FetchFeeds(
	ctx context.Context,
	ignoreAlreadyCached bool,
	ignoreItemsPublishedBeforeDays uint,
) ([]gofeed.Feed, error)

FetchFeeds fetches feeds.

func (*Client) ListCachedItems

func (c *Client) ListCachedItems(includeItemsMarkedAsRead bool) []CachedItem

ListCachedItems lists cached items.

func (*Client) MarkCachedItemsAsRead

func (c *Client) MarkCachedItemsAsRead(items []CachedItem) error

MarkCachedItemsAsRead marks given cached items as read.

func (*Client) PublishXML

func (c *Client) PublishXML(
	title, link, description, author, email string,
	items []CachedItem,
) (bytes []byte, err error)

PublishXML returns XML bytes (application/rss+xml) of given cached items.

func (*Client) SetDesiredLanguage

func (c *Client) SetDesiredLanguage(lang string)

SetDesiredLanguage sets the client's desired language for summaries.

func (*Client) SetGoogleAIModels added in v0.3.2

func (c *Client) SetGoogleAIModels(models []string)

SetGoogleAIModels sets the client's Google AI models.

func (*Client) SetSummarizeIntervalSeconds

func (c *Client) SetSummarizeIntervalSeconds(seconds int)

SetSummarizeIntervalSeconds sets the client's summarize interval seconds.

func (*Client) SetVerbose

func (c *Client) SetVerbose(v bool)

SetVerbose sets the client's verbose mode.

func (*Client) SummarizeAndCacheFeeds

func (c *Client) SummarizeAndCacheFeeds(
	ctx context.Context,
	feeds []gofeed.Feed,
	urlScrapper ...*ssg.Scrapper,
) (err error)

SummarizeAndCacheFeeds summarizes given feeds items and caches them.

Each feed item will be summarized with a timeout of `summarizeTimeoutSeconds` seconds.

If summary fails, the original content prepended with the error message will be cached.

If there was a retriable error(eg. model overloads), it will return immediately. (remaining feed items will be retried later)

type FeedsItemsCache

type FeedsItemsCache interface {
	Exists(guid string) bool
	Save(item gofeed.Item, title, summary string) error
	Fetch(guid string) *CachedItem
	MarkAsRead(guid string) error
	List(includeItemsMarkedAsRead bool) []CachedItem
	DeleteOlderThan1Month() error

	SetVerbose(v bool)
}

FeedsItemsCache is an interface of feeds items' cache

Directories

Path Synopsis
samples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL