SyndicationFeed
Base class for all syndication feeds. Subclasses should provide write()
Attributes
| Attribute | Type | Description |
|---|---|---|
| feed | dict | A dictionary containing the global metadata for the syndication feed, such as title, link, and language. |
| items | list = [] | 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
| Name | Type | Description |
|---|---|---|
| title | string | The title of the feed. |
| link | string | The URL of the feed's website. |
| description | string | A short description of the feed. |
| language | string = None | The language the feed is written in. |
| author_email | string = None | Email address of the feed author. |
| author_name | string = None | Name of the feed author. |
| author_link | string = None | URL for the feed author. |
| subtitle | string = None | A secondary title or subtitle for the feed. |
| categories | list = None | A list of categories the feed belongs to. |
| feed_url | string = None | The URL of the feed itself. |
| feed_copyright | string = None | Copyright notice for the feed content. |
| feed_guid | string = None | Unique identifier for the feed. |
| ttl | string = None | Time to live; number of minutes the feed can be cached. |
| stylesheets | list = None | A list of Stylesheet objects or strings for the feed. |
| **kwargs | dict | Additional 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
| Name | Type | Description |
|---|---|---|
| title | string | The title of the feed item |
| link | string | The URL of the item |
| description | string | A short summary or the full content of the item |
| author_email | string = null | The email address of the item's author |
| author_name | string = null | The name of the item's author |
| author_link | string = null | A URL for the author's website |
| pubdate | datetime.datetime = null | The initial publication date of the item |
| comments | string = null | A URL for a page containing comments related to this item |
| unique_id | string = null | A unique identifier for the item |
| unique_id_is_permalink | boolean = null | Flag indicating if the unique_id is a permanent URL |
| categories | iterable = () | A list of categories or tags associated with the item |
| item_copyright | string = null | Copyright information specific to this item |
| ttl | string = null | Time to Live: the number of minutes the item can be cached |
| updateddate | datetime.datetime = null | The date the item was last modified |
| enclosures | iterable | An 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
| Type | Description |
|---|---|
integer | The 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
| Type | Description |
|---|---|
object | A 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
| Name | Type | Description |
|---|---|---|
| handler | xml.sax.handler.ContentHandler | The 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
| Name | Type | Description |
|---|---|---|
| handler | xml.sax.handler.ContentHandler | The 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
| Name | Type | Description |
|---|---|---|
| item | object | The dictionary representing the feed item data |
Returns
| Type | Description |
|---|---|
object | A 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
| Name | Type | Description |
|---|---|---|
| handler | xml.sax.handler.ContentHandler | The SAX content handler used to write the item's XML elements |
| item | object | The 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
| Name | Type | Description |
|---|---|---|
| outfile | file-like object | The destination stream where the feed XML will be written |
| encoding | string | The 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
| Name | Type | Description |
|---|---|---|
| encoding | string | The character encoding to use for the generated string |
Returns
| Type | Description |
|---|---|
string | The 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
| Type | Description |
|---|---|
datetime.datetime | The most recent date found among items, or the current UTC time if none exist |