Plugins

This is a quick overview of the available plugins.

Output plugins

Archive

feed2exec.plugins.archive.DEFAULT_ARCHIVE_DIR = '/run/user/1000/'

default archive directory

feed2exec.plugins.archive.output(*args, feed=None, item=None, **kwargs)[source]

The archive plugin saves the feed’s item.link URLs into a directory, specified by DEFAULT_ARCHIVE_DIR or through the output args value.

Example:

[NASA breaking news]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
output = archive
args = /srv/archive/nasa/

The above will save the “NASA breaking news” into the /srv/archive/nasa directory. Do not use interpolation here as the feed’s variable could be used to mount a directory transversal attack.

Echo

class feed2exec.plugins.echo.output(*args, feed=None, **kwargs)[source]

This plugin outputs, to standard output, the arguments it receives. It can be useful to test your configuration. It also creates a side effect for the test suite to determine if the plugin was called.

This plugin does a similar thing when acting as a filter.

feed2exec.plugins.echo.filter

This filter just keeps the feed unmodified. It is just there for testing purposes.

alias of output

Error

feed2exec.plugins.error.output(*args, **kwargs)[source]

The error plugin is a simple plugin which raises an exception when called. It is designed for use in the test suite and should generally not be used elsewhere.

Exec

feed2exec.plugins.exec.output(command, *args, feed=None, **kwargs)[source]

The exec plugin is the ultimate security disaster. It simply executes whatever you feed it without any sort of sanitization. It does avoid to call to the shell and executes the command directly, however. Feed contents are also somewhat sanitized by the feedparser module, see the Sanitization documentation for more information in that regard. That is limited to stripping out hostile HTML tags, however.

You should be careful when sending arbitrary parameters to other programs. Even if we do not use the shell to execute the program, an hostile feed could still inject commandline flags to change the program behavior without injecting shell commands themselves.

For example, if a program can write files with the -o option, a feed could set their title to -oevil to overwrite the evil file. The only way to workaround that issue is to carefully craft the commandline so that this cannot happen.

Alternatively, writing a Python plugin is much safer as you can sanitize the arguments yourself.

Example:

[NASA Whats up?]
url = https://www.nasa.gov/rss/dyn/whats_up.rss
output = feed2exec.plugins.exec
args = wget -P /srv/archives/nasa/ {item.link}

The above is the equivalent of the archive plugin: it will save feed item links to the given directory.

Maildir

class feed2exec.plugins.maildir.output(to_addr=None, feed=None, item=None, lock=None, *args, **kwargs)[source]

The maildir plugin will save a feed item into a Maildir folder.

The configuration is a little clunky, but it should be safe against hostile feeds.

Parameters:to_addr (str) – the email to use as “to” (defaults to USER@localdomain)

Example:

[NASA breaking news]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
mailbox = ~/Maildir/
folder = nasa
args = me@example.com

The above will save new feed items from the NASA feed into the ~/Maildir/nasa/ maildir folder, and will set the To field of the email to me@example.com.

Mbox

class feed2exec.plugins.mbox.output(to_addr=None, feed=None, item=None, lock=None, *args, **kwargs)[source]

The mbox plugin will save a feed item in a Mbox mailbox.

This is mostly for testing purposes, but can of course be used in the unlikely event where you prefer mbox folders over the feed2exec.plugins.maildir plugin.

Parameters:to_addr (str) – the email to use as “to” (defaults to USER@localdomain)
Todo:There is some overlap between the code here and the maildir implementation. Refactoring may be in order, particularly if we add another mailbox format, though that is unlikely.

Null

feed2exec.plugins.null.output(*args, **kwargs)[source]

This plugin does nothing. It can be useful in cases where you want to catchup with imported feeds.

feed2exec.plugins.null.filter(item=None, *args, **kwargs)[source]

The null filter removes all elements from a feed item

Transmission

feed2exec.plugins.transmission.sanitize(text, repl='-')[source]

like utils.slug, but without lowercase and allow custom replacement

>>> sanitize('test')
'test'
>>> sanitize('../../../etc/password')
'etc-password'
>>> sanitize('Foo./.bar', repl='.')
'Foo.bar'
feed2exec.plugins.transmission.output(hostname='localhost', *args, feed=None, item=None, **kwargs)[source]

the transmission plugin will send feed items to a transmission instance

it assumes the transmission-remote command is already installed and configured to talk with transmission.

the hostname is passed in the args configuration and defaults to localhost. the folder parameter is also used to determine where to save the actual torrents files.

note that this will also append a sanitized version of the item title, if a folder is provided. this is to allow saving series in the same folder.

if the title is unique for each torrent, you may use a filter to set the title to the right location.

Wayback

feed2exec.plugins.wayback.output(*args, feed=None, item=None, **kwargs)[source]

This plugin saves the feed items link element to the wayback machine. It will retry URLs that fail, so it may be necessary to manually catchup feeds if they have broken link fields.

Example:

[NASA IOTD wayback]
url = https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss
output = feed2exec.plugins.wayback

The above will save the Image of the day updates to the wayback machine.

Filter plugins

Droptitle

feed2exec.plugins.droptitle.filter(*args, feed=None, item=None, **kwargs)[source]

the droptitle filter will drop any feed item with a title matching the given args.

Example:

[NASA breaking news]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
filter = feed2exec.plugins.droptitle
filter_args = Trump

The above will process the feed items according to the global configuration, but will skip any item that has the word “Trump” anywhere in the title field.

Emptysummary

feed2exec.plugins.emptysummary.filter(*args, feed=None, item=None, **kwargs)[source]

example of fixes for a broken feed, in this case, the GitHub release feed which (sometimes) sends empty contents, in which case the item link field is used as a summary instead.

Html2text

class feed2exec.plugins.html2text.filter(*args, feed=None, item=None, **kwargs)[source]

This filter plugin takes a given feed item and adds a content_plain field with the HTML parsed as text.

Important

the html2text plugin is called automatically from the email output plugins and should normally not be called directly.

static parse(html=None)[source]

parse html to text according to our preferences. this is where subclasses can override the HTML2Text settings or use a completely different parser

Ikiwiki Recentchanges

feed2exec.plugins.ikiwiki_recentchanges.filter(*args, item=None, **kwargs)[source]

the ikiwiki_recentchanges plugin fixes links in ikiwiki feeds

Ikiwiki recent changes show all the recent edits to pages, but the <link> element doesn’t point to the edit page: it points to the recent changes page itself, which make them useless for link checking or archival purposes.

This parses the recent changes entries and extracts the relevant links from it.

An alternative to this is to use the following entry to generate a special feed in Ikiwiki:

[[!inline pages="*" feeds=yes feedonly=yes feedfile=archive show=10]]

This generates a feed with proper <link> elements but requires write access to the wiki.

Writing new plugins

Most of the actual work in the program is performed by plugins. A plugin is a simple Python module that has a output or filter “callable” (function or class) with a predefined interface.

Basic plugin principles

To write a new plugin, you should start by creating a simple Python module, in your PYTHONPATH. You can find which directories are in the path by calling:

$ python3 -c "import sys; print(sys.path)"
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']

In the above example, a good location would be /usr/local/lib/python3.5/dist-packages. The naming convention is loose: as long as the plugin matches the expected API, it should just work. For the purpose of this demonstration, we’ll call our plugin trumpery, so we will create the plugin code like this:

touch /usr/local/lib/python3.5/dist-packages/trumpery.py

Naturally, if you are going to write multiple plugins, you may want to regroup your multiple plugins in a package, see the module documentation for more information about this concept in Python.

You are welcome to distribute plugins separately or send them as merge requests, see Contributor’s guide for more information on how to participate in this project. We of course welcome contributions to this documentation as well!

Filters

Now, you need your plugin to do something. In our case, let’s say we’d like to skip any feed entry that has the word Trump in it. For that purpose, we’ll create a plugin similar to the already existing feed2exec.plugins.droptitle plugin, but that operates on the body of the feed, but that also hardcodes the word, because this is just a demonstration and we want to keep it simple. Let’s look at the title plugin to see how it works:

def filter(*args, feed=None, item=None, **kwargs):
    '''the droptitle filter will drop any feed item with a title matching
    the given args.

    Example::

      [NASA breaking news]
      url = https://www.nasa.gov/rss/dyn/breaking_news.rss
      filter = feed2exec.plugins.droptitle
      filter_args = Trump

    The above will process the feed items according to the global
    configuration, but will skip any item that has the word "Trump"
    anywhere in the title field.
    '''
    item['skip'] = ' '.join(args) in item.get('title', '')

That may look like complete gibberish to you if you are not familiar with programming or with Python programming in particular. But let’s take this from the top and copy that in our own plugin. The first line declares a function that takes at least a feed and a item argument, but can also accept any other arbitrary argument. This is important because we want to have the plugin keep on working if the plugin API changes in the future. This is called “forward-compatibility”. So let’s copy that in our plugin and add a pass statement to make sure the plugin works (even if it does nothing for now):

def filter(*args, feed=None, item=None, **kwargs):
    pass

We can already test our plugin by adding it to our configuration, in ~/.config/feed2exec/feed2exec.ini:

[NASA]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
output = feed2exec.plugins.echo
args = {item.title}
filter = trumpery

Notice how we use the output plugin to show the title of feed items selected, as a debugging tool. Let’s fetch this feed in debugging mode to see what happens:

$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
arguments received: ('President Trump Welcomes Home Record-breaking NASA Astronaut Peggy Whitson',)
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
arguments received: ('NASA Statement on Nomination for Agency Administrator',)
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed

Good! The feed is fetched and items are displayed. It means our filter didn’t interfere, but now it’s time to make it do something. To skip items, we need to set the skip attribute for the feed item to True if we want to skip it and False otherwise. So we’ll use a simple recipe, a bit like droptitle does, but simpler, to look at the feed content to look for our evil word. The feedparser documentation tells us feed items have a summary field which we can inspect. There’s also a content list, but that’s a little more complicated so we’ll skip that for now. So, let’s set the skip parameter to match if there is the evil word in our feed item, like this:

def filter(*args, feed=None, item=None, **kwargs):
    item['skip'] = 'Trump' in item.get('summary', '')

And let’s see the result (note that we use the --force argument here otherwise we would just skip all items because of the cache):

$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
item President Trump Welcomes Home Record-breaking NASA Astronaut Peggy Whitson of feed NASA filtered out
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
item NASA Statement on Nomination for Agency Administrator of feed NASA filtered out
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed

Successs! We have skipped the two items that contain the fraud we wanted to remove from the world. Notice how we were able to modify the feed item: we can also use that to change the feed content. Normally, we would use this to fix malformed feeds, but let’s have some fun instead and rename Trump to Drumpf:

def filter(*args, feed=None, item=None, **kwargs):
    item['title'] = item.get('title', '').replace('Trump', 'Drumpf')

And the result:

$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
arguments received: ('President Drumpf Welcomes Home Record-breaking NASA Astronaut Peggy Whitson',)
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
arguments received: ('NASA Statement on Nomination for Agency Administrator',)
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed

I know, absolutely hilarious, right? More seriously, this is also how the feed2exec.plugins.html2text filter works, which is enabled by default and helps the email output plugin do its job by turning HTML into text. At this point, the only limit is your knowledge of Python programming and your imagination!

Output plugins

Output plugins are another beast entirely. While they operate with the same principle than filter plugins (search path and function signature are similar), they are designed to actually output something for each new feed item found. This can be anything: a file, email, HTTP request, whatever. If there is a commandline tool that does what you need, it is probably simpler to just call the exec plugin and there are numerous examples of this in the sample configuration file. For more complex things, however, it may be easier to actually write this as a Python.

Basic arguments

For our example, we’ll write an archival plugin which writes each new entry to a file hierarchy. First, we start with the same simple function signature as filters, except we name it output:

def output(*args, feed=None, item=None, **kwargs):
    pass

This is the equivalent of the null plugin and basically outputs nothing at all. To archive the feed items, we’ll need to look at the link element feedparser gives us. Let’s see what that looks like for the NASA feed:

def output(*args, feed=None, item=None, **kwargs):
    # only operate on items that actually have a link
    if item.get('link'):
        print(item.get('link', ''))
    else:
        logging.info('no link for feed item %s, not archiving', item.get('title'))

Note

Note that we try to make plugins silent in general. You can use logging.info() to have things show up in --verbose and logging.debug() for --debug but by default, your plugin should be silent unless there’s an error that requires the user’s intervention, in which case you should use logging.warning() for transient errors that may be automatically recovered and logging.error() for errors that require user intervention. This is to allow users to ignore warnings safely.

Note that here we first check to see if the feed item actually has a link - not all feeds do! After adding the above to our trumpery plugin and adding it as an output plugin:

[NASA]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
output = trumpery
filter = trumpery

We can try to see what happens when we call it:

$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
http://www.nasa.gov/press-release/president-trump-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson
http://www.nasa.gov/press-release/three-international-space-station-crewmates-safely-return-to-earth
http://www.nasa.gov/press-release/nasa-statement-on-nomination-for-agency-administrator
http://www.nasa.gov/press-release/nasa-television-to-air-return-of-three-international-space-station-crew-members
http://www.nasa.gov/press-release/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary
http://www.nasa.gov/press-release/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey
http://www.nasa.gov/press-release/nasa-cancels-planned-media-availabilities-with-astronauts
http://www.nasa.gov/press-release/nasa-awards-400000-to-top-teams-at-second-phase-of-3d-printing-competition
http://www.nasa.gov/press-release/nasa-awards-contract-for-center-protective-services-for-glenn-research-center
http://www.nasa.gov/press-release/nasa-announces-cassini-end-of-mission-media-activities
1 feeds processed

Sanitizing contents

Good. Those are the URLs we want to save to disk. Let’s start by just writing those to a file. We will also use a simple slug function to make a filesystem-safe name from the feed title and save those files in a pre-determined location:

import logging
import os.path
from feed2exec.utils import slug

ARCHIVE_DIR='/run/user/1000/feed-archives/'

def output(*args, feed=None, item=None, **kwargs):
    # make a safe path from the item name
    path = slug(item.get('title', 'no-name'))
    # put the file in the archive directory
    path = os.path.join(ARCHIVE_DIR, path)
    # only operate on items that actually have a link
    if item.get('link'):
        # tell the user what's going on, if verbose
        # otherwise, we try to stay silent if all goes well
        logging.info('saving feed item %s to %s from %s',
                     item.get('title'), path, item.get('link'))
        # open the file
        with open(path, 'w') as archive:
            # write the response
            archive.write(item.get('link'))
    else:
        logging.info('no link for feed item %s, not archiving', item.get('title'))

Now I know this may look like a huge step from the previous one but I’m sorry, I couldn’t find a simpler second step. :) The output now looks like this:

$ python3 -m feed2exec --config ./doc/ --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
saving feed item President Drumpf Welcomes Home Record-breaking NASA Astronaut Peggy Whitson to /run/user/1000/president-drumpf-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson from http://www.nasa.gov/press-release/president-trump-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson
saving feed item Three International Space Station Crewmates Safely Return to Earth to /run/user/1000/three-international-space-station-crewmates-safely-return-to-earth from http://www.nasa.gov/press-release/three-international-space-station-crewmates-safely-return-to-earth
saving feed item NASA Statement on Nomination for Agency Administrator to /run/user/1000/nasa-statement-on-nomination-for-agency-administrator from http://www.nasa.gov/press-release/nasa-statement-on-nomination-for-agency-administrator
saving feed item NASA Television to Air Return of Three International Space Station Crew Members to /run/user/1000/nasa-television-to-air-return-of-three-international-space-station-crew-members from http://www.nasa.gov/press-release/nasa-television-to-air-return-of-three-international-space-station-crew-members
saving feed item NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary to /run/user/1000/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary from http://www.nasa.gov/press-release/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary
saving feed item NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey to /run/user/1000/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey from http://www.nasa.gov/press-release/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey
saving feed item NASA Cancels Planned Media Availabilities with Astronauts to /run/user/1000/nasa-cancels-planned-media-availabilities-with-astronauts from http://www.nasa.gov/press-release/nasa-cancels-planned-media-availabilities-with-astronauts
saving feed item NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition to /run/user/1000/nasa-awards-400-000-to-top-teams-at-second-phase-of-3d-printing-competition from http://www.nasa.gov/press-release/nasa-awards-400000-to-top-teams-at-second-phase-of-3d-printing-competition
saving feed item NASA Awards Contract for Center Protective Services for Glenn Research Center to /run/user/1000/nasa-awards-contract-for-center-protective-services-for-glenn-research-center from http://www.nasa.gov/press-release/nasa-awards-contract-for-center-protective-services-for-glenn-research-center
saving feed item NASA Announces Cassini End-of-Mission Media Activities to /run/user/1000/nasa-announces-cassini-end-of-mission-media-activities from http://www.nasa.gov/press-release/nasa-announces-cassini-end-of-mission-media-activities

Sweet! Now it’s not really nice to save this in /run/user/1000. I just chose this directory because it was a safe place to write but it’s not a persistent directory. Best make that configurable, which is where plugin arguments come in.

User configuration

You see that *args parameter? That comes straight from the configuration file. So you could set the path in the configuration file, like this:

[NASA]
url = https://www.nasa.gov/rss/dyn/breaking_news.rss
output = trumpery
args = /srv/archives/nasa/
filter = trumpery

We also need to modify the plugin to fetch that configuration, like this:

def output(*args, feed=None, item=None, **kwargs):
    # make a safe path from the item name
    path = slug(item.get('title', 'no-name'))
    # take the archive dir from the user or use the default
    archive_dir = ' '.join(args) if args else DEFAULT_ARCHIVE_DIR
    # put the file in the archive directory
    path = os.path.join(archive_dir, path)
    # [...]
    # rest of the function unchanged

Making HTTP requests

And now obviously, we only saved the link itself, not the link content. For that we need some help from the requests module, and do something like this:

# fetch the URL in memory
result = requests.get(item.get('link'))
if result.status_code != requests.codes.ok:
    logging.warning('failed to fetch link %s: %s',
                    item.get('link'), result.status_code)
    # make sure we retry next time
    return False
# open the file
with open(path, 'w') as archive:
    # write the response
    archive.write(result.text)

This will save the actual link content (result.text) to the file. The important statement here is:

# fetch the URL in memory
result = requests.get(item.get('link'))

which fetches the URL in memory and checks for errors. The other change in the final plugin is simply:

archive.write(result.text)

which writes the article content instead of the link.

Plugin return values

Notice how we return False here: this makes the plugin system avoid adding the item to the cache, so it is retried on the next run. If the plugin returns True or nothing (None), the plugin is considered to have succeeded and the entry is added to the cache. That logic is defined in feed2exec.feeds.parse().

Catchup

A final thing that is missing that is critical in all plugins is to respect the catchup setting. It is propagated up from the commandline or configuration all the way down to plugins, through the feed parameters. How you handle it varies from plugin to plugin, but the basic idea is to give feedback (when verbose) of activity when the plugin is run but to not actually do anything. In our case, we simply return success, right before we fetch the URL:

if feed.get('catchup'):
    return True
# fetch the URL in memory
result = requests.get(item.get('link'))

Notice how we still fetch the actual feed content but stop before doing any permanent operation. That is the spirit of the “catchup” operation: we not only skip “write” operation, but also any operation which could slow down the “catchup”: fetching stuff over the network takes time and while it can be considered a “readonly” operation as far as the local machine is concerned, we are effectively writing to the network so that operation shouldn’t occur.

Hopefully that should get you going with most of the plugins you are thinking of writing!

See also

feed2exec(1)