summaryrefslogtreecommitdiff
path: root/flake.nix
diff options
context:
space:
mode:
authorBen Sima <ben@bensima.com>2026-06-04 16:38:54 -0400
committerBen Sima <ben@bensima.com>2026-06-04 16:38:54 -0400
commitfe04ad47c9abf843cb6f64176981325a5b05f074 (patch)
tree212dc37a6ed0a16eebc72f3ab06b35949b09b71b /flake.nix
parent11fd65b015dc6e24127f3cfdcb08975704381d46 (diff)
Add tests and fix research tools
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix51
1 files changed, 40 insertions, 11 deletions
diff --git a/flake.nix b/flake.nix
index a70e64e..a45dfa7 100644
--- a/flake.nix
+++ b/flake.nix
@@ -36,7 +36,7 @@
slopSearch = pkgs.writeShellApplication {
name = "slop-search";
- runtimeInputs = [ pkgs.curl pkgs.jq pkgs.python3 ];
+ runtimeInputs = [ pkgs.coreutils pkgs.curl pkgs.python3 ];
text = ''
if [ "$#" -eq 0 ]; then
echo "usage: slop-search QUERY" >&2
@@ -50,25 +50,44 @@
query="$*"
encoded="$(${pkgs.python3}/bin/python -c 'import sys, urllib.parse; print(urllib.parse.urlencode({"q": " ".join(sys.argv[1:])}))' "$@")"
+ tmp="$(mktemp)"
+ trap 'rm -f "$tmp"' EXIT
curl -fsS \
-H "Authorization: Bot ''${KAGI_API_KEY}" \
-H "Accept: application/json" \
"https://kagi.com/api/v0/search?''${encoded}" \
- | jq -r --arg query "$query" '
- "QUERY: \($query)",
- "",
- (.data // [])
- | map(select((.t // .type // "") == "search_result" or has("url")))
- | .[:8]
- | to_entries[]
- | "[\(.key + 1)] \(.value.title // "untitled")\nURL: \(.value.url)\nSNIPPET: \(.value.snippet // .value.description // "")\n"
- '
+ -o "$tmp"
+ ${pkgs.python3}/bin/python - "$query" "$tmp" <<'PY'
+import json
+import sys
+
+query, path = sys.argv[1], sys.argv[2]
+payload = json.load(open(path))
+items = payload.get("data") or []
+results = []
+for item in items:
+ if not item.get("url"):
+ continue
+ item_type = item.get("t") or item.get("type") or ""
+ if item_type and item_type != "search_result":
+ continue
+ results.append(item)
+ if len(results) >= 8:
+ break
+
+print(f"QUERY: {query}\n")
+for i, item in enumerate(results, 1):
+ title = item.get("title") or "untitled"
+ url = item.get("url") or ""
+ snippet = item.get("snippet") or item.get("description") or ""
+ print(f"[{i}] {title}\nURL: {url}\nSNIPPET: {snippet}\n")
+PY
'';
};
slopFetch = pkgs.writeShellApplication {
name = "slop-fetch";
- runtimeInputs = [ pkgs.curl researchPython ];
+ runtimeInputs = [ pkgs.coreutils pkgs.curl researchPython ];
text = ''
if [ "$#" -ne 1 ]; then
echo "usage: slop-fetch URL" >&2
@@ -107,6 +126,16 @@ PY
'';
};
+ checks.${system}.default = pkgs.runCommand "slopbot-tests" {
+ buildInputs = [ pythonEnv ];
+ } ''
+ cp -r ${self} source
+ cd source
+ chmod -R u+w .
+ python -m unittest discover -s tests
+ touch $out
+ '';
+
packages.${system} = rec {
default = slopbot;
slopbot = pkgs.writeShellScriptBin "slopbot" ''