From e7e13ebc4199e29a26dedceca3e3f94a3f5b81c0 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 25 Jun 2023 04:45:23 +0000 Subject: [PATCH] Add intake-echo --- README.md | 9 +++++++++ flake.nix | 4 +++- intake-echo/intake_echo/__main__.py | 3 +++ intake-echo/intake_echo/core.py | 22 ++++++++++++++++++++++ intake-echo/pyproject.toml | 10 ++++++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 intake-echo/intake_echo/__main__.py create mode 100644 intake-echo/intake_echo/core.py create mode 100644 intake-echo/pyproject.toml diff --git a/README.md b/README.md index df4864b..5b2bd3c 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,12 @@ Supported `env`: - `FETCH_COUNT`: Number of posts to fetch from the front page. Default 30. - `REQUEST_RETRY`: Attempt count for fetching posts. Retries are done with exponential backoff. - `MIN_SCORE`: Skip stories with scores below this number. + +## intake-echo + +A feed source that echoes a message. This is useful with `cron` support to create recurring reminders. + +Supported `env`: +- `TITLE`: The title of the item. +- `BODY`: The body of the item. +- `UNIQUE`: If set to a truthy value, the item id will be the hash of the title, so the same item will be generated until the message is changed. diff --git a/flake.nix b/flake.nix index c7e863a..4d4479e 100644 --- a/flake.nix +++ b/flake.nix @@ -14,7 +14,7 @@ }); in { default = pkgs.intakeSources; - inherit (pkgs) intake-rss intake-reddit intake-hackernews; + inherit (pkgs) intake-rss intake-reddit intake-hackernews intake-echo; }; devShells.${system} = { @@ -44,11 +44,13 @@ final.intake-rss final.intake-reddit final.intake-hackernews + final.intake-echo ]; }; intake-rss = pythonPackage "intake-rss" ./intake-rss [ final.python38Packages.feedparser ]; intake-reddit = pythonPackage "intake-reddit" ./intake-reddit []; intake-hackernews = pythonPackage "intake-hackernews" ./intake-hackernews []; + intake-echo = pythonPackage "intake-echo" ./intake-echo []; }; nixosModules.default = { diff --git a/intake-echo/intake_echo/__main__.py b/intake-echo/intake_echo/__main__.py new file mode 100644 index 0000000..06684aa --- /dev/null +++ b/intake-echo/intake_echo/__main__.py @@ -0,0 +1,3 @@ +import sys +from .core import main +sys.exit(main()) diff --git a/intake-echo/intake_echo/core.py b/intake-echo/intake_echo/core.py new file mode 100644 index 0000000..984e2b3 --- /dev/null +++ b/intake-echo/intake_echo/core.py @@ -0,0 +1,22 @@ +import hashlib +import json +import os +import random + + +def main(): + item = {} + + title = os.environ.get("TITLE", "Hello, world!") + + if os.environ.get("UNIQUE"): + item["id"] = hashlib.md5(title.encode("utf8")).hexdigest() + else: + item["id"] = "{:x}".format(random.getrandbits(16 * 4)) + + item["title"] = title + + if body := os.environ.get("BODY"): + item["body"] = body + + print(json.dumps(item)) diff --git a/intake-echo/pyproject.toml b/intake-echo/pyproject.toml new file mode 100644 index 0000000..2d01d05 --- /dev/null +++ b/intake-echo/pyproject.toml @@ -0,0 +1,10 @@ +[project] +name = "intake-echo" +version = "0.1.0" + +[project.scripts] +intake-echo = "intake_echo.core:main" + +[tool.setuptools] +packages = ["intake_echo"] +