forked from g1n/twtxt-c
214 lines
5.9 KiB
Bash
Executable File
214 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
PROGNAME=${0##*/}
|
||
VERSION="0.0.1"
|
||
|
||
# config variables
|
||
# you can set these according to your choice
|
||
nick=$USER # change this to your desired nick
|
||
path_to_twtxt=$HOME/twtxt.txt
|
||
url="https://example.com/twtxt.txt"
|
||
# list of getwtxt registries api
|
||
reg1="https://twtxt.envs.net/api/plain/"
|
||
reg2="https://twtxt.tilde.institute/api/plain/"
|
||
|
||
|
||
error_exit() {
|
||
printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2
|
||
exit 1
|
||
}
|
||
|
||
usage() {
|
||
printf "usage: %s [OPTIONS] COMMAND [ARGS]\n" "$PROGNAME"
|
||
printf "Simple client for twtxt\n"
|
||
printf "Options:\n
|
||
-c, --config PATH Specify a custom config file location.
|
||
-v, --verbose Enable verbose output for debugging purposes.
|
||
--version Show the version and exit.
|
||
-h, --help Show this message and exit.
|
||
|
||
Commands:
|
||
init Setting up twtxt.
|
||
config Get or set config item.
|
||
follow Add a new source to your followings.
|
||
following Return the list of sources you’re following.
|
||
timeline Retrieve your personal timeline.
|
||
tweet Append a new tweet to your twtxt file.
|
||
unfollow Remove an existing source from your...
|
||
view Show feed of given source.
|
||
reg list Show registers
|
||
reg add Add your twtxt to registry
|
||
reg users List users on the registry
|
||
reg twts Fetch recent tweets from registry
|
||
reg mentions search for mentions by url
|
||
reg tag Search for tags
|
||
"
|
||
|
||
}
|
||
|
||
|
||
|
||
read_config () {
|
||
declare -A v=()
|
||
while read -r var value; do
|
||
v[$var]=$value
|
||
done < $config/registry
|
||
}
|
||
case $1 in
|
||
-h | --help)
|
||
usage; exit ;;
|
||
-* | --*)
|
||
usage; error_exit "unknown option $1" ;;
|
||
init)
|
||
printf "The config variables are stored in this script itself \n Enter yes if you want to open it now \n Enter no if you want to edit it yourself later\n"
|
||
while true; do
|
||
read -rp "$2 " yn
|
||
case $yn in
|
||
[Yy]*) open=1; break;;
|
||
[Nn]*) open=0; break;;
|
||
*) echo "Please answer yes or no"
|
||
esac
|
||
done
|
||
if [[ $open == 1 ]]
|
||
then {
|
||
if which twtxt-c &> /dev/null
|
||
then
|
||
scriptpath=$(which twtxt-c)
|
||
else {
|
||
read -p "Path to this script [`pwd`/twtxt-c]" scriptpath
|
||
scriptpath=${scriptpath:-"`pwd`/twtxt-c"}
|
||
}
|
||
fi
|
||
if [ -z "$EDITOR" ]
|
||
then
|
||
{
|
||
if which vim &> /dev/null
|
||
then
|
||
vim $scriptpath
|
||
elif which nvim &> /dev/null
|
||
then
|
||
nvim $scriptpath
|
||
elif which emacs &> /dev/null
|
||
then
|
||
emacs -nw $scriptpath
|
||
elif which nano &> /dev/null
|
||
then
|
||
nano $scriptpath
|
||
else
|
||
echo " EDITOR variable is empty and vim, neovim, emacs and nano not found. please open this script using your preferred text editor and set the config variables"
|
||
fi
|
||
echo "Now you can tweet using twtxt-c tweet <tweet>"
|
||
}
|
||
else
|
||
$EDITOR $scriptpath
|
||
fi
|
||
}
|
||
else
|
||
echo "Please edit the script and set nick, path to twtxt file and url for twtxt file"
|
||
fi
|
||
echo "would you like to copy this script to $HOME/.local/bin [yes/no]"
|
||
while true; do
|
||
read -rp "$3 " yn
|
||
case $yn in
|
||
[Yy]*) copy=1; break;;
|
||
[Nn]*) copy=0; break;;
|
||
*) echo "Please answer yes or no"
|
||
esac
|
||
done
|
||
if [[ $copy == 1 ]]
|
||
then {
|
||
# give warning if path not ok
|
||
if [[ $PATH != *"$HOME/.local/bin"* && $PATH != *"~/.local/bin"* ]]
|
||
then
|
||
echo " Warning: $HOME/.local/bin is not in PATH "
|
||
fi
|
||
# checking for scriptpath
|
||
if which twtxt-c &> /dev/null
|
||
then
|
||
echo "already present"
|
||
elif [[ -z "$scriptpath" ]]
|
||
then {
|
||
read -p "Path to this script [`pwd`/twtxt-c]" scriptpath
|
||
scriptpath=${scriptpath:-"`pwd`/twtxt-c"}
|
||
cp $scriptpath $HOME/.local/bin/
|
||
}
|
||
else
|
||
cp $scriptpath $HOME/.local/bin/
|
||
fi
|
||
}
|
||
fi
|
||
|
||
printf "Would you like to set the following aliases \n ttc=twtxt-c timeline\n twtc=twtxt-c tweet\n"
|
||
while true; do
|
||
read -rp "$3 " yn
|
||
case $yn in
|
||
[Yy]*) als=1; break;;
|
||
[Nn]*) als=0; break;;
|
||
*) echo "Please answer yes or no"
|
||
esac
|
||
done
|
||
if [[ $als == 1 ]]
|
||
then {
|
||
read -p "Path to alias file [$HOME/.bash_aliases]" alspath
|
||
alspath=${alspath:-"$HOME/.bash_aliases"}
|
||
echo "alias ttc='twtxt-c timeline'" >> $alspath
|
||
echo "alias twtc='twtxt-c tweet'" >> $alspath
|
||
}
|
||
fi
|
||
;;
|
||
tweet)
|
||
export config=/home/$USER/.config/twtxt-c/config
|
||
printf "%s\t%s\n" "$(date +'%FT%T%:z')" "$2" >> $(awk '/twtfile/ {print $3}' $config)
|
||
;;
|
||
view)
|
||
curl $2 2>/dev/null | grep -v "#" | awk 'BEGIN { FS = "\t" } ; {print $2}'
|
||
;;
|
||
reg)
|
||
case $2 in
|
||
list)
|
||
cat /home/$USER/.config/twtxt-c/registry
|
||
;;
|
||
add)
|
||
config=/home/$USER/.config/twtxt-c/
|
||
declare -A v=()
|
||
while read -r line; do
|
||
var=$(echo $line | awk '{print $1}' )
|
||
value=$(echo $line | awk '{print $3}' )
|
||
v[$var]=$value
|
||
done < $config/config
|
||
req="https://twtxt.tilde.institute/api/plain/users?url=${v[url]}&nickname=${v[nick]}"
|
||
req2="https://twtxt.envs.net/api/plain/users?url=${v[url]}&nickname=${v[nick]}"
|
||
echo "post these requests:"
|
||
echo "curl -X POST $req"
|
||
echo "curl -X POST $req2"
|
||
#curl -X POST $req
|
||
#curl -X POST $req2
|
||
# I have commented the actual curl request so that I accidentally re-register myself during testing
|
||
;;
|
||
users)
|
||
curl 'https://twtxt.tilde.institute/api/plain/users'
|
||
curl 'https://twtxt.envs.net/api/plain/users' ;;
|
||
twts)
|
||
curl 'https://twtxt.tilde.institute/api/plain/tweets'
|
||
curl 'https://twtxt.envs.net/api/plain/tweets'
|
||
;;
|
||
mentions)
|
||
url=$3
|
||
req="https://twtxt.tilde.institute/api/plain/mentions?url=$url"
|
||
req2="https://twtxt.envs.net/api/plain/mentions?url=$url"
|
||
curl $req
|
||
curl $req2
|
||
;;
|
||
tag)
|
||
tag=$3
|
||
req="https://twtxt.tilde.institute/api/plain/tags/$tag"
|
||
req2="https://twtxt.envs.net/api/plain/tags/$tag"
|
||
curl $req
|
||
curl $req2
|
||
;;
|
||
esac ;;
|
||
*)
|
||
printf "Work in progress\n" ;;
|
||
esac
|
||
|