new conf parameter toot_max_len to truncate toot max length

This commit is contained in:
Carl Chenet 2019-12-30 22:11:19 +01:00
parent 9249e00152
commit 86ca0f14b6
4 changed files with 46 additions and 4 deletions

View file

@ -40,6 +40,7 @@ from feed2toot.confparsers.rss.toot import parsetoot
from feed2toot.confparsers.rss.uri import parseuri from feed2toot.confparsers.rss.uri import parseuri
from feed2toot.confparsers.rss.urilist import parseurilist from feed2toot.confparsers.rss.urilist import parseurilist
from feed2toot.confparsers.rss.addtags import parseaddtags from feed2toot.confparsers.rss.addtags import parseaddtags
from feed2toot.confparsers.rss.tootmaxlen import parsetootmaxlen
class ConfParse: class ConfParse:
'''ConfParse class''' '''ConfParse class'''
@ -67,6 +68,7 @@ class ConfParse:
# the rss section # the rss section
########################### ###########################
self.tweetformat = parsetoot(config) self.tweetformat = parsetoot(config)
options['tootmaxlen'] = parsetootmaxlen(config)
################################################# #################################################
# pattern and patter_case_sensitive format option # pattern and patter_case_sensitive format option
################################################# #################################################

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Copyright © 2015-2019 Carl Chenet <carl.chenet@ohmytux.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/
# Get value of the toot/tweet option of rss section
'''Get value of the toot/tweet option of the rss section'''
# standard library imports
import sys
import logging
def parsetootmaxlen(config):
'''Parse configuration value of the toot_max_len option of the rss section'''
section = 'rss'
tootmaxlen = 500
if config.has_section(section):
############################
# toot_max_len parameter
############################
confoption = 'toot_max_len'
if config.has_option(section, confoption):
try:
tootmaxlen = int(config.get(section, confoption))
except ValueError as err:
sys.exit('Error in configuration with the {confoption} parameter in [{section}]: {err}'.format(confoption=confoption, section=section, err=err))
return tootmaxlen

View file

@ -116,7 +116,7 @@ class Main:
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname']) fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname'])
entrytosend = fe.finalentry entrytosend = fe.finalentry
if entrytosend: if entrytosend:
finaltweet = build_message(entrytosend, tweetformat, rss) finaltweet = build_message(entrytosend, tweetformat, rss, options['tootmaxlen'])
if clioptions.dryrun: if clioptions.dryrun:
send_message_dry_run(config, entrytosend, finaltweet) send_message_dry_run(config, entrytosend, finaltweet)
else: else:

View file

@ -26,7 +26,7 @@ from feed2toot.addtags import AddTags
from feed2toot.removeduplicates import RemoveDuplicates from feed2toot.removeduplicates import RemoveDuplicates
from feed2toot.tootpost import TootPost from feed2toot.tootpost import TootPost
def build_message(entrytosend, tweetformat, rss): def build_message(entrytosend, tweetformat, rss, tootmaxlen):
'''populate the rss dict with the new entry''' '''populate the rss dict with the new entry'''
tweetwithnotag = tweetformat.format(**entrytosend) tweetwithnotag = tweetformat.format(**entrytosend)
# replace line breaks # replace line breaks
@ -40,10 +40,13 @@ def build_message(entrytosend, tweetformat, rss):
finaltweet = addtag.finaltweet finaltweet = addtag.finaltweet
else: else:
finaltweet = dedup.finaltweet finaltweet = dedup.finaltweet
# strip html tags # strip html tags
finaltweet = BeautifulSoup(finaltweet, 'html.parser').get_text() finaltweet = BeautifulSoup(finaltweet, 'html.parser').get_text()
return finaltweet # truncate toot to user-defined value
if len(finaltweet) > tootmaxlen:
return ''.join([finaltweet[0:-3], '...'])
else:
return finaltweet
def send_message_dry_run(config, entrytosend, finaltweet): def send_message_dry_run(config, entrytosend, finaltweet):
'''simulate sending message using dry run mode''' '''simulate sending message using dry run mode'''