dot

packages and services management
Log | Files | Refs | README

commit da50f5c8dbdd3d278bd7f29e6c2b14a8164f563f
parent 35f3134befae47e69ba4c874608efa8afb6d3ad6
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 20 Nov 2017 00:57:15 +0100

further progress on twtsh

Diffstat:
Mbin/twtsh | 64+++++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 45 insertions(+), 19 deletions(-)

diff --git a/bin/twtsh b/bin/twtsh @@ -1,8 +1,10 @@ #!/bin/sh +# twtxt client in a single small POSIX shell script TWT_NICK=josuah TWT_FILE=$HOME/twtxt.txt TWT_CONF=$HOME +TWT_CACHE=$HOME/twtxt err() { @@ -10,9 +12,6 @@ err() exit 1 } -mkdir -p "$TWT_CONF" -touch "$TWT_CONF/following.txt" - awk_date=' function bissextile(yrs) { @@ -66,10 +65,29 @@ function rfc3339(str) { hrs = substr(str, 12, 2); min = substr(str, 15, 2); sec = substr(str, 18, 2); - off = substr(str, 20, 1) =="Z" ? 0 : substr(str, 20, 3); + off = substr(str, 20, 1) == "Z" ? 0 : substr(str, 20, 3); return to_sec(yrs, mth, day, hrs, min, sec) - off * 3600; }' +awk_line="$awk_date"' +/^20/ { + "date +%Z" | getline off + sec = rfc3339($1) + off * 3600; + printf("%s", to_date("%04d/%02d/%02d %02d:%02d:%02d", sec)); + sub("[^\t]*\t", "", $0); + sub(".*/", "", FILENAME); + printf(" %s %s\n", FILENAME, $0); +}' + +awk_show=' +{ + sub("[^ ]+ [^ ]+ [^ ]+ ", "$0" + while (length() > 72) +}' + +mkdir -p "$TWT_CONF" "$TWT_CACHE" +touch "$TWT_CONF/following" + case "$1" in (tweet) [ $# -ne 2 ] && exec "$0" @@ -81,37 +99,45 @@ case "$1" in usr=$(printf %s "$usr" | sed 's/./[&]/g') url=$(printf %s "$url" | sed 's/[&/\]/\\&/g') msg=$(printf %s "$msg" | sed "s/@$usr/@<$new $url>/") - done < "$TWT_CONF/following.txt" + done < "$TWT_CONF/following" printf '%s\t%s\n' "$(date +'%Y-%m-%dT%T%z' | sed 's/..$/:&/')" "$msg" | tr '\n' ' ' >> "$TWT_FILE" ;; (view) - [ $# -ne 2 ] && exec err "$0" - awk -v off="$(date +%z)" "$awk_date"'{ - sec = rfc3339($1) + off * 3600; - print(to_date("%04d/%02d/%02d %02d:%02d:%02d", sec)); - }' "$TWT_FILE" + [ $# -ne 2 ] && exec "$0" + [ -f "$TWT_CACHE/$2" ] || err "$2 feed not found. update and try again" + awk "$awk_line" "$TWT_CACHE/$2" | sort | awk "$awk_show" + ;; +(timeline) + [ $# -ne 1 ] && exec "$0" + touch "$TWT_CACHE"/* || err 'no feed found. update and try again' + awk "$awk_line" "$TWT_CACHE"/* | sort | awk "$awk_show" + ;; +(update) + while read -r usr url + do curl -#L "$url" > "$TWT_CACHE/$usr" || rm "$TWT_CACHE/$usr" & + done < "$TWT_CONF/following" + wait ;; (follow) [ $# -ne 3 ] && exec "$0" - sed 's/[ ].*//' "$TWT_CONF/following.txt" | grep -Fqx "$2" && - err "already following the nick $2" - printf '%s\t%s\n' "$2" "$3" >> "$TWT_CONF/following.txt" - sort -u "$TWT_CONF/following.txt" -o "$TWT_CONF/following.txt" + sed 's/[ ].*//' "$TWT_CONF/following" | grep -Fqx "$2" && + err "already following the user $2" + printf '%s\t%s\n' "$2" "$3" >> "$TWT_CONF/following" + sort -u "$TWT_CONF/following" -o "$TWT_CONF/following" ;; (unfollow) [ $# -ne 2 ] && exec "$0" - tr '\t' ' ' | sed 's/ .*//' "$TWT_CONF/following.txt" | grep -Fx "$2" || + tr '\t' ' ' | sed 's/ .*//' "$TWT_CONF/following" | grep -Fx "$2" || err "not folling $2" ;; (following) [ $# -ne 1 ] && exec "$0" - tr '\t' ' ' < "$TWT_CONF/following.txt" | + tr '\t' ' ' < "$TWT_CONF/following" | sed -r -e 's/ / /' -e 's/(.{10}[^ ]*) */ \1 @ /' ;; (*) - printf 'usage: twtsh tweet msg | view nick | timeline\n' - printf ' follow nick url | unfollow nick | following\n' + printf 'usage: twtsh tweet msg | view user | timeline | update\n' + printf ' follow user url | unfollow user | following\n' ;; esac -