From d17c0d309eaad4f1269ba235ce95489e473457e6 Mon Sep 17 00:00:00 2001 From: Ada Isopods Date: Wed, 11 Dec 2024 22:47:34 -0700 Subject: Fix emoji find+replace Emojis will no longer be added where their names are part of a larger word, eg. category -> :cat:egory where :cat: is a valid emoji If posts_log.json is invalid program will now overwrite it instead of exploding and dying and getting it all over you about it --- generate.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'generate.py') diff --git a/generate.py b/generate.py index 9e94910..041e436 100644 --- a/generate.py +++ b/generate.py @@ -3,6 +3,7 @@ import sys import requests import json import os +import re import secrets__ import config__ @@ -16,7 +17,7 @@ log_path = dir_path + "/posts_log.json" try: with open(log_path, "r") as infile: posts_log = json.loads(infile.read()) -except FileNotFoundError: +except (FileNotFoundError, json.decoder.JSONDecodeError): with open(log_path, "w") as outfile: outfile.write('[""]') posts_log = [] @@ -44,10 +45,9 @@ posts_log.append(text) with open(log_path, "w") as outfile: outfile.write(json.dumps(posts_log[-config__.log_length:])) -if(config__.emojis == True): - emojis = json.load(open(dir_path + "/emojis.json")) - for emoji in emojis: - text = text.replace(emoji, ":"+emoji+":") +emojis = json.load(open(dir_path + "/emojis.json")) +for emoji in emojis: + text = re.sub(r'\b'+emoji+r'\b', ":"+emoji+":", text) text = text.replace('@','@​').replace('#','#​') print(text) -- cgit v1.2.3