Remove old hashtags when new ones are created

main
Bob Mottram 2020-05-31 17:31:33 +01:00
parent 85bb69ec66
commit 09d63088f0
2 changed files with 34 additions and 14 deletions

View File

@ -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,

View File

@ -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()