tool to help making templates
This commit is contained in:
+2
-1
@@ -1,4 +1,5 @@
|
||||
.env
|
||||
.idea
|
||||
__pycache__
|
||||
error_*.txt
|
||||
error_*.txt
|
||||
tmp
|
||||
+16
-9
@@ -12,10 +12,16 @@ ALIASES = {}
|
||||
logger = logging.getLogger("meme_db")
|
||||
|
||||
|
||||
def load_memes():
|
||||
def load_memes(purge=False):
|
||||
"""
|
||||
TODO
|
||||
|
||||
:param (bool) purge:
|
||||
"""
|
||||
global DATA, ALIASES
|
||||
if purge:
|
||||
DATA = {}
|
||||
ALIASES = {}
|
||||
try:
|
||||
with open(DATA_FILE) as f:
|
||||
content = "".join(f.readlines())
|
||||
@@ -85,11 +91,14 @@ def load_item(i, item):
|
||||
logger.warning(f"Item '{item_id}'({i}): no texts loaded")
|
||||
else:
|
||||
DATA[item_id] = meme
|
||||
for alias in meme.aliases:
|
||||
if alias in ALIASES:
|
||||
logger.warning(f"Item '{item_id}'({i}): alias '{alias}' already registered by '{ALIASES[alias]}'")
|
||||
else:
|
||||
ALIASES[alias] = item_id
|
||||
if not meme.abstract:
|
||||
ALIASES[item_id] = item_id
|
||||
for alias in meme.aliases:
|
||||
if alias in ALIASES:
|
||||
logger.warning(
|
||||
f"Item '{item_id}'({i}): alias '{alias}' already registered by '{ALIASES[alias]}'")
|
||||
else:
|
||||
ALIASES[alias] = item_id
|
||||
logger.info(f"Loaded meme '{item_id}' with {len(meme.texts)} texts")
|
||||
except KeyError as e:
|
||||
logger.warning(f"Item '{item_id}'({i}): key {e} not found")
|
||||
@@ -140,9 +149,7 @@ def get_meme(name):
|
||||
:rtype: Meme|None
|
||||
:return:
|
||||
"""
|
||||
if name in DATA and not DATA[name].abstract:
|
||||
return DATA[name].clone()
|
||||
elif name in ALIASES:
|
||||
if name in ALIASES:
|
||||
return DATA[ALIASES[name]].clone()
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import stat
|
||||
import time
|
||||
import datetime
|
||||
import logging
|
||||
from os import path
|
||||
from meme_otron import img_factory as imgf
|
||||
from meme_otron import meme_db
|
||||
from meme_otron import utils
|
||||
|
||||
logging.basicConfig(format="%(message)s", level=logging.WARNING)
|
||||
|
||||
imgf.load_fonts()
|
||||
|
||||
db_file = utils.relative_path(__file__, "..", meme_db.DATA_FILE)
|
||||
templates_dir = utils.relative_path(__file__)
|
||||
dst_dir = utils.relative_path(__file__, "tmp")
|
||||
|
||||
if not path.exists(dst_dir):
|
||||
os.mkdir(dst_dir)
|
||||
|
||||
last = None
|
||||
|
||||
while True:
|
||||
while os.stat(db_file)[stat.ST_MTIME] == last:
|
||||
time.sleep(0.1)
|
||||
last = os.stat(db_file)[stat.ST_MTIME]
|
||||
meme_db.load_memes(purge=True)
|
||||
count = 0
|
||||
for meme_id in meme_db.DATA:
|
||||
meme = meme_db.get_meme(meme_id)
|
||||
if meme is not None:
|
||||
img = imgf.make(meme.template, meme.texts, debug=True)
|
||||
if img is not None:
|
||||
img.save(path.join(dst_dir, meme.template))
|
||||
count += 1
|
||||
print(f"{datetime.datetime.now():%H:%M:%S} / {count} registered templates / {len(os.listdir(templates_dir))} files")
|
||||
Reference in New Issue
Block a user