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.rss import populate_rss
from feed2toot.sortentries import sort_entries from feed2toot.sortentries import sort_entries
class Main: class Main:
'''Main class of Feed2toot''' '''Main class of Feed2toot'''
@ -103,6 +104,15 @@ class Main:
sys.exit(0) sys.exit(0)
# sort entries and check if they were not previously sent # sort entries and check if they were not previously sent
totweet = sort_entries(clioptions.all, cache, entries) 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: for entry in totweet:
# populate rss with new entry to send # populate rss with new entry to send
rss = populate_rss(entry) rss = populate_rss(entry)
@ -120,7 +130,7 @@ class Main:
if clioptions.dryrun: if clioptions.dryrun:
send_message_dry_run(config, entrytosend, finaltweet) send_message_dry_run(config, entrytosend, finaltweet)
else: else:
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss) send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss, language)
# plugins # plugins
if plugins and entrytosend: if plugins and entrytosend:
activate_plugins(plugins, finaltweet) activate_plugins(plugins, finaltweet)

View File

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

View File

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