From b147d465359fcca8621fd79798656c8809fea1a0 Mon Sep 17 00:00:00 2001 From: knoflook Date: Mon, 21 Feb 2022 11:19:12 +0100 Subject: [PATCH] feed: compile regex once --- lumbunglib/feed.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lumbunglib/feed.py b/lumbunglib/feed.py index 983a90a..10ba290 100644 --- a/lumbunglib/feed.py +++ b/lumbunglib/feed.py @@ -12,7 +12,8 @@ import jinja2 import requests from bs4 import BeautifulSoup from slugify import slugify -from re import sub +from re import compile as re_compile +yamlre = re_compile('"') def write_etag(feed_name, feed_data): @@ -48,7 +49,7 @@ def get_etag(feed_name): def create_frontmatter(entry): """ - parse RSS metadata and return as frontmatter + parse RSS metadata and return as frontmatter """ if 'published' in entry: published = entry.published_parsed @@ -61,7 +62,7 @@ def create_frontmatter(entry): author = entry.author else: author = '' - + if 'authors' in entry: authors = [] for a in entry.authors: @@ -119,12 +120,12 @@ def sanitize_yaml (frontmatter): #some fields are lists l = [] for i in v: - i = sub('"', '\\"', i) + i = yamlre.sub('\\"', i) l.append(i) frontmatter[k] = l else: - v = sub('"', '\\"', v) + v = yamlre.sub('\\"', v) frontmatter[k] = v return frontmatter @@ -348,7 +349,7 @@ def main(): entry["feed_name"] = feed_name post_name = slugify(entry.title) - + # pixelfed returns the whole post text as the post name. max # filename length is 255 on many systems. here we're shortening # the name and adding a hash to it to avoid a conflict in a @@ -356,7 +357,7 @@ def main(): if len(post_name) > 150: post_hash = md5(bytes(post_name, "utf-8")) post_name = post_name[:150] + "-" + post_hash.hexdigest() - + if opds_feed: entry['opds'] = True #format: Beyond-Debiasing-Report_Online-75535a4886e3 @@ -376,7 +377,7 @@ def main(): if opds_feed: create_opds_post(post_dir, entry) else: - create_post(post_dir, entry) + create_post(post_dir, entry) existing_posts.remove( post_name ) # create list of posts which have not been returned by the feed -- 2.45.2