Something went wrong. Try again.
Source code for my personal quote bot project.
Something went wrong. Try again.
audquotes redis_random_push.py
1.7 kB · 46 lines
Python
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#!/usr/bin/env python3
import timeimport randomimport osimport loggingimport hashlibimport copy
from glob import iglobfrom typing import Optionalfrom pathlib import Path
import redisimport typer
def main(source_key, destination_key, rename_key: bool = False, password: str = "", host: str = "localhost", port: int = 16379, quotes_pattern: str = "quotes/**/*.txt", quotes_list_file: Path | None = None, log_level: str = "INFO") -> None: logging.getLogger().setLevel(os.getenv("LOGGING") or log_level)
new_quotes = tuple(iglob(quotes_pattern, recursive=True)) if not quotes_list_file else tuple(line for line in quotes_list_file.read_text().split("\n") if line) logging.info("Quotes to add: `%s`", new_quotes) r = redis.Redis(host=host, port=port, decode_responses=True, password=password)
logging.info("Fetching existing list at key `%s`.", source_key) current_list = r.lrange(source_key, "0", "-1") logging.info("Found list of length %d.", len(current_list))
new_list = copy.deepcopy(current_list) new_list.extend(Path(p).read_text().strip() for p in new_quotes) random.shuffle(new_list)
if (actual_len := r.llen(destination_key)) > 0: logging.error("Attempted to push result to non-empty key `%s` (length: %d). Aborting.", destination_key, actual_len) return
r.lpush(destination_key, *new_list) logging.info("Pushed new, shuffled list (length: %d) successfully to key `%s`.", len(new_list), destination_key)
if rename_key: logging.info("Renaming `%s` to `%s`.", destination_key, source_key) r.rename(destination_key, source_key)
if __name__ == '__main__': random.seed(time.monotonic()) typer.run(main)