peertube-cli/peertube-cli.sh

373 lines
10 KiB
Bash
Raw Normal View History

2022-04-05 11:05:58 +00:00
#!/bin/sh
2023-10-11 10:48:57 +00:00
workdir="$HOME/.local/share/peertube-cli"
instance=$(jj -i $workdir/config.json instance)
pref_video_quality=$(jj -i $workdir/config.json prefer_quality_video)
torrent_enabled=$(jj -i $workdir/config.json torrent_enabled)
rpcport=9095
2023-10-11 10:48:57 +00:00
instance_hist="$workdir/instance.hist"
2022-04-05 11:05:58 +00:00
ordering='title'
tempvideostor=$(mktemp -d)
default_player_command="mpv --hwdec --network-timeout=30 --profile=low-latency --cache=yes --cache-on-disk=yes --cache-dir=$tempvideostor --cache-unlink-files=immediate"
2022-04-05 11:05:58 +00:00
2023-10-11 10:48:57 +00:00
mkdir -p $workdir
if [ ! -f "$workdir/config.json" ]; then
cp -r config.json transmission-daemon $workdir
echo 'config created'
fi
2022-04-05 11:05:58 +00:00
instance_point="https://$instance/api/v1"
2022-04-14 11:23:30 +00:00
default_curl_opt()
{
curl -s --compressed "$@"
}
2022-04-05 11:05:58 +00:00
2022-04-05 16:16:13 +00:00
torrent_init()
{
if [ "$torrent_enabled" = 'true' ]; then
2023-10-11 10:48:57 +00:00
tmp_dir=$(mktemp -d)
transmission-daemon -T -g $workdir/transmission-daemon -w $tmp_dir --password '' -p $rpcport
2022-04-05 16:16:13 +00:00
fi
}
2022-04-06 13:23:40 +00:00
check_connect()
{
2023-10-11 10:56:31 +00:00
if [ -n "$(curl --head -s "$instance_point"/config | grep 'HTTP/. 200')" ]; then
2022-04-06 13:23:40 +00:00
echo 'OK'
else
echo
fi
}
2022-04-05 11:05:58 +00:00
peertube_api_check_ratelimit()
{
2022-04-14 11:23:30 +00:00
default_curl_opt --head "$instance_point/" | grep rate
2022-04-05 11:05:58 +00:00
}
peertube_api_version_server()
{
2022-04-14 11:23:30 +00:00
default_curl_opt "$instance_point/config" | jj serverVersion | cut -f 1 -d.
}
2022-04-05 11:05:58 +00:00
peertube_api_get_all_videos()
{
2023-10-11 10:48:57 +00:00
default_curl_opt "$instance_point/videos?count=100&start=$1" | jj -p | tee $workdir/preload >> /dev/null
}
peertube_api_get_local_videos()
{
2022-04-18 14:12:22 +00:00
if [ "$version" -gt 3 ]; then
2023-10-11 10:48:57 +00:00
default_curl_opt "$instance_point/videos?count=100&start=$1&isLocal=true" | jj -p | tee $workdir/preload >> /dev/null
else
2023-10-11 10:48:57 +00:00
default_curl_opt "$instance_point/videos?count=100&start=$1&filter=local" | jj -p | tee $workdir/preload >> /dev/null
fi
2022-04-05 11:05:58 +00:00
}
2022-04-05 22:12:47 +00:00
peertube_api_get_live_streams()
{
2023-10-11 10:48:57 +00:00
default_curl_opt "$instance_point/videos?count=100&start=$1&isLive=true&sort=-publishedAt" | jj -p | tee $workdir/preload >> /dev/null
2022-04-05 22:12:47 +00:00
}
2022-05-06 21:26:17 +00:00
peertube_api_get_livechat()
{
2023-06-11 18:04:03 +00:00
versions_plugin_livechat='6.0.0 5.9.0 5.8.0 5.7.1 5.7.0 5.6.0 5.5.0 5.4.0 5.3.0'
2022-05-06 21:26:17 +00:00
for version_chat in $versions_plugin_livechat; do
link_on_chat="https://$instance/plugins/livechat/$version_chat/router/webchat/room/$1"
curl -s --head $link_on_chat | grep "HTTP\/. 200"
if [ $? -eq 0 ]; then
echo "$link_on_chat"
chat_found=1
break
fi
done
if [ -z $chat_found ]; then
echo 'Chat not avalaible'
fi
}
2022-04-05 11:05:58 +00:00
peertube_api_get_video()
{
2022-04-14 11:23:30 +00:00
default_curl_opt "$instance_point/videos/$1"
2022-04-05 11:05:58 +00:00
}
2022-04-06 11:11:24 +00:00
peertube_api_get_fulldescription()
{
2022-04-14 11:23:30 +00:00
default_curl_opt "$instance_point/videos/$1/description" | jj description
2022-04-06 11:11:24 +00:00
}
2022-04-05 11:05:58 +00:00
peertube_menu_videos()
{
2023-10-11 10:48:57 +00:00
echo "Avalaible $(jj -i $workdir/preload total) videos"
2022-04-05 11:05:58 +00:00
sub_menu=1
2022-04-05 20:37:09 +00:00
page=0
2022-04-05 11:05:58 +00:00
while [ $sub_menu -eq 1 ]; do
2023-10-11 10:48:57 +00:00
names=$(jj -i $workdir/preload -l 'data.#.name' | nl -s: -v0 -w1)
2022-04-05 20:37:09 +00:00
menu_videos_choice=$(echo "Main menu\n$names\nNext page" | fzy)
2022-04-05 11:05:58 +00:00
case $menu_videos_choice in
"Main menu") sub_menu=0 ;;
2022-04-05 20:37:09 +00:00
"Next page")
if [ "$1" = 'all' ]; then
page=$(expr $page + 100)
2022-04-18 14:12:22 +00:00
peertube_api_get_all_videos "$page"
2022-04-05 20:37:09 +00:00
else
2022-04-18 14:12:22 +00:00
page=$(expr "$page" + 100)
peertube_api_get_local_videos "$page"
2022-04-05 20:37:09 +00:00
fi ;;
2022-04-05 11:05:58 +00:00
*)
2022-04-18 14:12:22 +00:00
index=$(echo "$menu_videos_choice" | cut -f 1 -d:)
2023-10-11 10:48:57 +00:00
video_uuid=$(jj -i $workdir/preload data."$index".uuid)
2022-04-05 22:12:47 +00:00
if [ "$1" = 'lives' ]; then
2022-04-18 14:12:22 +00:00
peertube_menu_stream "$video_uuid"
2022-04-05 22:12:47 +00:00
else
2022-04-18 14:12:22 +00:00
peertube_menu_video "$video_uuid"
2022-04-05 22:12:47 +00:00
fi ;;
2022-04-05 11:05:58 +00:00
esac
done
}
peertube_menu_video()
{
clear
2022-04-05 11:05:58 +00:00
sub2_menu=1
2022-04-18 14:12:22 +00:00
get_video=$(peertube_api_get_video "$1")
2022-04-05 11:05:58 +00:00
while [ $sub2_menu -eq 1 ]; do
2022-04-18 14:12:22 +00:00
name=$(echo "$get_video" | jj name)
desc=$(echo "$get_video" | jj description)
channel=$(echo "$get_video" | jj channel.name)
support_author=$(echo "$get_video" | jj support)
if [ -n "$support_author" ]; then
echo "Support/Donate: $support_author"
fi
2022-04-18 14:12:22 +00:00
check_hls_empty=$(echo "$get_video" | jj streamingPlaylists.0)
if [ -z "$check_hls_empty" ]; then
2022-04-05 22:31:15 +00:00
hls=''
else
hls='streamingPlaylists.0.'
fi
2023-10-11 10:55:21 +00:00
video_url=$(echo "$get_video" | jj "$hls"files.\#[resolution.id="$pref_video_quality"].fileUrl)
torrent_url=$(echo "$get_video" | jj "$hls"files.\#[resolution.id="$pref_video_quality"].torrentUrl)
2022-04-05 11:05:58 +00:00
echo "Channel: $channel"
2022-04-06 11:34:07 +00:00
fulldescr=
if [ -n "$desc" ]; then
echo "Description video:\n$desc"
echo '_____'
fulldescr="\nFull description"
fi
2022-04-06 16:01:10 +00:00
menu_video_choice=$(echo "Play$fulldescr\nShare\nBack\nMain menu" | fzy)
2022-04-05 11:05:58 +00:00
case $menu_video_choice in
"Main menu") sub2_menu=0 && sub_menu=0 ;;
"Back") sub2_menu=0 ;;
"Play")
if [ -z "$video_url" ]; then
2022-04-05 17:36:11 +00:00
echo "Resolution $pref_video_quality"'p not avalaible'
2022-04-05 11:05:58 +00:00
echo 'Please choice:'
2023-10-11 10:55:21 +00:00
resolution=$(echo "$get_video" | jj -l "$hls"files.\#.resolution.label | fzy)
video_url=$(echo "$get_video" | jj "$hls"files.\#[resolution.label="$resolution"].fileUrl)
torrent_url=$(echo "$get_video" | jj "$hls"files.\#[resolution.label="$resolution"].torrentUrl)
2022-04-05 11:05:58 +00:00
fi
2022-04-05 17:36:11 +00:00
2022-04-05 16:16:13 +00:00
if [ "$torrent_enabled" = 'true' ]; then
2022-04-18 14:12:22 +00:00
transmission-remote $rpcport -a "$torrent_url"
curl -s --tcp-fastopen --output - "$video_url" | $default_player_command -
transmission-remote $rpcport -t 1 -rad
2022-04-05 16:16:13 +00:00
else
2022-04-18 14:12:22 +00:00
curl -s --tcp-fastopen --output - "$video_url" | $default_player_command -
2022-04-05 16:16:13 +00:00
fi ;;
2022-04-06 11:11:24 +00:00
"Full description")
2022-04-18 14:12:22 +00:00
fulldesc=$(peertube_api_get_fulldescription "$1")
2022-04-06 11:11:24 +00:00
echo "$fulldesc" | less -e ;;
2022-04-06 16:01:10 +00:00
"Share")
echo "Link: https://$instance/w/$1"
echo "Direct links:"
2023-10-11 10:55:21 +00:00
echo "$(echo "$get_video" | jj -l "$hls"files.\#.resolution.label)"
echo "$(echo "$get_video" | jj -l "$hls"files.\#.fileUrl)" ;;
2022-04-05 11:05:58 +00:00
esac
done
}
2022-04-05 22:12:47 +00:00
peertube_menu_stream()
{
clear
sub2_menu=1
2022-04-18 14:12:22 +00:00
get_video=$(peertube_api_get_video "$1")
2022-04-05 22:12:47 +00:00
while [ $sub2_menu -eq 1 ]; do
2022-04-18 14:12:22 +00:00
name=$(echo "$get_video" | jj name)
desc=$(echo "$get_video" | jj description)
channel=$(echo "$get_video" | jj channel.name)
support_author=$(echo "$get_video" | jj support)
if [ -n "$support_author" ]; then
echo "Support/Donate: $support_author"
fi
2022-04-18 14:12:22 +00:00
playlist_stream=$(echo "$get_video" | jj streamingPlaylists.0.playlistUrl)
state=$(echo "$get_video" | jj state.label)
2022-04-05 22:12:47 +00:00
echo "Status: $state"
#torrent_url=$(echo $get_video | jj streamingPlaylists.0.files.#[resolution.id=$pref_video_quality].torrentUrl)
echo "Channel: $channel"
2022-04-06 11:34:07 +00:00
fulldescr=
if [ -n "$desc" ]; then
echo "Description stream:\n$desc"
echo '_____'
fulldescr="\nFull description"
fi
2022-05-06 21:26:17 +00:00
menu_video_choice=$(echo "Play\nWait mode$fulldescr\nShare\nLive chat\nBack\nMain menu" | fzy)
2022-04-05 22:12:47 +00:00
case $menu_video_choice in
"Main menu") sub2_menu=0 && sub_menu=0 ;;
"Back") sub2_menu=0 ;;
2022-04-18 14:12:22 +00:00
"Play") $default_player_command "$playlist_stream" ;;
2022-04-06 00:25:25 +00:00
"Wait mode")
2022-04-18 14:12:22 +00:00
playlist_stream=$(echo "$get_video" | jj streamingPlaylists.0.playlistUrl)
2022-04-06 00:25:25 +00:00
echo 'Wait mode enabled...'
echo 'Stream will be played when published state'
2022-04-18 14:12:22 +00:00
while [ -z "$playlist_stream" ]; do
get_video=$(peertube_api_get_video "$1")
playlist_stream=$(echo "$get_video" | jj streamingPlaylists.0.playlistUrl)
2022-04-06 00:25:25 +00:00
wait=15
while [ $wait -gt 0 ]; do
date
sleep 1
wait=$(expr $wait - 1)
done
done
clear
echo 'Stream PUBLISHED!'
2022-04-18 14:12:22 +00:00
$default_player_command "$playlist_stream"
2022-04-06 00:25:25 +00:00
;;
2022-04-06 11:11:24 +00:00
"Full description")
2022-04-18 14:12:22 +00:00
fulldesc=$(peertube_api_get_fulldescription "$1")
2022-04-06 16:01:10 +00:00
echo "$fulldesc" | less -e ;;
"Share")
echo "Link: https://$instance/w/$1"
2022-04-06 17:20:25 +00:00
echo "Direct link: $playlist_stream" ;;
2022-05-06 21:26:17 +00:00
"Live chat") peertube_api_get_livechat "$(echo "$get_video" | jj uuid)" ;;
2022-04-05 22:12:47 +00:00
esac
done
}
2022-04-05 11:05:58 +00:00
menu_settings()
{
sub_menu=1
while [ $sub_menu -eq 1 ]; do
2022-04-05 16:16:13 +00:00
default_res="Default resolution (current: $pref_video_quality)"
torrent_set="Torrent (current: $torrent_enabled)"
set_name=$(echo "Main menu\n$default_res\n$torrent_set" | fzy)
case $set_name in
2022-04-05 11:05:58 +00:00
"Main menu") sub_menu=0 ;;
2022-04-05 16:16:13 +00:00
"$default_res")
2022-04-05 11:05:58 +00:00
resolution=$(echo '144\n240\n288\n480\n720\n1080' | fzy)
2023-10-11 10:48:57 +00:00
jj -i $workdir/config.json prefer_quality_video -v "$resolution" -o $workdir/config.json
2022-04-18 14:12:22 +00:00
export pref_video_quality="$resolution" ;;
2022-04-05 16:16:13 +00:00
"$torrent_set")
case $torrent_enabled in
'true')
2023-10-11 10:48:57 +00:00
jj -i $workdir/config.json torrent_enabled -v 'false' -o $workdir/config.json
2022-04-05 16:16:13 +00:00
export torrent_enabled='false'
transmission-remote $rpcport --exit ;;
2022-04-05 16:16:13 +00:00
'false')
2023-10-11 10:48:57 +00:00
jj -i $workdir/config.json torrent_enabled -v 'true' -o $workdir/config.json
2022-04-05 16:16:13 +00:00
export torrent_enabled='true'
torrent_init ;;
esac
2022-04-05 11:05:58 +00:00
esac
done
}
2022-04-06 13:23:40 +00:00
if [ -z $(check_connect) ]; then
echo 'Connect error...'
echo 'Please retry later or switch instance'
2022-04-06 18:48:54 +00:00
else
echo "Connected to $instance"
2022-04-06 13:23:40 +00:00
fi
version=$(peertube_api_version_server)
2022-04-05 16:16:13 +00:00
torrent_init
2022-04-05 11:05:58 +00:00
videosmenu='All Videos'
videosmenulocal='Local Videos'
2022-04-05 22:12:47 +00:00
streamsmenu='Live streams'
2022-04-12 20:15:14 +00:00
manualinput='Manual URL'
2022-04-05 11:05:58 +00:00
changepod='Switch instance'
settings='Settings'
checkapilimits='Check API limits (debug)'
Exit='Exit'
while true; do
2022-04-12 20:15:14 +00:00
choice=$(echo "$videosmenu\n$videosmenulocal\n$streamsmenu\n$manualinput\n$changepod\n$settings\n$checkapilimits\n$Exit" | fzy)
2022-04-05 11:05:58 +00:00
case "$choice" in
"$videosmenu")
2022-04-05 20:37:09 +00:00
peertube_api_get_all_videos 0
peertube_menu_videos 'all' ;;
2022-04-05 11:05:58 +00:00
"$videosmenulocal")
2022-04-05 20:37:09 +00:00
peertube_api_get_local_videos 0
peertube_menu_videos 'local' ;;
2022-04-05 22:12:47 +00:00
"$streamsmenu")
peertube_api_get_live_streams 0
peertube_menu_videos 'lives' ;;
2022-04-12 20:15:14 +00:00
"$manualinput")
echo 'Type'
type_choice=$(echo "Video\nStream" | fzy)
echo 'Input link:'
read url
2022-04-18 14:12:22 +00:00
url=$(echo "$url" | rev | cut -f1 -d/ | rev)
2022-04-12 20:15:14 +00:00
case $type_choice in
2022-04-18 14:12:22 +00:00
"Video") peertube_menu_video "$url" ;;
"Stream") peertube_menu_stream "$url" ;;
2022-04-12 20:15:14 +00:00
esac
;;
2022-04-05 11:05:58 +00:00
"$changepod")
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 ;;
2023-10-11 10:48:57 +00:00
"Choice from list") instance=$(jj -l -i $workdir/config.json public_list_instances | sed 's/"//g' | fzy) ;;
2022-04-05 11:05:58 +00:00
"Manual input") echo "Type instance (ex. $instance):" && read instance ;;
esac
if [ $empty -eq 0 ]; then
2022-04-18 14:12:22 +00:00
echo "$instance" >> $instance_hist
2022-04-05 11:05:58 +00:00
cat $instance_hist | sort | uniq | tee $instance_hist 1>>/dev/null
export instance
export instance_point="https://$instance/api/v1"
2022-04-06 13:23:40 +00:00
if [ -z $(check_connect) ]; then
echo 'Connect error...'
echo 'Please retry later or switch instance'
2022-04-06 18:48:54 +00:00
else
echo "Connected to $instance"
2022-04-06 13:23:40 +00:00
fi
export version=$(peertube_api_version_server)
2022-04-05 11:05:58 +00:00
conf_instance_state=$(echo 'Permanent\nTemporaly' | fzy)
if [ "$conf_instance_state" = 'Permanent' ]; then
2023-10-11 10:48:57 +00:00
jj -i $workdir/config.json instance -v "$instance" -o $workdir/config.json
2022-04-05 11:05:58 +00:00
else
echo ''
fi
clear
fi ;;
"$settings") menu_settings ;;
"$checkapilimits") peertube_api_check_ratelimit ;;
2022-04-05 16:16:13 +00:00
"$Exit")
if [ $torrent_enabled = 'true' ]; then transmission-remote $rpcport --exit; fi
2022-04-05 16:16:13 +00:00
exit 0 ;;
2022-04-05 11:05:58 +00:00
esac
done