Documentation
¶
Overview ¶
Package rss provides a simple RSS and Atom feed parser with context support. It supports RSS 2.0 and Atom feeds with proper error handling, timeout support, and resource management.
Basic usage:
ctx := context.Background()
resp, err := rss.Read(ctx, "https://example.com/feed.rss", false)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
channel, err := rss.Regular(ctx, resp)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Channel: %s\n", channel.Title)
Index ¶
- func InsecureRead(ctx context.Context, url string, reddit bool) (*http.Response, error)
- func Read(ctx context.Context, url string, reddit bool) (*http.Response, error)
- func ReadWithClient(ctx context.Context, url string, client *http.Client, reddit bool) (*http.Response, error)
- type Channel
- type Date
- type Entry
- type Feed
- type Fetcher
- type Item
- type ItemEnclosure
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsecureRead ¶
InsecureRead fetches an RSS or Atom feed from the given URL using an HTTP client that skips SSL certificate verification. This should only be used in development or testing environments where SSL verification is not critical.
WARNING: This function disables SSL certificate verification, making it vulnerable to man-in-the-middle attacks. Do not use in production environments.
func Read ¶
Read fetches an RSS or Atom feed from the given URL using the default HTTP client. The context is used for cancellation and timeout control. The reddit parameter should be set to true when fetching Reddit feeds to use the appropriate user agent header.
Returns an HTTP response that should be closed by the caller. The response body should be passed to either Regular() or Atom() for parsing.
func ReadWithClient ¶
func ReadWithClient(ctx context.Context, url string, client *http.Client, reddit bool) (*http.Response, error)
ReadWithClient fetches an RSS or Atom feed from the given URL using a custom HTTP client. This allows for custom transport configurations, timeouts, and other HTTP client settings. The context is used for cancellation and timeout control.
The client parameter allows you to customize the HTTP behavior, such as: - Setting custom timeouts - Configuring proxy settings - Adding custom headers - Implementing custom transport logic
Returns an HTTP response that should be closed by the caller. The response body should be passed to either Regular() or Atom() for parsing.
Types ¶
type Channel ¶
type Channel struct {
// Title is the name of the channel
Title string `xml:"title"`
// Link is the URL to the HTML website corresponding to the channel
Link string `xml:"link"`
// Description is a phrase or sentence describing the channel
Description string `xml:"description"`
// Language is the language the channel is written in
Language string `xml:"language"`
// LastBuildDate indicates the last time the content of the channel changed
LastBuildDate Date `xml:"lastBuildDate"`
// Item is a slice of items in the channel
Item []Item `xml:"item"`
}
Channel represents an RSS channel containing metadata and items. It follows the RSS 2.0 specification structure.
func ParseRegular ¶
ParseRegular parses an RSS 2.0 feed from an io.Reader. It expects the reader to contain valid RSS XML. The context is used for cancellation control during parsing.
The function automatically handles character encoding detection and conversion using the go-charset library, supporting various encodings commonly found in RSS feeds.
Returns a Channel struct containing the parsed RSS data and any error that occurred. The reader is not closed by this function; the caller is responsible for closing it.
func Regular ¶
Regular parses an RSS 2.0 feed from an HTTP response. It expects the response body to contain valid RSS XML. The context is used for cancellation control during parsing.
The function automatically handles character encoding detection and conversion using the go-charset library, supporting various encodings commonly found in RSS feeds.
Returns a Channel struct containing the parsed RSS data and any error that occurred. The response body is automatically closed after parsing.
type Date ¶
type Date string
Date represents a date string from RSS/Atom feeds. It provides methods for parsing dates in various formats commonly found in RSS and Atom feeds.
func (Date) Format ¶
Format parses the date and formats it using the specified format string. It first calls Parse() to convert the date string to a time.Time, then formats it using the provided format.
Returns the formatted date string and any error that occurred.
func (Date) MustFormat ¶
MustFormat parses the date and formats it using the specified format string. Unlike Format(), this function does not return an error. If parsing fails, it returns the error message as a string instead of panicking.
This is useful when you want to display a date but don't want to handle parsing errors explicitly.
func (Date) Parse ¶
Parse attempts to parse the date string using multiple common formats. It tries formats in the following order: 1. WordPress format (Mon, 02 Jan 2006 15:04:05 -0700) 2. RFC822 format with a named zone 3. RFC1123 format with a named zone 4. RFC822 format with a numeric zone 5. RFC3339 format (Atom standard)
Returns the parsed time and any error that occurred.
func (Date) ParseWithFormat ¶
ParseWithFormat parses the date string using the specified format. The format should follow Go's time format layout (reference time: Mon Jan 2 15:04:05 MST 2006).
Returns the parsed time and any error that occurred.
type Entry ¶
type Entry struct {
// ID is a permanent, universally unique identifier for the entry
ID string `xml:"id"`
// Title is the title of the entry
Title string `xml:"title"`
// Updated is the time when the entry was last modified
Updated string `xml:"updated"`
}
Entry represents a single entry in an Atom feed. Each entry typically represents a blog post, article, or other piece of content.
type Feed ¶
type Feed struct {
// Entry is a slice of entries in the feed
Entry []Entry `xml:"entry"`
}
Feed represents an Atom feed containing entries. It follows the Atom 1.0 specification structure.
func Atom ¶
Atom parses an Atom 1.0 feed from an HTTP response. It expects the response body to contain valid Atom XML. The context is used for cancellation control during parsing.
The function automatically handles character encoding detection and conversion using the go-charset library, supporting various encodings commonly found in Atom feeds.
Returns a Feed struct containing the parsed Atom data and any error that occurred. The response body is automatically closed after parsing.
func ParseAtom ¶
ParseAtom parses an Atom 1.0 feed from an io.Reader. It expects the reader to contain valid Atom XML. The context is used for cancellation control during parsing.
The function automatically handles character encoding detection and conversion using the go-charset library, supporting various encodings commonly found in Atom feeds.
Returns a Feed struct containing the parsed Atom data and any error that occurred. The reader is not closed by this function; the caller is responsible for closing it.
type Fetcher ¶
type Fetcher interface {
// Get fetches a resource from the given URL using the provided context.
// It returns an HTTP response and any error that occurred during the fetch.
Get(ctx context.Context, url string) (resp *http.Response, err error)
}
Fetcher defines the interface for fetching HTTP resources. This interface allows for custom implementations of HTTP clients while maintaining compatibility with the RSS parsing functions.
type Item ¶
type Item struct {
// Title is the title of the item
Title string `xml:"title"`
// Link is the URL of the item
Link string `xml:"link"`
// Comments is the URL of a page for comments relating to the item
Comments string `xml:"comments"`
// PubDate is the publication date of the item
PubDate Date `xml:"pubDate"`
// GUID is a string that uniquely identifies the item
GUID string `xml:"guid"`
// Category is a list of categories that the item belongs to
Category []string `xml:"category"`
// Enclosure is a list of media files associated with the item
Enclosure []ItemEnclosure `xml:"enclosure"`
// Description is a synopsis of the item
Description string `xml:"description"`
// Author is the email address of the author of the item
Author string `xml:"author"`
// Content is the full content of the item (if available)
Content string `xml:"content"`
// FullText is the complete text content of the item
FullText string `xml:"full-text"`
}
Item represents a single item in an RSS channel. Each item typically represents a story, article, or other piece of content.
type ItemEnclosure ¶
type ItemEnclosure struct {
// URL is the location of the enclosed file
URL string `xml:"url,attr"`
// Type is the MIME type of the enclosed file
Type string `xml:"type,attr"`
}
ItemEnclosure represents an enclosure element in an RSS item. Enclosures are used to include media files with RSS items.