mirror of https://gitlab.com/chaica/feed2toot
Merge branch 'master' into 'master'
Draft: Ensure cache is closed/saved on MastodonAPIError See merge request chaica/feed2toot!22
This commit is contained in:
commit
9beb5d65bf
|
@ -16,16 +16,18 @@
|
|||
|
||||
"""Checks an RSS feed and posts new entries to Mastodon."""
|
||||
|
||||
# standard libraires imports
|
||||
# standard libraries imports
|
||||
import codecs
|
||||
import importlib
|
||||
import logging
|
||||
import logging.handlers
|
||||
import sys
|
||||
import re
|
||||
import traceback
|
||||
|
||||
# external liraries imports
|
||||
# external libraries imports
|
||||
from bs4 import BeautifulSoup
|
||||
from mastodon.errors import MastodonAPIError
|
||||
|
||||
# app libraries imports
|
||||
from feed2toot.addtags import AddTags
|
||||
|
@ -74,57 +76,61 @@ class Main:
|
|||
# iterating over the different configuration files
|
||||
cfgp = ConfParse(clioptions)
|
||||
confs = cfgp.confvalues
|
||||
for conf in confs:
|
||||
options = conf[0]
|
||||
config = conf[1]
|
||||
tweetformat = conf[2]
|
||||
feeds = conf[3]
|
||||
plugins = conf[4]
|
||||
# check the logfile and logtimeout
|
||||
lockfile = LockFile(options['lockfile'], options['locktimeout'])
|
||||
# create link to the persistent list
|
||||
cache = FeedCache(options)
|
||||
severalwordshashtags = extract_hashtags_from_list(options)
|
||||
# reverse feed entries because most recent one should be sent as the last one in Mastodon
|
||||
for feed in feeds:
|
||||
# store the patterns by rss
|
||||
if 'patterns' in feed:
|
||||
patterns = feed['patterns']
|
||||
entries = feed['feed']['entries'][0:clioptions.limit]
|
||||
entries.reverse()
|
||||
# --rss-sections option: print rss sections and exit
|
||||
if clioptions.rsssections:
|
||||
if entries:
|
||||
print('The following sections are available in this RSS feed: {}'.format([j for j in entries[0]]))
|
||||
else:
|
||||
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)
|
||||
try:
|
||||
for conf in confs:
|
||||
options = conf[0]
|
||||
config = conf[1]
|
||||
tweetformat = conf[2]
|
||||
feeds = conf[3]
|
||||
plugins = conf[4]
|
||||
# check the logfile and logtimeout
|
||||
lockfile = LockFile(options['lockfile'], options['locktimeout'])
|
||||
# create link to the persistent list
|
||||
cache = FeedCache(options)
|
||||
severalwordshashtags = extract_hashtags_from_list(options)
|
||||
# reverse feed entries because most recent one should be sent as the last one in Mastodon
|
||||
for feed in feeds:
|
||||
# store the patterns by rss
|
||||
if 'patterns' in feed:
|
||||
patterns = feed['patterns']
|
||||
entries = feed['feed']['entries'][0:clioptions.limit]
|
||||
entries.reverse()
|
||||
# --rss-sections option: print rss sections and exit
|
||||
if clioptions.rsssections:
|
||||
if entries:
|
||||
print('The following sections are available in this RSS feed: {}'.format([j for j in entries[0]]))
|
||||
else:
|
||||
send_message(config, clioptions, options, entrytosend, finaltweet, cache, rss)
|
||||
# plugins
|
||||
if plugins and entrytosend:
|
||||
activate_plugins(plugins, finaltweet)
|
||||
# do not forget to close cache (shelf object)
|
||||
cache.close()
|
||||
# release the lock file
|
||||
lockfile.release()
|
||||
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:
|
||||
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