monolith/utils/dedup.py

27 lines
1.0 KiB
Python
Raw Normal View History

from datetime import datetime
from csiphash import siphash24
from copy import deepcopy
from json import dumps
import main
from utils.logging.debug import debug
def dedup(numName, b):
if b["type"] in ["quit", "nick"]:
return False # QUITs and NICKs can't be duplicated, they are global and duplications are likely
c = deepcopy(b)
if "time" in c.keys():
del c["time"]
c["approxtime"] = str(datetime.utcnow().timestamp())[:main.config["Tweaks"]["DedupPrecision"]]
castHash = siphash24(main.hashKey, dumps(c, sort_keys=True).encode("utf-8"))
del c["approxtime"]
isDuplicate= any(castHash in main.lastEvents[x] for x in main.lastEvents.keys() if not x == numName)
if isDuplicate:
debug("Duplicate: %s" % (c))
return True
if numName in main.lastEvents.keys():
main.lastEvents[numName].insert(0, castHash)
main.lastEvents[numName] = main.lastEvents[numName][0:main.config["Tweaks"]["MaxHash"]]
else:
main.lastEvents[numName] = [castHash]
return False