From a30c1c1044ab6a22769b41c84cc842e0be860dce Mon Sep 17 00:00:00 2001 From: Bob Mottram <bob@freedombone.net> Date: Sat, 21 Nov 2020 13:45:37 +0000 Subject: [PATCH] More efficient reading of tag index --- webapp_hashtagswarm.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/webapp_hashtagswarm.py b/webapp_hashtagswarm.py index 552f8045..e26a8bd3 100644 --- a/webapp_hashtagswarm.py +++ b/webapp_hashtagswarm.py @@ -76,6 +76,7 @@ def htmlHashTagSwarm(baseDir: str, actor: str, translate: {}) -> str: currTime = datetime.utcnow() daysSinceEpoch = (currTime - datetime(1970, 1, 1)).days daysSinceEpochStr = str(daysSinceEpoch) + ' ' + daysSinceEpochStr2 = str(daysSinceEpoch - 1) + ' ' recently = daysSinceEpoch - 1 tagSwarm = [] domainHistogram = {} @@ -99,8 +100,12 @@ def htmlHashTagSwarm(baseDir: str, actor: str, translate: {}) -> str: hashTagName = f.split('.')[0] if isBlockedHashtag(baseDir, hashTagName): continue - if daysSinceEpochStr not in open(tagsFilename).read(): - continue + with open(tagsFilename, 'r') as fp: + # only read one line, which saves time and memory + lastTag = fp.readline() + if not lastTag.startswith(daysSinceEpochStr): + if not lastTag.startswith(daysSinceEpochStr2): + continue with open(tagsFilename, 'r') as tagsFile: while True: line = tagsFile.readline()