diff options
Diffstat (limited to 'flake.nix')
| -rw-r--r-- | flake.nix | 51 |
1 files changed, 40 insertions, 11 deletions
@@ -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" '' |
