summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2016-02-11 14:38:19 (CET)
committerp4bl0 <r _at_ uzy .dot. me>2016-02-11 14:38:19 (CET)
commitd7dafc9b629a59e938f6fb9904f4acc01ea0c630 (patch)
tree3a4aa54113e59a16b0b82516822050bdf5060e94
parentc48e0dd1a633e9a2566bdfcc6dd08eeb1722429a (diff)
downloadmyutils-d7dafc9b629a59e938f6fb9904f4acc01ea0c630.zip
myutils-d7dafc9b629a59e938f6fb9904f4acc01ea0c630.tar.gz
add terss
-rwxr-xr-xterss131
1 files changed, 131 insertions, 0 deletions
diff --git a/terss b/terss
new file mode 100755
index 0000000..3ac6bf3
--- /dev/null
+++ b/terss
@@ -0,0 +1,131 @@
+#!/bin/bash
+
+TERSSDIR=~/.terss
+
+_terss_tput=$(which tput)
+if [ ! -z "$_terss_tput" -a -x "$_terss_tput" ]; then
+ declare -A _terss_colors
+ declare -A _terss_styles
+ _terss_colors=([red]=$($_terss_tput setaf 1) [green]=$($_terss_tput setaf 2)
+ [yellow]=$($_terss_tput setaf 3) [blue]=$($_terss_tput setaf 4)
+ [orange]=$($_terss_tput setaf 5) [purple]=$($_terss_tput setaf 6)
+ [white]=$($_terss_tput setaf 7))
+ _terss_styles=([u]=$($_terss_tput sgr 0 1) [b]=$($_terss_tput bold) [n]="")
+ _terss_clear=$($_terss_tput sgr0)
+fi
+
+_terss_echo(){
+ local n=""
+ if [ "$1" = "-n" ]; then n="-n"; shift; fi
+ local c="${1%_*}"
+ local color=${_terss_colors[$c]}
+ local s="${1#*_}"
+ local style=${_terss_styles[$s]}
+ shift
+ echo $n "$style$color$*$_terss_clear"
+}
+
+function terss_init(){
+ echo -n "Username? "
+ read USERNAME
+ echo -n "Server (for SSH connection)? "
+ read SERVER
+ echo -n "Path of your feed (on the server)? "
+ read FEED
+ echo -n "Public URL of your feed (over HTTP)? "
+ read URL
+ mkdir -p "$TERSSDIR"
+ cat > "$TERSSDIR/config" <<EOF
+USERNAME="$USERNAME"
+SERVER="$SERVER"
+FEED="$FEED"
+EOF
+ cat > "$TERSSDIR/feed" <<EOF
+<?xml version="1.0" encoding="utf-8"?>
+<rss version="2.0">
+ <channel>
+ <title>$USERNAME TeRSS feed</title>
+ <link>$URL</link>
+ <description>$USERNAME TeRSS feed</description>
+ </channel>
+</rss>
+EOF
+ echo "$URL" > "$TERSSDIR/subscriptions"
+}
+
+function terss_follow(){
+ NAME="$1"
+ URL="$2"
+ echo -e "$NAME\t$URL" >> "$TERSSDIR/subscriptions"
+}
+
+function terss_unfollow(){
+ NAME="$1"
+ sed -i "\~^$NAME\t~d" "$TERSSDIR/subscriptions"
+}
+
+function terss_post(){
+ MSG="$1"
+ # prétraitement MSG (@name to link)
+ DATE="$(date -R)"
+ xml2 < "$TERSSDIR/feed" | grep -v -E '^/rss/channel/item' > "$TERSSDIR/tmp"
+ cat >>"$TERSSDIR/tmp" <<EOF
+/rss/channel/item/title=$MSG
+/rss/channel/item/pubDate=$DATE
+/rss/channel/item
+EOF
+ xml2 < "$TERSSDIR/feed" | grep -E '^/rss/channel/item' >> "$TERSSDIR/tmp"
+ echo '<?xml version="1.0" encoding="utf-8"?>' > "$TERSSDIR/feed"
+ 2xml < "$TERSSDIR/tmp" >> "$TERSSDIR/feed"
+ scp "$TERSSDIR/feed" "$SERVER:$FEED"
+}
+
+function terss_fetch(){
+ NAME="$1"
+ URL="$2"
+ wget -q "$URL" -O "$TERSSDIR/feeds/$NAME"
+ xml2 < "$TERSSDIR/feeds/$NAME" | grep -E '^/rss/channel/item' | \
+ sed 's~^/rss/channel/~~' > "$TERSSDIR/feeds/$NAME.tmp"
+ MSG=
+ DATE=
+ while read LINE; do
+ if [[ "$LINE" = "item" && ! -z "$MSG" && ! -z "$DATE" ]]; then
+ echo -e "$DATE\t$NAME\t$MSG"
+
+ elif [[ "${LINE%%=*}" = "item/title" ]]; then
+ MSG="${LINE#*=}"
+ elif [[ "${LINE%%=*}" = "item/pubDate" ]]; then
+ DATE=$(date -d "${LINE#*=}" +"%s")
+ fi
+ done < "$TERSSDIR/feeds/$NAME.tmp" >> "$TERSSDIR/timeline.tmp"
+ echo -e "$DATE\t$NAME\t$MSG" >> "$TERSSDIR/timeline.tmp"
+}
+
+function terss_list(){
+ MAX="$1"
+ mkdir -p "$TERSSDIR/feeds"
+ rm "$TERSSDIR/timeline.tmp"
+ touch "$TERSSDIR/timeline.tmp"
+ local IFS=$'\t'
+ while read SUB; do
+ SUB=($SUB)
+ terss_fetch "${SUB[0]}" "${SUB[1]}"
+ done < "$TERSSDIR/subscriptions"
+ sort -n -r -k1 < "$TERSSDIR/timeline.tmp" | head -"$MAX" > "$TERSSDIR/timeline"
+ while read MSG; do
+ MSG=($MSG)
+ _terss_echo -n green_u "${MSG[1]}"
+ _terss_echo white ' ('$(date -d "@${MSG[0]}" +"%Y-%m-%d %H:%M")') :'
+ _terss_echo white_b " ${MSG[2]}"
+ echo
+ done < "$TERSSDIR/timeline"
+}
+
+. "$TERSSDIR/config"
+case "$1" in
+ init) terss_init ;;
+ follow) terss_follow "$2" "$3" ;;
+ unfollow) terss_unfollow "$2" ;;
+ post) shift; terss_post "$*" ;;
+ list) terss_list "${2:-20}" ;;
+esac
Pablo Rauzy — generated by cgit