pleroma-cli/pleroma-cli.sh

250 lines
7 KiB
Bash
Executable file

#!/bin/sh
instance=$(jj -i config.json instance)
instance_point="https://$instance/api/v1"
instance_hist='instance.hist'
max_statuses=$(jj -i config.json max_statuses)
default_visibility=$(jj -i config.json default_visibility)
default_content_type=$(jj -i config.json default_content_type)
mkdir -m 711 -p .app_sessions
touch .auth.json
chmod 600 .auth.json
auth="$(jj -i .auth.json "$(echo $instance | sed 's/\./\\\./g')")"
echo "Instance: $instance"
if [ -n "$auth" ]; then
default_curl_opt()
{
curl -s --compressed -H "Authorization: Bearer $auth" $1
}
echo '+Authorized account+'
else
default_curl_opt()
{
curl -s --compressed $1
}
echo 'Please make auth and restart'
fi
auth_api_create_client()
{
if [ ! -e ".app_sessions/$instance" ]; then
curl -s --compressed --url $instance_point/apps \
--data-urlencode 'client_name=pleroma-cli' \
--data-urlencode 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \
--data-urlencode 'scopes=read write follow' \
--data-urlencode 'website=https://gitea.phreedom.club/localhost_frssoft/pleroma-cli' \
--output .app_sessions/$instance
chmod 600 .app_sessions/$instance
fi
}
auth_api_get_code()
{
auth_api_create_client
client_id=$(jj -i .app_sessions/$instance client_id)
links "https://$instance/oauth/authorize?client_id=$client_id&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=read+write+follow"
echo 'Input token-code:'
read pass
}
auth_api_get_token()
{
auth_api_get_code
clear
client_id=$(jj -i .app_sessions/$instance client_id)
client_secret=$(jj -i .app_sessions/$instance client_secret)
token=$(curl -s --compressed --url https://$instance/oauth/token \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode "client_id=$client_id" \
--data-urlencode "client_secret=$client_secret" \
--data-urlencode "redirect_uri=urn:ietf:wg:oauth:2.0:oob" \
--data-urlencode 'scope=read write follow' \
--data-urlencode "code=$pass" | jj access_token)
jj -p -i .auth.json -v "$token" "$(echo $instance | sed 's/\./\\\./g')" -o .auth.json
}
timeline_api()
{
default_curl_opt "$instance_point/$timeline?limit=$max_statuses&max_id=$1" | tee preload
}
timeline_menu()
{
json=$(timeline_api)
indexator=$(expr $max_statuses - 1)
sub_menu_lvl=1
echo '#EXTM3U' > attachments.m3u8
while [ $sub_menu_lvl -eq 1 ]; do
while [ $indexator -gt 0 ]; do
status=$(echo $json | jj $indexator)
uri=$(echo $status | jj uri)
id_status=$(echo $status | jj id)
dateutc=$(echo $status | jj created_at)
echo "$(date -d $dateutc) <$id_status> $uri"
echo $status | jj content | sed -e 's/<br[^>]*>/\n/g ; s/<[^>]*>//g ; s/&gt;*/>/g ; s/&lt;*/</g ; s/&quot;/"/g'
attachments=$(echo $status | jj -l media_attachments.#.remote_url | sed 's/"//g')
if [ -n "$attachments" ]; then
echo "#EXTINF:-1, $uri" >> attachments.m3u8
echo "$attachments" >> attachments.m3u8
fi
indexator=$(expr $indexator - 1)
echo '_____'
done
menu=$(echo 'Prev\nNext\nShare\nMain menu' | fzy)
case $menu in
"Prev")
indexator=$(expr $max_statuses - 1)
echo '#EXTM3U' > attachments.m3u8
clear
offset=$(jj -i preload 39.id)
json=$(timeline_api $offset)
;;
"Next")
indexator=$(expr $max_statuses - 1)
echo '#EXTM3U' > attachments.m3u8
clear
offset=$(jj -i preload 0.id)
json=$(timeline_api $offset)
;;
"Share")
echo 'Input id (s - stop)'
sharemode=1
while [ $sharemode -eq 1 ]; do
read status_id
if [ "$status_id" = 's' ]; then
sharemode=0
else
share_api_status $status_id >> /dev/null
echo $http_code
fi
done
;;
"Main menu")
sub_menu_lvl=0 ;;
esac
done
}
share_api_status()
{
curl -w "%{http_code}" -X POST -s --compressed -H "Authorization: Bearer $auth" --url $instance_point/statuses/$1/reblog
}
write_api_status()
{
curl -s --compressed -H "Authorization: Bearer $auth" --url $instance_point/statuses \
--data-urlencode "status=$1" \
--data-urlencode "content_type=$content_type" \
--data-urlencode "visibility=$status_visibility"
}
write_status_menu()
{
touch tmp_status.md
content_type="$default_content_type"
status_visibility="$default_visibility"
sub_menu_lvl=1
while [ $sub_menu_lvl -eq 1 ]; do
clear
echo 'Status:'
cat tmp_status.md
echo '\==========/'
echo "Chars: $(cat tmp_status.md | wc -m)"
echo "Visiblity: $status_visibility"
echo "Content type: $content_type"
wrirepostmenu=$(echo "Write\nSend\nChange type\nVisiblity\nMain menu" | fzy)
case $wrirepostmenu in
"Write") echo > tmp_status.md; $EDITOR tmp_status.md ;;
"Send") write_api_status "$(cat tmp_status.md)" | jj -p ;;
"Change type") content_type=$(echo 'text/plain\ntext/markdown\ntext/html' | fzy) ;;
"Visiblity")
status_visibility=$(echo 'public\nunlisted\nlocal\nprivate\ndirect\nlist' | fzy)
;;
"Main menu") sub_menu_lvl=0 ;;
esac
done
}
notif_api_get_all()
{
default_curl_opt "$instance_point/notifications" | jj -p
}
notif_api_remove_all()
{
curl -X POST --compressed -H "Authorization: Bearer $auth" --url $instance_point/notifications/clear
}
menu_write_status='Write status'
menu_timeline='Timelines'
notif='Notifications (raw)'
clrnotif='Clear all notifications'
authmake='Auth'
switchinstance='Switch instance'
Exit='Exit'
while true; do
if [ -n "$auth" ]; then
main_menu=$(echo "$menu_write_status\n$menu_timeline\n$notif\n$clrnotif\n$switchinstance\n$Exit" | fzy)
else
main_menu=$(echo "$menu_write_status\n$menu_timeline\n$notif\n$clrnotif\n$authmake\n$switchinstance\n$Exit" | fzy)
fi
case $main_menu in
"$menu_write_status") write_status_menu ;;
"$menu_timeline")
timeline=$(echo 'timelines/home\nfavourites\ntimelines/direct\ntimelines/public' | fzy)
timeline_menu
;;
"$notif") notif_api_get_all | more ;;
"$clrnotif") notif_api_remove_all ;;
"$switchinstance")
empty=0
case $(echo 'Recently used\nChoice from list\nManual input' | fzy) in
"Recently used")
if [ -s $instance_hist ]; then
touch $instance_hist && instance=$(cat $instance_hist | fzy)
else
echo 'No recently used instances...'
empty=1
fi ;;
"Choice from list") instance=$(jj -l -i config.json public_list_instances | sed 's/"//g' | fzy) ;;
"Manual input") echo "Type instance (ex. $instance):" && read instance ;;
esac
if [ $empty -eq 0 ]; then
echo $instance >> $instance_hist
cat $instance_hist | sort | uniq | tee $instance_hist 1>>/dev/null
export instance
export instance_point="https://$instance/api/v1"
conf_instance_state=$(echo 'Permanent\nTemporaly' | fzy)
if [ "$conf_instance_state" = 'Permanent' ]; then
jj -i config.json instance -v $instance -o config.json
else
echo ''
fi
clear
auth="$(jj -i .auth.json "$(echo $instance | sed 's/\./\\\./g')")"
echo "Instance: $instance"
if [ -n "$auth" ]; then
default_curl_opt()
{
curl -s --compressed -H "Authorization: Bearer $auth" $1
}
echo '+Authorized account+'
else
default_curl_opt()
{
curl -s --compressed $1
}
echo 'Please make auth and restart'
fi
fi ;;
"$authmake") auth_api_get_token ;;
"$Exit") exit 0 ;;
esac
done