Added auth mode

This commit is contained in:
localhost_frssoft 2022-04-07 22:05:49 +03:00
parent 52b27009e8
commit 125c76bbed

View File

@ -4,22 +4,38 @@ 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"
touch .auth.json
chmod 600 .auth.json
auth="$(jj -i .auth.json "$(echo $instance | sed 's/\./\\\./g')")"
if [ -n "$auth" ]; then
default_curl_opt()
{
curl -s --compressed -H "Authorization: Bearer $auth" $1
}
listen_token=$(default_curl_opt "$instance_point/users/me" | jj tokens.listen)
echo '+Authorized account+'
else
default_curl_opt()
{
curl -s --compressed $1
}
fi
funkwhale_api_check_api_limits()
{
curl -s --compressed "$instance_point/rate-limit/" | jj -p
default_curl_opt "$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)
get_json=$(default_curl_opt "$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_albums()
{
get_json=$(curl -s --compressed "$instance_point/albums?ordering=$ordering&playable=true&page=$1" 2>&1 | tee preload)
get_json=$(default_curl_opt "$instance_point/albums?ordering=$ordering&playable=true&page=$1" 2>&1 | tee preload)
}
funkwhale_menu_albums()
@ -51,7 +67,7 @@ funkwhale_menu_albums_tracks()
level=1
fi
tracks_a=$(curl -s --compressed "$instance_point/tracks?ordering=$ordering&playable=true&album=$1&page_size=50")
tracks_a=$(default_curl_opt "$instance_point/tracks?ordering=$ordering&playable=true&album=$1&page_size=50")
while [ $sub2_menu -eq $level ]; do
@ -69,27 +85,36 @@ funkwhale_menu_albums_tracks()
"Listen all")
echo > playlist.m3u8
chmod 600 playlist.m3u8
for i in $playlist; do
echo "https://$instance$i\n" >> playlist.m3u8
if [ -n "$auth" ]; then
echo "https://$instance$i?token=$listen_token\n" >> playlist.m3u8
else
echo "https://$instance$i\n" >> playlist.m3u8
fi
done
$default_player_command playlist.m3u8 ;;
*)
index=$(echo $menu_album_tracks_choice | cut -f 1 -d:)
play_track=$(echo $tracks_a | jj results.$index.listen_url)
$default_player_command "https://$instance$play_track" ;;
if [ -n "$auth" ]; then
$default_player_command "https://$instance$play_track?token=$listen_token"
else
$default_player_command "https://$instance$play_track"
fi ;;
esac
done
}
funkwhale_api_get_artists()
{
curl -s --compressed "$instance_point/artists?ordering=-creation_date&playable=true&page=$1"
default_curl_opt "$instance_point/artists?ordering=-creation_date&playable=true&page=$1"
}
funkwhale_api_get_artist()
{
curl -s --compressed "$instance_point/artists/$1"
default_curl_opt "$instance_point/artists/$1"
}
funkwhale_menu_artists()
@ -137,7 +162,7 @@ funkwhale_menu_albums_artist()
funkwhale_api_get_tracks_from_channel()
{
sub2_menu=1
track_list=$(curl -s --compressed "$instance_point/tracks?channel=$1&playable=true&include_channels=true")
track_list=$(default_curl_opt "$instance_point/tracks?channel=$1&playable=true&include_channels=true")
echo "Loaded $(echo $track_list | jj count) podcasts"
while [ $sub2_menu -eq 1 ]; do
@ -149,7 +174,11 @@ funkwhale_api_get_tracks_from_channel()
*)
index=$(echo $menu_podcast_choice | cut -f 1 -d:)
play_track=$(echo $track_list | jj results.$index.listen_url)
$default_player_command "https://$instance$play_track" ;;
if [ -n "$auth" ]; then
$default_player_command "https://$instance$play_track&token=$listen_token"
else
$default_player_command "https://$instance$play_track"
fi ;;
esac
done
}
@ -158,7 +187,7 @@ 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"
default_curl_opt "$instance_point/artists?ordering=-creation_date&playable=true&include_channels=true&content_category=podcast&page=$1" | tee preload 1>> /dev/null
while [ $sub_menu -eq 1 ]; do
count_artists=$(jj -i preload count)
echo "$count_artists avalaible"
@ -184,6 +213,7 @@ load_tracks_to_playlist()
{
echo "Loading page $1 ..."
echo '#EXTM3U\n' > playlist.m3u8
chmod 600 playlist.m3u8
counter_titles=0
for i in $(funkwhale_api_get_tracks $1); do
title=$(jj -i preload results."$counter_titles".title)
@ -191,7 +221,11 @@ load_tracks_to_playlist()
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
if [ -n "$auth" ]; then
echo "https://$instance$i&token=$listen_token\n" >> playlist.m3u8
else
echo "https://$instance$i\n" >> playlist.m3u8
fi
done
count_tracks_results=$(jj -i preload 'results.#')
if [ -z $count_tracks_results ] || [ $count_tracks_results -eq 0 ]; then