123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/usr/bin/env bash
- INSTANCE=""
- OUTFILE=playlist.json
- TOKEN=""
- while getopts "i:o:t:" arg; do
- case $arg in
- i)
- INSTANCE=${OPTARG}
- ;;
- o)
- OUTFILE=${OPTARG}
- ;;
- t)
- TOKEN=${OPTARG}
- ;;
- esac
- done
- shift $((OPTIND-1))
- if [ -z "${INSTANCE}" ]; then
- echo No instance given
- exit 1
- fi
- if [ -z "${TOKEN}" ]; then
- echo No token given
- exit 1
- fi
- if [ "$(host ${INSTANCE}|grep "not found"|wc -l)" -lt 0 ]; then
- echo Instance ${INSTANCE} has no DNS entry
- exit 1
- fi
- if [ "$(curl -s -X 'GET' https://${INSTANCE}/api/v1/instance/nodeinfo/2.0/ -H 'accept: application/json' |jq -r '.software.name')" != "funkwhale" ]; then
- echo Instance ${INSTANCE} seems not to be a funkwhale instance
- exit 1
- fi
- for plid in $(find . -name "*pl.json"|xargs -n1 jq -r '.|[.name,.id]|@csv' |sort|cut -d',' -f2); do
- plname="$(jq -r '.name' ${plid}.pl.json)"
- plurl=$(printf https://${INSTANCE}/api/v1/playlists/?name=${plname} | jq --slurp --raw-input --raw-output @uri)
- answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plname} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
- echo ${answer}|jq -r '.count'
- if [ "$(echo ${answer}|jq -r '.count')" -eq 0 ]; then
- valuejson=$(jq -c --null-input --arg plname "${plname}" '{"name":$plname,"privacy_level":"instance"}')
- curl -s -X 'POST' https://${INSTANCE}/api/v1/playlists/ -H 'Content-Type: application/json' -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN} -d "${valuejson}"
- fi
- nplid=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plname} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
- echo ${nplid}|jq -r '.id'
- while IFS='#' read mbid title; do
- answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/tracks/?mbid=${mbid} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
- stitle=$(echo ${answer}|jq -r '.results[0].title')
- id=$(echo ${answer}|jq -r '.results[0].id')
- echo ${mbid} - ${title} - ${stitle} - ${id}
- done <<<$(jq -r '.results[].track|[.mbid,.title]|@csv' ${plid}.tracks.json |sed -e 's/","/"#"/g'|tr -d '"')
- done
|