RSS digests are now optional
This commit is contained in:
parent
a3b83e8c57
commit
bd761d420e
1 changed files with 20 additions and 19 deletions
|
@ -29,12 +29,12 @@ class RssUpdates(HttpRequest):
|
||||||
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
|
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
|
||||||
|
|
||||||
def __init__(self, url, title=None, headers=None, params=None, max_entries=None,
|
def __init__(self, url, title=None, headers=None, params=None, max_entries=None,
|
||||||
mercury_api_key=None, digest_format='html', *args, **kwargs):
|
mercury_api_key=None, digest_format=None, *args, **kwargs):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.title = title
|
self.title = title
|
||||||
self.max_entries = max_entries
|
self.max_entries = max_entries
|
||||||
self.mercury_api_key = mercury_api_key # Mercury Reader API used to parse the content of the link
|
self.mercury_api_key = mercury_api_key # Mercury Reader API used to parse the content of the link
|
||||||
self.digest_format = digest_format.lower() if digest_format else 'html'
|
self.digest_format = digest_format.lower() if digest_format else None # Supported formats: html, pdf
|
||||||
|
|
||||||
os.makedirs(os.path.expanduser(os.path.dirname(self.dbfile)), exist_ok=True)
|
os.makedirs(os.path.expanduser(os.path.dirname(self.dbfile)), exist_ok=True)
|
||||||
|
|
||||||
|
@ -149,27 +149,28 @@ class RssUpdates(HttpRequest):
|
||||||
logging.info('Parsed {} new entries from the RSS feed {}'.format(
|
logging.info('Parsed {} new entries from the RSS feed {}'.format(
|
||||||
len(entries), self.title))
|
len(entries), self.title))
|
||||||
|
|
||||||
digest_filename = os.path.join(self.workdir, 'cache', '{}_{}.{}'.format(
|
if self.digest_format:
|
||||||
datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
|
digest_filename = os.path.join(self.workdir, 'cache', '{}_{}.{}'.format(
|
||||||
self.title, self.digest_format))
|
datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
|
||||||
|
self.title, self.digest_format))
|
||||||
|
|
||||||
os.makedirs(os.path.dirname(digest_filename), exist_ok=True)
|
os.makedirs(os.path.dirname(digest_filename), exist_ok=True)
|
||||||
|
|
||||||
if self.digest_format == 'html':
|
if self.digest_format == 'html':
|
||||||
with open(digest_filename, 'w', encoding='utf-8') as f:
|
with open(digest_filename, 'w', encoding='utf-8') as f:
|
||||||
f.write(digest)
|
f.write(digest)
|
||||||
elif self.digest_format == 'pdf':
|
elif self.digest_format == 'pdf':
|
||||||
import weasyprint
|
import weasyprint
|
||||||
weasyprint.HTML(string=digest).write_pdf(digest_filename)
|
weasyprint.HTML(string=digest).write_pdf(digest_filename)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('Unsupported format: {}. Supported formats: ' +
|
raise RuntimeError('Unsupported format: {}. Supported formats: ' +
|
||||||
'html or pdf'.format(self.digest_format))
|
'html or pdf'.format(self.digest_format))
|
||||||
|
|
||||||
digest_entry = FeedDigest(source_id=source_record.id,
|
digest_entry = FeedDigest(source_id=source_record.id,
|
||||||
format=self.digest_format,
|
format=self.digest_format,
|
||||||
filename=digest_filename)
|
filename=digest_filename)
|
||||||
|
|
||||||
session.add(digest_entry)
|
session.add(digest_entry)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue