funkwlmpv/funkwhale-cli.sh

175 lines
4.8 KiB
Bash
Executable File

#!/bin/sh
instance=$(jj -i config.json instance)
instance_hist='instance.hist'
ordering='title'
default_player_command='mpv --no-vid --no-ytdl --network-timeout=30'
instance_point="https://$instance/api/v1"
funkwhale_api_check_api_limits()
{
curl -s --compressed "$instance_point/rate-limit/" | jj -p
}
funkwhale_api_get_tracks()
{
get_json=$(curl -s --compressed "$instance_point/tracks?ordering=$ordering&playable=true&page=$1&tag=$tag" 2>&1 | tee preload)
jj -i preload -l 'results.#.uploads.0.listen_url' | sed 's/"//g'
}
funkwhale_api_get_tracks_from_channel()
{
sub2_menu=1
track_list=$(curl -s --compressed "$instance_point/tracks?channel=$1&playable=true&include_channels=true")
echo "Loaded $(echo $track_list | jj count) podcasts"
while [ $sub2_menu -eq 1 ]; do
titles=$(echo $track_list | jj -l 'results.#.title')
menu_podcast_choice=$(echo "Main menu\nBack\n$titles" | fzy)
case $menu_podcast_choice in
"Main menu") sub2_menu=0 && sub_menu=0 ;;
"Back") sub2_menu=0 ;;
*)
play_track=$(echo $track_list | jj results.#[title="$menu_podcast_choice"].listen_url)
$default_player_command "https://$instance$play_track" ;;
esac
done
}
funkwhale_get_podcasts_artists()
{
sub_menu=1
echo 'Loading podcast artists...'
curl -s --compressed --output preload "$instance_point/artists?ordering=-creation_date&playable=true&include_channels=true&content_category=podcast&page=$1"
while [ $sub_menu -eq 1 ]; do
count_artists=$(jj -i preload count)
echo "$count_artists avalaible"
podcast_names=$(jj -l -i preload results.#.name)
menu_podcast_choice=$(echo "Main menu\n$podcast_names" | fzy)
case $menu_podcast_choice in
"Main menu") sub_menu=0 ;;
*)
channel_uuid=$(jj -i preload results.#[name="$menu_podcast_choice"].channel.uuid)
if [ -z $channel_uuid ]; then
echo 'Error when loading podcast'
else
funkwhale_api_get_tracks_from_channel $channel_uuid
fi ;;
esac
done
}
load_tracks_to_playlist()
{
echo "Loading page $1 ..."
echo '#EXTM3U\n' > playlist.m3u8
counter_titles=0
for i in $(funkwhale_api_get_tracks $1); do
title=$(jj -i preload results."$counter_titles".title)
artist_name=$(jj -i preload results."$counter_titles".artist.name)
duration=$(jj -i preload results."$counter_titles".uploads.0.duration)
echo "#EXTINF:$duration, $artist_name - $title" >> playlist.m3u8
counter_titles=$(expr $counter_titles + 1)
echo "https://$instance$i\n" >> playlist.m3u8
done
count_tracks_results=$(jj -i preload 'results.#')
if [ -z $count_tracks_results ] || [ $count_tracks_results -eq 0 ]; then
echo 'Error: Page empty or connection error'
else
echo "Loaded $count_tracks_results tracks"
fi
}
get_all_avalaible_count_tracks()
{
funkwhale_api_get_tracks 1 1>> /dev/null
count_all_tracks=$(jj -i preload -l count)
if [ -z $count_all_tracks ]; then
echo 'Error: Connection error or API limit expired'
elif [ $count_all_tracks -eq 0 ]; then
echo 'Error: No tracks'
else
echo "Tracks avalaible on $instance: $count_all_tracks\n"
fi
}
get_all_avalaible_count_tracks
downloadtrackspls='Download tracks in playlist'
startplayer='Start player'
podcasts='Podcasts'
changepod='Switch instance'
checkapilimits='Check API limits (debug)'
Exit='Exit'
while true; do
choice=$(echo "$downloadtrackspls\n$startplayer\n$podcasts\n$changepod\n$checkapilimits\n$Exit" | fzy)
case "$choice" in
"$downloadtrackspls")
echo 'Order by (prefix - is DESC ordering):'
ordering=$(echo 'title\n-title\ncreation_date\n-creation_date\nrelease_date\n-release_date\nrandom' | fzy)
export ordering
echo 'Tags:'
tag=$(jj -l -i config.json tags | sed 's/"//g' | fzy)
if [ "$tag" = "TagOFF" ]; then
tag=
fi
export tag
echo 'Enter page number: '
read page
load_tracks_to_playlist $page ;;
"$startplayer")
$default_player_command playlist.m3u8 ;;
"$podcasts")
echo 'Enter page number: '
read page
funkwhale_get_podcasts_artists $page ;;
"$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 ;;
"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
get_all_avalaible_count_tracks
fi ;;
"$checkapilimits")
funkwhale_api_check_api_limits | more ;;
"$Exit")
exit 0 ;;
esac
done