mirror of https://gitlab.com/chaica/feed2toot
Address issue #78
This commit is contained in:
parent
89518b2478
commit
3b46ce4f4c
|
@ -16,16 +16,18 @@
|
||||||
|
|
||||||
"""Checks an RSS feed and posts new entries to Mastodon."""
|
"""Checks an RSS feed and posts new entries to Mastodon."""
|
||||||
|
|
||||||
# standard libraires imports
|
# standard libraries imports
|
||||||
import codecs
|
import codecs
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import traceback
|
||||||
|
|
||||||
# external liraries imports
|
# external libraries imports
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from mastodon.errors import MastodonAPIError
|
||||||
|
|
||||||
# app libraries imports
|
# app libraries imports
|
||||||
from feed2toot.addtags import AddTags
|
from feed2toot.addtags import AddTags
|
||||||
|
@ -74,57 +76,61 @@ class Main:
|
||||||
# iterating over the different configuration files
|
# iterating over the different configuration files
|
||||||
cfgp = ConfParse(clioptions)
|
cfgp = ConfParse(clioptions)
|
||||||
confs = cfgp.confvalues
|
confs = cfgp.confvalues
|
||||||
for conf in confs:
|
try:
|
||||||
options = conf[0]
|
for conf in confs:
|
||||||
config = conf[1]
|
options = conf[0]
|
||||||
tweetformat = conf[2]
|
config = conf[1]
|
||||||
feeds = conf[3]
|
tweetformat = conf[2]
|
||||||
plugins = conf[4]
|
feeds = conf[3]
|
||||||
# check the logfile and logtimeout
|
plugins = conf[4]
|
||||||
lockfile = LockFile(options['lockfile'], options['locktimeout'])
|
# check the logfile and logtimeout
|
||||||
# create link to the persistent list
|
lockfile = LockFile(options['lockfile'], options['locktimeout'])
|
||||||
cache = FeedCache(options)
|
# create link to the persistent list
|
||||||
severalwordshashtags = extract_hashtags_from_list(options)
|
cache = FeedCache(options)
|
||||||
# reverse feed entries because most recent one should be sent as the last one in Mastodon
|
severalwordshashtags = extract_hashtags_from_list(options)
|
||||||
for feed in feeds:
|
# reverse feed entries because most recent one should be sent as the last one in Mastodon
|
||||||
# store the patterns by rss
|
for feed in feeds:
|
||||||
if 'patterns' in feed:
|
# store the patterns by rss
|
||||||
patterns = feed['patterns']
|
if 'patterns' in feed:
|
||||||
entries = feed['feed']['entries'][0:clioptions.limit]
|
patterns = feed['patterns']
|
||||||
entries.reverse()
|
entries = feed['feed']['entries'][0:clioptions.limit]
|
||||||
# --rss-sections option: print rss sections and exit
|
entries.reverse()
|
||||||
if clioptions.rsssections:
|
# --rss-sections option: print rss sections and exit
|
||||||
if entries:
|
if clioptions.rsssections:
|
||||||
print('The following sections are available in this RSS feed: {}'.format([j for j in entries[0]]))
|
if entries:
|
||||||
else:
|
print('The following sections are available in this RSS feed: {}'.format([j for j in entries[0]]))
|
||||||
print('Could not parse the section of the rss feed')
|
|
||||||
# release the lock file
|
|
||||||
lockfile.release()
|
|
||||||
sys.exit(0)
|
|
||||||
# sort entries and check if they were not previously sent
|
|
||||||
totweet = sort_entries(clioptions.all, cache, entries)
|
|
||||||
for entry in totweet:
|
|
||||||
# populate rss with new entry to send
|
|
||||||
rss = populate_rss(entry)
|
|
||||||
rss = build_hashtags(entry, rss, options, severalwordshashtags)
|
|
||||||
# parse tweetfomat to elements
|
|
||||||
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]
|
|
||||||
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname'])
|
|
||||||
entrytosend = fe.finalentry
|
|
||||||
if entrytosend:
|
|
||||||
finaltweet = build_message(entrytosend, tweetformat, rss, options['tootmaxlen'], options['notagsintoot'])
|
|
||||||
if clioptions.dryrun:
|
|
||||||
send_message_dry_run(config, entrytosend, finaltweet)
|
|
||||||
else:
|
else:
|
||||||
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss)
|
print('Could not parse the section of the rss feed')
|
||||||
# plugins
|
# release the lock file
|
||||||
if plugins and entrytosend:
|
lockfile.release()
|
||||||
activate_plugins(plugins, finaltweet)
|
sys.exit(0)
|
||||||
# do not forget to close cache (shelf object)
|
# sort entries and check if they were not previously sent
|
||||||
cache.close()
|
totweet = sort_entries(clioptions.all, cache, entries)
|
||||||
# release the lock file
|
for entry in totweet:
|
||||||
lockfile.release()
|
# populate rss with new entry to send
|
||||||
|
rss = populate_rss(entry)
|
||||||
|
rss = build_hashtags(entry, rss, options, severalwordshashtags)
|
||||||
|
# parse tweetfomat to elements
|
||||||
|
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]
|
||||||
|
fe = FilterEntry(elements, entry, options, feed['patterns'], feed['rssobject'], feed['feedname'])
|
||||||
|
entrytosend = fe.finalentry
|
||||||
|
if entrytosend:
|
||||||
|
finaltweet = build_message(entrytosend, tweetformat, rss, options['tootmaxlen'], options['notagsintoot'])
|
||||||
|
if clioptions.dryrun:
|
||||||
|
send_message_dry_run(config, entrytosend, finaltweet)
|
||||||
|
else:
|
||||||
|
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss)
|
||||||
|
# plugins
|
||||||
|
if plugins and entrytosend:
|
||||||
|
activate_plugins(plugins, finaltweet)
|
||||||
|
except MastodonAPIError:
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# do not forget to close cache (shelf object)
|
||||||
|
cache.close()
|
||||||
|
# release the lock file
|
||||||
|
lockfile.release()
|
||||||
|
|
Loading…
Reference in New Issue