Update README.md
[lace] / lacewithdate
1 #!/usr/bin/env bash
2 #Forked from https://gitlab.com/uoou/dotfiles/-/tree/master/stow/bin/home/drew/.local/bin/lace 
3 # Modified to show date rather than how many days ago by frrobert2
4 # https://github.com/frrobert2/lace/blob/main/lacewithdate
5 if [ -z "$XDG_CONFIG_HOME" ];then
6         config_place="$HOME/.config/lace"
7 else
8         config_place="$XDG_CONFIG_HOME/lace"
9 fi
10 [[ ! -d "$config_place" ]] && mkdir -p $config_place
11 subsfile="$config_place/subs"
12
13 red="[31m"
14 blue="[34m"
15 nc="[m"
16 esc=$(echo -e "\033")
17 tmpfile=$(mktemp /tmp/lace.XXXXXX)
18
19 function dohelp() {
20         echo -e "\nUsage:\n"
21         echo -e "\t$(basename "$0") - read subscriptions\n"
22         echo -e "\t$(basename "$0") nope - read subscriptions without pager\n"
23         echo -e "\t$(basename "$0") subs - list subscriptions\n"
24         echo -e "\t$(basename "$0") sub nickname url - add subscription, e.g.:"
25         echo -e "\t$(basename "$0") sub Drew gemini://friendo.monster/tiny.gmi\n"
26         echo -e "\t$(basename "$0") unsub nickname - remove subscription, e.g.:"
27         echo -e "\t$(basename "$0") unsub Drew\n"
28         echo -e "\t$(basename "$0") update - redownload script to location of current script (might need sudo, depending on location)\n"
29         exit
30 }
31
32 function timeline() {
33         while read f;do
34                 local user=$(awk '{print $2}' <<< "$f")
35                 local url=$(awk '{print $1}' <<< "$f")
36                 if [[ "$url" != *://* ]];then
37                         domain="$url"
38                         url="gemini://$url"
39                 else
40                         domain=$(sed 's/gemini:\/\///' <<< "$url")
41                 fi
42                 if [[ "$url" != *.*/*.* ]] && [[ $url != */ ]];then
43                         url="$url/"
44                 fi
45                 domain=$(sed 's/\/.*$//' <<< "$domain")
46                 fav=$(openssl s_client -crlf -quiet -connect "$domain:1965" <<<"gemini://$domain/favicon.txt" 2>/dev/null | sed -e '1d')
47                 if [ ! -z "$fav" ];then
48                         user="$fav $user"
49                 fi
50                 openssl s_client -crlf -quiet -connect "$domain:1965" <<<"$url" 2>/dev/null | sed -e '1d' | sed -n '/##/,$p' | sed -e "s/^\(## .*\)$/\1\n${esc}$red$user${esc}$nc ($domain)/g" >> $tmpfile
51                 [[ ! -z $(sed -n '$p' $tmpfile) ]] && echo "" >> $tmpfile
52         done <"$subsfile"
53         cat $tmpfile | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sed -rn '/^## .*/p' | sed -e 's/\v/\n/g' | sed -e "s/^## \(.*\)/date -d '\1' +%s/e" | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sort -r | sed -e 's/\v/\n/g'  > $tmpfile
54
55         sed -i "s/\(^[0-9]\{8,\}$\)/date -d @\1/e" $tmpfile
56         if [[ "$1" == "nope" ]];then
57                 cat $tmpfile && rm -f $tmpfile
58         else
59                 less -RisW $tmpfile && rm -f $tmpfile
60         fi
61 }
62
63 if [[ "$1" == "help" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-help" ]] || [[ "$1" == "-h" ]];then
64         dohelp
65 elif [[ "$1" == "sub" ]];then
66         [[ "$#" -ne 3 ]] && dohelp
67         echo "$3 $2" >> $subsfile && echo "added $2 to subscriptions"
68 elif [[ "$1" == "unsub" ]];then
69         [[ "$#" -ne 2 ]] && dohelp
70         if grep " $2$" $subsfile 1>/dev/null;then
71                 sed -i "/ $2$/d" $subsfile &&
72                 echo "removed subscription to $2"
73         else
74                 echo "subscription \"$2\" does not exist"
75         fi
76 elif [[ "$1" == "subs" ]];then
77         awk '{print $2" ("$1")"}' $subsfile 
78 elif [[ "$1" == "update" ]];then
79         curl https://raw.githubusercontent.com/frrobert2/lace/main/lacewithdate > "${BASH_SOURCE[0]}"
80 else
81         now_unix="$(date +'%s')"
82         if [[ "$1" == "nope" ]];then
83                 timeline nope
84         else
85                 timeline
86         fi
87 fi