sfeedql: add more stupid things to a stupid hack - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 2f5cce672c8db6fd93a458fb93a3de19b6e74b68
(DIR) parent 62eb9cd16d2fbc341f6a07be98bae017ed1d3f41
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Thu, 28 Sep 2023 17:30:01 +0200
sfeedql: add more stupid things to a stupid hack
Diffstat:
M sfeedql | 61 +++++++++++++++++++++++++++++--
1 file changed, 57 insertions(+), 4 deletions(-)
---
(DIR) diff --git a/sfeedql b/sfeedql
@@ -1,5 +1,58 @@
#!/bin/sh
+# sfeed Stupid Query Language hack
+# inspired by some ugly hack on some blog somewhere on the interwebs.
+#
+# Reads sfeed(5) data from stdin or from files.
+# It creates a temporary in-memory SQLite3 database for executing SQL queries.
+#
+# Options:
+# When files are specified then the basename of the file is the value for the
+# feedname column for stdin it is "-".
+#
+# Use the -q option to execute a SQL query, the table name is items.
+# the default query is to show the last 10 items (by timestamp DESC).
+#
+# Example: curl -s https://codemadness.org/atom_content.xml | \
+# sfeed | \
+# sfeedql -q "SELECT * FROM items WHERE title LIKE '%sfeed%'"
+usage() {
+ echo "$0 [-q query] [files...]" >&2
+ exit 1
+}
+
+# tsvwithfeed(suffix, filename)
+# use the sfeed(5) format, add the feedname as an extra field.
+tsvwithfeed() {
+ LC_ALL=C awk -v "suffix=$1" '
+BEGIN {
+ FS = OFS = "\t";
+}
+{
+ print $0 OFS suffix;
+}' "$2"
+}
+
+# default query
+query="SELECT * FROM items ORDER BY timestamp DESC LIMIT 10"
+
+while getopts q: name; do
+ case "$name" in
+ q) query="$OPTARG";;
+ ?) usage;;
+ esac
+done
+shift $(($OPTIND - 1))
+
+if test "$#" -eq 0; then
+ # no files specified: use stdin.
+ tsvwithfeed "-" "-"
+else
+ # ... or read from files.
+ for f in "$@"; do
+ tsvwithfeed "${f##*/}" "$f"
+ done
+fi | \
sqlite3 \
':memory:' \
'CREATE TABLE items (
@@ -7,14 +60,14 @@ sqlite3 \
title TEXT,
link TEXT,
content TEXT,
- content_type varchar(16),
+ content_type varchar(8),
id TEXT,
author TEXT,
enclosure TEXT,
- category TEXT);' \
+ category TEXT,
+ feedname nvarchar(255));' \
'.mode ascii' \
'.headers off' \
'.separator "\t" "\n"' \
'.import /dev/stdin items' \
-"${1:-SELECT * FROM items LIMIT 10}"
-
+"${query}"