pleroma-cli/pleroma-cli.sh

276 lines
7.7 KiB
Bash
Raw Normal View History

2022-04-11 19:34:04 +00:00
#!/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)
format_time='+%d.%m.%Y %H:%M:%S'
2022-04-11 19:34:04 +00:00
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
2022-04-11 19:34:04 +00:00
}
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)
2022-04-11 22:02:16 +00:00
id_status=$(echo $status | jj id)
2022-04-11 19:34:04 +00:00
dateutc=$(echo $status | jj created_at)
echo "$(date -d $dateutc "$format_time") <$id_status> $uri"
2022-04-11 19:34:04 +00:00
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
2022-04-11 22:02:16 +00:00
menu=$(echo 'Prev\nNext\nShare\nMain menu' | fzy)
2022-04-11 19:34:04 +00:00
case $menu in
"Prev")
2022-04-11 22:05:58 +00:00
indexator=$(expr $max_statuses - 1)
2022-04-11 19:34:04 +00:00
echo '#EXTM3U' > attachments.m3u8
clear
offset=$(jj -i preload 39.id)
json=$(timeline_api $offset)
;;
"Next")
2022-04-11 22:05:58 +00:00
indexator=$(expr $max_statuses - 1)
2022-04-11 19:34:04 +00:00
echo '#EXTM3U' > attachments.m3u8
clear
offset=$(jj -i preload 0.id)
json=$(timeline_api $offset)
;;
2022-04-11 22:02:16 +00:00
"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
;;
2022-04-11 19:34:04 +00:00
"Main menu")
sub_menu_lvl=0 ;;
esac
done
}
2022-04-11 22:02:16 +00:00
share_api_status()
{
curl -w "%{http_code}" -X POST -s --compressed -H "Authorization: Bearer $auth" --url $instance_point/statuses/$1/reblog
}
2022-04-11 19:34:04 +00:00
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_menu()
{
sub_menu_lvl=1
clrnotif='Clear all notifications'
json=$(notif_api_get_all)
while [ $sub_menu_lvl -eq 1 ]; do
clear
echo "Notifications: $(echo $json | jj \#)"
for i in $(echo $json | jj -l \#.id); do
date_utc=$(echo $json | jj \#[id=$i].created_at)
date -d "$date_utc" "$format_time"
acct=$(echo $json | jj \#[id=$i].account.acct)
typenotif=$(echo $json | jj \#[id=$i].type)
echo "$typenotif <- $acct"
echo $json | jj \#[id=$i].status.pleroma.content.text/plain
echo '___'
done
menu_choice=$(echo "Main menu\nRefresh\n$clrnotif" | fzy)
case "$menu_choice" in
"Main menu") sub_menu_lvl=0 ;;
"Refresh") json=$(notif_api_get_all) ;;
"$clrnotif") notif_api_remove_all && json=$(notif_api_get_all) ;;
esac
done
}
2022-04-11 19:34:04 +00:00
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'
2022-04-11 19:34:04 +00:00
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$switchinstance\n$Exit" | fzy)
2022-04-11 19:34:04 +00:00
else
main_menu=$(echo "$menu_write_status\n$menu_timeline\n$notif\n$authmake\n$switchinstance\n$Exit" | fzy)
2022-04-11 19:34:04 +00:00
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_menu ;;
2022-04-11 19:34:04 +00:00
"$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