diff --git a/config.toml b/config.toml index da4aac4..549c8be 100644 --- a/config.toml +++ b/config.toml @@ -30,3 +30,7 @@ font = "./res/curie.bdf" text_filters = [ "lowercase" ] text_offset = [45, 4] text_fill = [255, 255, 255] # your template image must have alpha channel to use alpha + +[[image]] +output = "out/iloveseason.gif" +command = [ "python", "./res/iloveseason.py" ] diff --git a/readme.md b/readme.md index 924501e..74f0911 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ such as nginx or caddy. 1. install requirements: `pip install -r requirements.txt` 2. run: `python main/src.py [-c config-file] [-L]`. -## docker +### docker a [dockerfile](./Dockerfile) and [compose file](./compose.yaml) have been provided. keep in mind the commands will run in the docker container, too. @@ -37,6 +37,36 @@ RUN apt-get update && apt-get install blahblahblah CMD [ "python", "-u", "./src/main.py", "-c", "/config.toml" ] ``` +## writing commands + +### basic + +basic commands return just text. +the text can end in 0 or 1 newlines but not multiple, +else it will be interpreted as a dynamic config command. + +### dynamic config commands + +any config can be dynamically set by preceeding the text to be printed +with TOML config: + +``` +template = "pat/to/templat.gif" +font = "fonts/custom_font.bdf" +text_offset = [20, 4] +hello world +``` + +if you do not want to print any text, +simply leave an extra blank/empty line +(the text ends in two newlines) +or a space character: + +``` +template = "pat/to/templat.gif" + +``` + ## config the provided [example config file](./config.toml) lists all the options that can be used. diff --git a/res/autumn.gif b/res/autumn.gif new file mode 100644 index 0000000..73cda6e Binary files /dev/null and b/res/autumn.gif differ diff --git a/res/iloveseason.py b/res/iloveseason.py new file mode 100755 index 0000000..6df497b --- /dev/null +++ b/res/iloveseason.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# +# Akbar Rahman +# + +import sys +from datetime import datetime as dt + + +def get_args(): + """ Get command line arguments """ + + import argparse + parser = argparse.ArgumentParser() + return parser.parse_args() + + +def main(args): + """ Entry point for script """ + now = dt.now() + + template = "" + if now.month in [3, 4, 5]: + template = './res/spring.gif' + if now.month in [6, 7, 8]: + template = './res/summer.gif' + if now.month in [9, 10, 11]: + template = './res/autumn.gif' + if now.month in [12, 1, 2]: + template = './res/winter.gif' + + print(f'template = "{template}"') + print() + + return 0 + + +if __name__ == '__main__': + try: + sys.exit(main(get_args())) + except KeyboardInterrupt: + sys.exit(0) diff --git a/res/spring.gif b/res/spring.gif new file mode 100644 index 0000000..6c1b364 Binary files /dev/null and b/res/spring.gif differ diff --git a/res/summer.gif b/res/summer.gif new file mode 100644 index 0000000..fd32443 Binary files /dev/null and b/res/summer.gif differ diff --git a/res/winter.gif b/res/winter.gif new file mode 100644 index 0000000..ca1d6ed Binary files /dev/null and b/res/winter.gif differ diff --git a/src/main.py b/src/main.py index d734c3b..db07a6b 100755 --- a/src/main.py +++ b/src/main.py @@ -3,6 +3,7 @@ # Akbar Rahman # +import json import sys import subprocess import time @@ -22,31 +23,45 @@ def get_args(): def generate(config): - if config.get('template'): - img = Image.open(config['template']) - else: - img = Image.new(mode="RGBA", size = config['size']) - - draw = ImageDraw.Draw(img) - with open(config['font'], "rb") as fp: - font = BdfFontFile.BdfFontFile(fp).to_imagefont() - process = subprocess.run(config['command'], stdout=subprocess.PIPE, shell=False) if process.returncode != config.get('return_code', 0): # process did not run succesfully. # do not risk displaying that to user return + cmd_output = process.stdout.decode('utf-8').split('\n') + print(f"{cmd_output=}") + dynamic_config = {} + if len(cmd_output) > 2: + dynamic_config = tomllib.loads('\n'.join(cmd_output[:-2])) + text = cmd_output[-2] + else: + text = cmd_output[0] - text = process.stdout.decode('utf-8').split('\n')[0] - for filter in config.get('text_filters', []): - if filter == "lowercase": - text = text.lower() - elif filter == "uppercase": - text = text.upper() + print(f"{dynamic_config=}") + for key, val in dynamic_config.items(): + config[key] = val - fill = config.get('text_fill') - fill = tuple(fill) if fill else None - draw.text(config.get('text_offset', [0, 0]), text, font=font, fill=fill) + if config.get('template'): + img = Image.open(config['template']) + else: + img = Image.new(mode="RGBA", size = config['size']) + + print(f"{text=}") + + if text: + draw = ImageDraw.Draw(img) + with open(config['font'], "rb") as fp: + font = BdfFontFile.BdfFontFile(fp).to_imagefont() + + for filter in config.get('text_filters', []): + if filter == "lowercase": + text = text.lower() + elif filter == "uppercase": + text = text.upper() + + fill = config.get('text_fill') + fill = tuple(fill) if fill else None + draw.text(config.get('text_offset', [0, 0]), text, font=font, fill=fill) img.save(config['output'], save_all=True, loop=0)