Merge branch 'toot_language' into 'master'

Post messages in the language of the original feed

Closes #80

See merge request chaica/feed2toot!23
This commit is contained in:
Gábor Sebestyén 2023-07-12 08:19:31 +00:00
commit b24a635b9f
3 changed files with 19 additions and 8 deletions

View File

@ -43,6 +43,7 @@ from feed2toot.plugins import activate_plugins
from feed2toot.rss import populate_rss
from feed2toot.sortentries import sort_entries
class Main:
'''Main class of Feed2toot'''
@ -103,6 +104,15 @@ class Main:
sys.exit(0)
# sort entries and check if they were not previously sent
totweet = sort_entries(clioptions.all, cache, entries)
# get language of the feed
# language code should conform to ISO-639-1
if 'language' in feed['feed']['feed']:
# Only the first two letters count ...
language = feed['feed']['feed']['language'][:2]
else:
language = 'en'
print("Language of feeds: {}".format(language))
for entry in totweet:
# populate rss with new entry to send
rss = populate_rss(entry)
@ -111,8 +121,8 @@ class Main:
elements = re.findall(r"\{(.*?)\}",tweetformat)
# strip : from elements to allow string formating, eg. {title:.20}
for i,s in enumerate(elements):
if s.find(':'):
elements[i] = s.split(':')[0]
if s.find(':'):
elements[i] = s.split(':')[0]
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname'])
entrytosend = fe.finalentry
if entrytosend:
@ -120,7 +130,7 @@ class Main:
if clioptions.dryrun:
send_message_dry_run(config, entrytosend, finaltweet)
else:
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss)
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss, language)
# plugins
if plugins and entrytosend:
activate_plugins(plugins, finaltweet)

View File

@ -64,7 +64,7 @@ def send_message_dry_run(config, entrytosend, finaltweet):
else:
logging.debug('This rss entry did not meet pattern criteria. Should have not been sent')
def send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss):
def send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss, language):
'''send message'''
storeit = True
if entrytosend and not clioptions.populate:
@ -73,7 +73,7 @@ def send_message(config, clioptions, options, entrytosend, finaltweet, cache, rs
visibility=config.get(
'mastodon', 'toot_visibility',
fallback='public')))
twp = TootPost(config, options, finaltweet)
twp = TootPost(config, options, finaltweet, language)
storeit = twp.storeit()
else:
logging.debug('populating RSS entry {}'.format(rss['id']))

View File

@ -21,12 +21,13 @@ from mastodon import Mastodon
class TootPost:
'''TootPost class'''
def __init__(self, config, options, toot):
def __init__(self, config, options, toot, lang):
'''Constructore of the TootPost class'''
self.config = config
self.options = options
self.store = True
self.toot = toot
self.lang = lang
self.main()
def main(self):
@ -39,9 +40,9 @@ class TootPost:
toot_visibility = self.config.get('mastodon', 'toot_visibility', fallback='public')
if 'custom' in self.options['media']:
mediaid = mastodon.media_post(self.config['media']['custom'])
mastodon.status_post(self.toot, media_ids=[mediaid], visibility=toot_visibility)
mastodon.status_post(self.toot, media_ids=[mediaid], visibility=toot_visibility, language=self.lang)
else:
mastodon.status_post(self.toot, visibility=toot_visibility)
mastodon.status_post(self.toot, visibility=toot_visibility, language=self.lang)
def storeit(self):
'''Indicate if the tweet should be stored or not'''