From 09d63088f06da6173b7a469c333d807b10e25e5a Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 31 May 2020 17:31:33 +0100 Subject: [PATCH] Remove old hashtags when new ones are created --- inbox.py | 2 ++ webinterface.py | 46 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/inbox.py b/inbox.py index fa269206..51130f97 100644 --- a/inbox.py +++ b/inbox.py @@ -56,6 +56,7 @@ from posts import sendSignedJson from posts import sendToFollowersThread from webinterface import individualPostAsHtml from webinterface import getIconsDir +from webinterface import removeOldHashtags from question import questionUpdateVotes from media import replaceYouTube from git import isGitPatch @@ -108,6 +109,7 @@ def storeHashTags(baseDir: str, nickname: str, postJsonObject: {}) -> None: except Exception as e: print('WARN: Failed to write entry to tags file ' + tagsFilename + ' ' + str(e)) + removeOldHashtags(baseDir, 3) def inboxStorePostToHtmlCache(recentPostsCache: {}, maxRecentPosts: int, diff --git a/webinterface.py b/webinterface.py index c3de636b..4e66d5ac 100644 --- a/webinterface.py +++ b/webinterface.py @@ -5725,15 +5725,15 @@ def htmlCalendar(translate: {}, return calendarStr -def htmlHashTagSwarm(baseDir: str, actor: str) -> str: - """Returns a tag swarm of today's hashtags +def removeOldHashtags(baseDir: str, maxMonths: int) -> str: + """Remove old hashtags """ - currTime = datetime.utcnow() - daysSinceEpoch = (currTime - datetime(1970, 1, 1)).days - maxDaysSinceEpoch = (currTime - datetime(1970, 3, 1)).days - daysSinceEpochStr = str(daysSinceEpoch) + ' ' - tagSwarm = [] + if maxMonths > 11: + maxMonths = 11 + maxDaysSinceEpoch = \ + (datetime.utcnow() - datetime(1970, 1 + maxMonths, 1)).days removeHashtags = [] + for subdir, dirs, files in os.walk(baseDir + '/tags'): for f in files: tagsFilename = os.path.join(baseDir + '/tags', f) @@ -5743,10 +5743,35 @@ def htmlHashTagSwarm(baseDir: str, actor: str) -> str: modTimesinceEpoc = os.path.getmtime(tagsFilename) lastModifiedDate = datetime.fromtimestamp(modTimesinceEpoc) fileDaysSinceEpoch = (lastModifiedDate - datetime(1970, 1, 1)).days + # check of the file is too old if fileDaysSinceEpoch < maxDaysSinceEpoch: removeHashtags.append(tagsFilename) + + for removeFilename in removeHashtags: + try: + os.remove(removeFilename) + except BaseException: + pass + + +def htmlHashTagSwarm(baseDir: str, actor: str) -> str: + """Returns a tag swarm of today's hashtags + """ + currTime = datetime.utcnow() + daysSinceEpoch = (currTime - datetime(1970, 1, 1)).days + daysSinceEpochStr = str(daysSinceEpoch) + ' ' + tagSwarm = [] + + for subdir, dirs, files in os.walk(baseDir + '/tags'): + for f in files: + tagsFilename = os.path.join(baseDir + '/tags', f) + if not os.path.isfile(tagsFilename): continue + # get last modified datetime + modTimesinceEpoc = os.path.getmtime(tagsFilename) + lastModifiedDate = datetime.fromtimestamp(modTimesinceEpoc) + fileDaysSinceEpoch = (lastModifiedDate - datetime(1970, 1, 1)).days # check if the file was last modified today if fileDaysSinceEpoch != daysSinceEpoch: continue @@ -5791,13 +5816,6 @@ def htmlHashTagSwarm(baseDir: str, actor: str) -> str: if lineCtr >= maxLineCtr: break - # remove old hashtags - for removeFilename in removeHashtags: - try: - os.remove(removeFilename) - except BaseException: - pass - if not tagSwarm: return '' tagSwarm.sort()