Merge branch 'add-feedname' into 'master'

Add the ability to use {feedname} in the tweet template

See merge request !7
This commit is contained in:
Carl Chenet 2017-07-31 13:33:59 +00:00
commit 67b7e72507
4 changed files with 39 additions and 18 deletions

View file

@ -5,7 +5,7 @@ As a prerequisite to use Feed2toot, you need to authorize a Mastodon app for you
Just use the script register_feed2toot_app to register the feed2toot app for your account.::
$ ./register_feed2toot_app
$ ./register_feed2toot_app
This app generates Mastodon app credentials needed by Feed2toot.
feed2toot_clientcred.txt and feed2toot_usercred.txt will be written in the current dir /home/chaica/progra/python/feed2toot.
@ -107,6 +107,15 @@ Now let's have a look at the =/home/john/feed2toot/rsslist.txt file::
Each line of this file is a url to a rss feed. Pretty simple.
Display the name of the feed in the toots
-----------------------------------------
If you want to display the name of the feed in the resulting toot, you can do so by giving it a name with the following syntax::
Le journal du hacker <https://www.journalduhacker.net/rss/>
Then in the `tweet` configuration, you can use the `{feedname}` syntax, which will be replaced by the actual name of the feed.
Match specific patterns of rss feeds in the uri_list files
----------------------------------------------------------
You can use specific pattern matching for uri in the uri_list file to filter some of the rss entries of a rss feed. Lets modify the previous file::

View file

@ -23,6 +23,7 @@ import os
import os.path
import socket
import sys
import re
# 3rd party library imports
import feedparser
@ -53,9 +54,9 @@ class ConfParse(object):
self.accept_bozo_exceptions = False
###########################
#
#
# the rss section
#
#
###########################
section = 'rss'
if config.has_section(section):
@ -108,6 +109,12 @@ class ConfParse(object):
for line in rsslist:
line = line.strip()
# split each line in two parts, rss link and a string with the different patterns to look for
feedname = None
if '<' in line:
matches = re.match('(.*) <(.*)>', line)
if not matches:
sys.exit('This line in the list of uri to parse is not formatted correctly: {line}'.format(line))
feedname, line = matches.groups()
confobjects = line.split('|')
if len(confobjects) > 3 or len(confobjects) == 2:
sys.exit('This line in the list of uri to parse is not formatted correctly: {line}'.format(line))
@ -132,7 +139,7 @@ class ConfParse(object):
sys.exit('The rss object {rssobject} could not be found in the feed {rss}'.format(rssobject=rssobject, rss=rss))
else:
sys.exit('The rss feed {rss} does not seem to be valid'.format(rss=rss))
feeds.append({'feed': feed, 'patterns': patterns, 'rssobject': rssobject})
feeds.append({'feed': feed, 'patterns': patterns, 'rssobject': rssobject, 'feedname': feedname})
# test if all feeds in the list were unsuccessfully retrieved and if so, leave
if not feeds and bozoexception:
sys.exit('No feed could be retrieved. Leaving.')
@ -161,9 +168,9 @@ class ConfParse(object):
if config.has_option(section, currentoption):
options['nopatternurinoglobalpattern'] = config.getboolean(section, currentoption)
###########################
#
#
# the cache section
#
#
###########################
section = 'cache'
if not self.clioptions.cachefile:
@ -191,9 +198,9 @@ class ConfParse(object):
else:
options['cache_limit'] = 100
###########################
#
#
# the hashtag section
#
#
###########################
section = 'hashtaglist'
if not self.clioptions.hashtaglist:
@ -207,9 +214,9 @@ class ConfParse(object):
else:
options['hashtaglist'] = ''
###########################
#
#
# the plugins section
#
#
###########################
plugins = {}
section = 'influxdb'
@ -236,7 +243,7 @@ class ConfParse(object):
self.confs.append((options, config, self.tweetformat, feeds, plugins))
else:
self.confs.append((options, config, self.tweetformat, [{'feed': feed, 'patterns': [], 'rssobject': ''}], plugins))
@property
def confvalues(self):
'''Return the values of the different configuration files'''

View file

@ -27,7 +27,7 @@ import feedparser
class FilterEntry(object):
'''FilterEntry class'''
def __init__(self, elements, entry, options, byrsspatterns, rssobject):
def __init__(self, elements, entry, options, byrsspatterns, rssobject, feedname):
'''Constructor of the FilterEntry class'''
self.matching = {}
self.entry = entry
@ -35,15 +35,20 @@ class FilterEntry(object):
self.options = options
self.byrsspatterns = byrsspatterns
self.rssobject = rssobject
self.feedname = feedname
self.main()
def main(self):
'''Main of the FilterEntry class'''
authorized_elements = ['feedname', ]
authorized_elements.extend(self.entry.keys())
for i in self.elements:
if i not in self.entry:
if i not in authorized_elements:
sys.exit('The element {} is not available in the RSS feed. The available ones are: {}'.format(i, [j for j in self.entry]))
# for the case if no pattern at all is defined
if not self.options['patterns'] and not self.byrsspatterns and not self.rssobject:
if i == 'feedname':
self.matching[i] = self.feedname
elif not self.options['patterns'] and not self.byrsspatterns and not self.rssobject:
self.matching[i] = self.entry[i]
# global filter only
elif self.options['patterns'] and not self.byrsspatterns and not self.rssobject:
@ -67,7 +72,7 @@ class FilterEntry(object):
if not self.options['patternscasesensitive']['{}_case_sensitive'.format(patternlist)]:
# not case sensitive, so we compare the lower case
for pattern in self.options['patterns'][patternlist]:
finalpattern = pattern.lower()
finalpattern = pattern.lower()
finaltitle = self.entry[patternlist.split('_')[0]].lower()
if finalpattern in finaltitle:
self.matching[i] = self.entry[i]

View file

@ -173,7 +173,7 @@ class Main(object):
tmpelement = i.strip('{}')
elements.append(tmpelement)
# match elements of the tweet format string with available element in the RSS feed
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'])
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname'])
entrytosend = fe.finalentry
if entrytosend:
tweetwithnotag = tweetformat.format(**entrytosend)
@ -186,7 +186,7 @@ class Main(object):
finaltweet = addtag.finaltweet
else:
finaltweet = dedup.finaltweet
if clioptions.dryrun:
if entrytosend:
logging.warning('Would toot with visibility "{visibility}": {toot}'.format(
@ -219,7 +219,7 @@ class Main(object):
pluginmodulename = 'feed2toot.plugins.{pluginmodule}'.format(pluginmodule=pluginclassname.lower())
try:
pluginmodule = importlib.import_module(pluginmodulename)
pluginclass = getattr(pluginmodule, pluginclassname)
pluginclass = getattr(pluginmodule, pluginclassname)
pluginclass(plugins[plugin], finaltweet)
except ImportError as err:
print(err)