From cdf99e3f0b4a2a957ed01305605e7f58a69eab57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 13 Jul 2017 20:19:04 +0200 Subject: [PATCH] Add the ability to use {feedname} in the tweet template --- docs/source/configure.rst | 11 ++++++++++- feed2toot/confparse.py | 27 +++++++++++++++++---------- feed2toot/filterentry.py | 13 +++++++++---- feed2toot/main.py | 6 +++--- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/docs/source/configure.rst b/docs/source/configure.rst index f354ce8..607feda 100644 --- a/docs/source/configure.rst +++ b/docs/source/configure.rst @@ -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. @@ -100,6 +100,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 + +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:: diff --git a/feed2toot/confparse.py b/feed2toot/confparse.py index 1611fb7..71ec749 100644 --- a/feed2toot/confparse.py +++ b/feed2toot/confparse.py @@ -23,6 +23,7 @@ import os import os.path import socket import sys +import re # 3rd party library imports import feedparser @@ -46,9 +47,9 @@ class ConfParse(object): if not config.read(os.path.expanduser(pathtoconfig)): sys.exit('Could not read config file') ########################### - # + # # the rss section - # + # ########################### section = 'rss' if config.has_section(section): @@ -101,6 +102,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)) @@ -124,7 +131,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.') @@ -153,9 +160,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: @@ -183,9 +190,9 @@ class ConfParse(object): else: options['cache_limit'] = 100 ########################### - # + # # the hashtag section - # + # ########################### section = 'hashtaglist' if not self.clioptions.hashtaglist: @@ -199,9 +206,9 @@ class ConfParse(object): else: options['hashtaglist'] = '' ########################### - # + # # the plugins section - # + # ########################### plugins = {} section = 'influxdb' @@ -228,7 +235,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''' diff --git a/feed2toot/filterentry.py b/feed2toot/filterentry.py index bf27ffe..3ede1c0 100644 --- a/feed2toot/filterentry.py +++ b/feed2toot/filterentry.py @@ -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] diff --git a/feed2toot/main.py b/feed2toot/main.py index 733bd49..3da91b2 100644 --- a/feed2toot/main.py +++ b/feed2toot/main.py @@ -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)