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

This commit is contained in:
Alexis Métaireau 2017-07-13 20:19:04 +02:00
parent 734d9450fb
commit cdf99e3f0b
4 changed files with 39 additions and 18 deletions

View File

@ -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 <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
@ -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.')

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:

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)