Skip to main content

SyndicationFeed

Base class for all syndication feeds. Subclasses should provide write()

Attributes

AttributeTypeDescription
feeddictA dictionary containing the global metadata for the syndication feed, such as title, link, and language.
itemslist = []A list of dictionaries where each entry represents an individual item or post added to the feed.

Constructor

Signature

def SyndicationFeed(
title: string,
link: string,
description: string,
language: string = None,
author_email: string = None,
author_name: string = None,
author_link: string = None,
subtitle: string = None,
categories: list = None,
feed_url: string = None,
feed_copyright: string = None,
feed_guid: string = None,
ttl: string = None,
stylesheets: list = None,
**kwargs: dict
)

Parameters

NameTypeDescription
titlestringThe title of the feed.
linkstringThe URL of the feed's website.
descriptionstringA short description of the feed.
languagestring = NoneThe language the feed is written in.
author_emailstring = NoneEmail address of the feed author.
author_namestring = NoneName of the feed author.
author_linkstring = NoneURL for the feed author.
subtitlestring = NoneA secondary title or subtitle for the feed.
categorieslist = NoneA list of categories the feed belongs to.
feed_urlstring = NoneThe URL of the feed itself.
feed_copyrightstring = NoneCopyright notice for the feed content.
feed_guidstring = NoneUnique identifier for the feed.
ttlstring = NoneTime to live; number of minutes the feed can be cached.
stylesheetslist = NoneA list of Stylesheet objects or strings for the feed.
**kwargsdictAdditional custom keyword arguments for the feed metadata.

Methods


add_item()

@classmethod
def add_item(
title: string,
link: string,
description: string,
author_email: string = null,
author_name: string = null,
author_link: string = null,
pubdate: datetime.datetime = null,
comments: string = null,
unique_id: string = null,
unique_id_is_permalink: boolean = null,
categories: iterable = (),
item_copyright: string = null,
ttl: string = null,
updateddate: datetime.datetime = null,
enclosures: iterable
)

Add an item to the feed. All args are expected to be strings except pubdate and updateddate, which are datetime.datetime objects, and enclosures, which is an iterable of instances of the Enclosure class.

Parameters

NameTypeDescription
titlestringThe title of the feed item
linkstringThe URL of the item
descriptionstringA short summary or the full content of the item
author_emailstring = nullThe email address of the item's author
author_namestring = nullThe name of the item's author
author_linkstring = nullA URL for the author's website
pubdatedatetime.datetime = nullThe initial publication date of the item
commentsstring = nullA URL for a page containing comments related to this item
unique_idstring = nullA unique identifier for the item
unique_id_is_permalinkboolean = nullFlag indicating if the unique_id is a permanent URL
categoriesiterable = ()A list of categories or tags associated with the item
item_copyrightstring = nullCopyright information specific to this item
ttlstring = nullTime to Live: the number of minutes the item can be cached
updateddatedatetime.datetime = nullThe date the item was last modified
enclosuresiterableAn iterable of Enclosure instances representing media attachments

num_items()

@classmethod
def num_items() - > integer

Calculates the total number of items currently added to the feed.

Returns

TypeDescription
integerThe count of items in the feed list

root_attributes()

@classmethod
def root_attributes() - > object

Return extra attributes to place on the root (i.e. feed/channel) element. Called from write().

Returns

TypeDescription
objectA dictionary of attribute names and values for the root element

add_root_elements()

@classmethod
def add_root_elements(
handler: xml.sax.handler.ContentHandler
)

Add elements in the root (i.e. feed/channel) element. Called from write().

Parameters

NameTypeDescription
handlerxml.sax.handler.ContentHandlerThe SAX content handler used to write the XML elements

add_stylesheets()

@classmethod
def add_stylesheets(
handler: xml.sax.handler.ContentHandler
)

Add stylesheet(s) to the feed. Called from write().

Parameters

NameTypeDescription
handlerxml.sax.handler.ContentHandlerThe SAX content handler used to write the stylesheet processing instructions

item_attributes()

@classmethod
def item_attributes(
item: object
) - > object

Return extra attributes to place on each item (i.e. item/entry) element.

Parameters

NameTypeDescription
itemobjectThe dictionary representing the feed item data

Returns

TypeDescription
objectA dictionary of attribute names and values for a specific item element

add_item_elements()

@classmethod
def add_item_elements(
handler: xml.sax.handler.ContentHandler,
item: object
)

Add elements on each item (i.e. item/entry) element.

Parameters

NameTypeDescription
handlerxml.sax.handler.ContentHandlerThe SAX content handler used to write the item's XML elements
itemobjectThe dictionary representing the feed item data

write()

@classmethod
def write(
outfile: file-like object,
encoding: string
)

Output the feed in the given encoding to outfile, which is a file-like object. Subclasses should override this.

Parameters

NameTypeDescription
outfilefile-like objectThe destination stream where the feed XML will be written
encodingstringThe character encoding to use for the output

writeString()

@classmethod
def writeString(
encoding: string
) - > string

Return the feed in the given encoding as a string.

Parameters

NameTypeDescription
encodingstringThe character encoding to use for the generated string

Returns

TypeDescription
stringThe complete feed XML as a string in the specified encoding

latest_post_date()

@classmethod
def latest_post_date() - > datetime.datetime

Return the latest item's pubdate or updateddate. If no items have either of these attributes this return the current UTC date/time.

Returns

TypeDescription
datetime.datetimeThe most recent date found among items, or the current UTC time if none exist