From 0c8d3ab698ef37f9ae6524163334a08249d21161 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 18 May 2020 10:42:02 +0100 Subject: [PATCH] Remove entries from blog replies files if they have expired --- blog.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/blog.py b/blog.py index 458076ce..aa6c0764 100644 --- a/blog.py +++ b/blog.py @@ -53,15 +53,31 @@ def noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {}, return 1 return 0 + removals = [] replies = 0 + lines = [] with open(postFilename, "r") as f: lines = f.readlines() for replyPostId in lines: replyPostId = replyPostId.replace('\n', '').replace('.json', '') - replyPostId = replyPostId.replace('.replies', '') - replies += 1 + noOfBlogReplies(baseDir, httpPrefix, translate, - nickname, domain, domainFull, - replyPostId, depth+1) + if locatePost(baseDir, nickname, domain, replyPostId): + replyPostId = replyPostId.replace('.replies', '') + replies += 1 + noOfBlogReplies(baseDir, httpPrefix, translate, + nickname, domain, domainFull, + replyPostId, depth+1) + else: + # remove post which no longer exists + removals.append(replyPostId) + + # remove posts from .replies file if they don't exist + if lines and removals: + print('Rewriting ' + postFilename + ' to remove ' + + str(len(removals)) + ' entries') + with open(postFilename, "w") as f: + for replyPostId in lines: + if replyPostId not in removals: + f.write(replyPostId + '\n') + return replies