2013-05-03 17:13:08 +02:00
|
|
|
=============
|
2013-05-04 20:08:08 +02:00
|
|
|
Feedgenerator
|
2013-05-03 17:13:08 +02:00
|
|
|
=============
|
|
|
|
|
2016-12-22 00:05:55 +01:00
|
|
|
.. image:: https://travis-ci.org/lkiesow/python-feedgen.svg?branch=master
|
|
|
|
:target: https://travis-ci.org/lkiesow/python-feedgen
|
|
|
|
:alt: Build Status
|
|
|
|
|
2017-02-05 23:22:15 +01:00
|
|
|
.. image:: https://coveralls.io/repos/github/lkiesow/python-feedgen/badge.svg?branch=master
|
|
|
|
:target: https://coveralls.io/github/lkiesow/python-feedgen?branch=master
|
2017-02-05 23:17:41 +01:00
|
|
|
:alt: Test Coverage Status
|
|
|
|
|
2015-10-29 00:45:14 +01:00
|
|
|
|
2018-12-14 23:55:36 +01:00
|
|
|
This module can be used to generate web feeds in both ATOM and RSS format. It
|
|
|
|
has support for extensions. Included is for example an extension to produce
|
|
|
|
Podcasts.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-04 22:24:54 +02:00
|
|
|
It is licensed under the terms of both, the FreeBSD license and the LGPLv3+.
|
2013-05-04 20:08:08 +02:00
|
|
|
Choose the one which is more convenient for you. For more details have a look
|
|
|
|
at license.bsd and license.lgpl.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-16 19:10:48 +02:00
|
|
|
More details about the project:
|
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
- `Repository <https://github.com/lkiesow/python-feedgen>`_
|
|
|
|
- `Documentation <https://lkiesow.github.io/python-feedgen/>`_
|
|
|
|
- `Python Package Index <https://pypi.python.org/pypi/feedgen/>`_
|
2013-05-16 19:10:48 +02:00
|
|
|
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-05 00:43:31 +02:00
|
|
|
------------
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
**Prebuild packages**
|
|
|
|
|
2019-08-11 22:45:27 +02:00
|
|
|
If your distribution includes this project as package, like Fedora Linux does,
|
|
|
|
you can simply use your package manager to install the package. For example::
|
2013-05-05 00:43:31 +02:00
|
|
|
|
2019-08-11 22:45:27 +02:00
|
|
|
$ dnf install python3-feedgen
|
2015-10-30 01:03:26 +01:00
|
|
|
|
2013-05-05 00:43:31 +02:00
|
|
|
|
|
|
|
**Using pip**
|
|
|
|
|
|
|
|
You can also use pip to install the feedgen module. Simply run::
|
|
|
|
|
2015-10-28 22:31:20 +01:00
|
|
|
$ pip install feedgen
|
2013-05-05 00:43:31 +02:00
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
|
2013-05-03 17:13:08 +02:00
|
|
|
-------------
|
|
|
|
Create a Feed
|
|
|
|
-------------
|
|
|
|
|
2018-07-04 09:29:15 +02:00
|
|
|
To create a feed simply instantiate the FeedGenerator class and insert some
|
2020-01-25 22:09:52 +01:00
|
|
|
data:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from feedgen.feed import FeedGenerator
|
|
|
|
fg = FeedGenerator()
|
|
|
|
fg.id('http://lernfunk.de/media/654321')
|
|
|
|
fg.title('Some Testfeed')
|
|
|
|
fg.author( {'name':'John Doe','email':'john@example.de'} )
|
|
|
|
fg.link( href='http://example.com', rel='alternate' )
|
|
|
|
fg.logo('http://ex.com/logo.jpg')
|
|
|
|
fg.subtitle('This is a cool feed!')
|
|
|
|
fg.link( href='http://larskiesow.de/test.atom', rel='self' )
|
|
|
|
fg.language('en')
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
Note that for the methods which set fields that can occur more than once in a
|
|
|
|
feed you can use all of the following ways to provide data:
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-04 20:08:08 +02:00
|
|
|
- Provide the data for that element as keyword arguments
|
|
|
|
- Provide the data for that element as dictionary
|
|
|
|
- Provide a list of dictionaries with the data for several elements
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
Example:
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
fg.contributor( name='John Doe', email='jdoe@example.com' )
|
|
|
|
fg.contributor({'name':'John Doe', 'email':'jdoe@example.com'})
|
|
|
|
fg.contributor([{'name':'John Doe', 'email':'jdoe@example.com'}, ...])
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
-----------------
|
|
|
|
Generate the Feed
|
|
|
|
-----------------
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
After that you can generate both RSS or ATOM by calling the respective method:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
atomfeed = fg.atom_str(pretty=True) # Get the ATOM feed as string
|
|
|
|
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
|
|
|
|
fg.atom_file('atom.xml') # Write the ATOM feed to a file
|
|
|
|
fg.rss_file('rss.xml') # Write the RSS feed to a file
|
2013-05-03 17:13:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
----------------
|
|
|
|
Add Feed Entries
|
|
|
|
----------------
|
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
To add entries (items) to a feed you need to create new FeedEntry objects and
|
|
|
|
append them to the list of entries in the FeedGenerator. The most convenient
|
|
|
|
way to go is to use the FeedGenerator itself for the instantiation of the
|
2020-01-25 22:09:52 +01:00
|
|
|
FeedEntry object:
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
.. code-block:: python
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
fe = fg.add_entry()
|
|
|
|
fe.id('http://lernfunk.de/media/654321/1')
|
|
|
|
fe.title('The First Episode')
|
|
|
|
fe.link(href="http://lernfunk.de/feed")
|
|
|
|
|
|
|
|
The FeedGenerator's method `add_entry(...)` will generate a new FeedEntry
|
|
|
|
object, automatically append it to the feeds internal list of entries and
|
|
|
|
return it, so that additional data can be added.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
----------
|
|
|
|
Extensions
|
|
|
|
----------
|
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
The FeedGenerator supports extensions to include additional data into the XML
|
|
|
|
structure of the feeds. Extensions can be loaded like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2013-05-05 20:36:35 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
fg.load_extension('someext', atom=True, rss=True)
|
2013-05-05 20:36:35 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
This example would try to load the extension “someext” from the file
|
|
|
|
`ext/someext.py`. It is required that `someext.py` contains a class named
|
|
|
|
“SomextExtension” which is required to have at least the two methods
|
|
|
|
`extend_rss(...)` and `extend_atom(...)`. Although not required, it is strongly
|
|
|
|
suggested to use `BaseExtension` from `ext/base.py` as superclass.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-05 20:36:35 +02:00
|
|
|
`load_extension('someext', ...)` will also try to load a class named
|
|
|
|
“SomextEntryExtension” for every entry of the feed. This class can be located
|
|
|
|
either in the same file as SomextExtension or in `ext/someext_entry.py` which
|
|
|
|
is suggested especially for large extensions.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2016-08-28 21:24:01 +02:00
|
|
|
The parameters `atom` and `rss` control if the extension is used for ATOM and
|
2020-01-25 22:09:52 +01:00
|
|
|
RSS feeds respectively. The default value for both parameters is `True`,
|
2016-08-28 21:24:01 +02:00
|
|
|
meaning the extension is used for both kinds of feeds.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2016-01-01 03:43:23 +01:00
|
|
|
**Example: Producing a Podcast**
|
2013-05-05 20:36:35 +02:00
|
|
|
|
|
|
|
One extension already provided is the podcast extension. A podcast is an RSS
|
|
|
|
feed with some additional elements for ITunes.
|
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
To produce a podcast simply load the `podcast` extension:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2013-05-05 20:36:35 +02:00
|
|
|
|
2020-01-25 22:09:52 +01:00
|
|
|
from feedgen.feed import FeedGenerator
|
|
|
|
fg = FeedGenerator()
|
|
|
|
fg.load_extension('podcast')
|
2015-10-28 22:31:20 +01:00
|
|
|
...
|
2020-01-25 22:09:52 +01:00
|
|
|
fg.podcast.itunes_category('Technology', 'Podcasting')
|
2015-10-28 22:31:20 +01:00
|
|
|
...
|
2020-01-25 22:09:52 +01:00
|
|
|
fe = fg.add_entry()
|
|
|
|
fe.id('http://lernfunk.de/media/654321/1/file.mp3')
|
|
|
|
fe.title('The First Episode')
|
|
|
|
fe.description('Enjoy our first episode.')
|
|
|
|
fe.enclosure('http://lernfunk.de/media/654321/1/file.mp3', 0, 'audio/mpeg')
|
2015-10-28 22:31:20 +01:00
|
|
|
...
|
2020-01-25 22:09:52 +01:00
|
|
|
fg.rss_str(pretty=True)
|
|
|
|
fg.rss_file('podcast.xml')
|
2013-05-05 20:36:35 +02:00
|
|
|
|
2018-12-14 23:55:36 +01:00
|
|
|
If the FeedGenerator class is used to load an extension, it is automatically
|
|
|
|
loaded for every feed entry as well. You can, however, load an extension for a
|
|
|
|
specific FeedEntry only by calling `load_extension(...)` on that entry.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2018-12-14 23:55:36 +01:00
|
|
|
Even if extensions are loaded, they can be temporarily disabled during the feed
|
|
|
|
generation by calling the generating method with the keyword argument
|
|
|
|
`extensions` set to `False`.
|
2016-08-28 21:24:01 +02:00
|
|
|
|
2016-08-28 21:25:53 +02:00
|
|
|
**Custom Extensions**
|
2016-08-28 21:24:01 +02:00
|
|
|
|
2018-12-14 23:55:36 +01:00
|
|
|
If you want to load custom extensions which are not part of the feedgen
|
2016-08-28 21:24:01 +02:00
|
|
|
package, you can use the method `register_extension` instead. You can directly
|
|
|
|
pass the classes for the feed and the entry extension to this method meaning
|
|
|
|
that you can define them everywhere.
|
2013-05-03 17:13:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
Testing the Generator
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
You can test the module by simply executing::
|
|
|
|
|
2015-10-28 22:31:20 +01:00
|
|
|
$ python -m feedgen
|
2013-05-03 17:13:08 +02:00
|
|
|
|
2013-05-03 17:27:47 +02:00
|
|
|
If you want to have a look at the code for this test to have a working code
|
|
|
|
example for a whole feed generation process, you can find it in the
|
2016-12-22 00:05:55 +01:00
|
|
|
`__main__.py <https://github.com/lkiesow/python-feedgen/blob/master/feedgen/__main__.py>`_.
|