import_playlist.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env bash
  2. INSTANCE=""
  3. OUTFILE=playlist.json
  4. TOKEN=""
  5. while getopts "i:o:t:" arg; do
  6. case $arg in
  7. i)
  8. INSTANCE=${OPTARG}
  9. ;;
  10. o)
  11. OUTFILE=${OPTARG}
  12. ;;
  13. t)
  14. TOKEN=${OPTARG}
  15. ;;
  16. esac
  17. done
  18. shift $((OPTIND-1))
  19. if [ -z "${INSTANCE}" ]; then
  20. echo No instance given
  21. exit 1
  22. fi
  23. if [ -z "${TOKEN}" ]; then
  24. echo No token given
  25. exit 1
  26. fi
  27. if [ "$(host ${INSTANCE}|grep "not found"|wc -l)" -lt 0 ]; then
  28. echo Instance ${INSTANCE} has no DNS entry
  29. exit 1
  30. fi
  31. if [ "$(curl -s -X 'GET' https://${INSTANCE}/api/v1/instance/nodeinfo/2.0/ -H 'accept: application/json' |jq -r '.software.name')" != "funkwhale" ]; then
  32. echo Instance ${INSTANCE} seems not to be a funkwhale instance
  33. exit 1
  34. fi
  35. for plid in $(find . -name "*pl.json"|xargs -n1 jq -r '.|[.name,.id]|@csv' |sort|cut -d',' -f2); do
  36. plname="$(jq -r '.name' ${plid}.pl.json)"
  37. plurl=$(printf https://${INSTANCE}/api/v1/playlists/?name=${plname} | jq --slurp --raw-input --raw-output @uri)
  38. answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plname} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
  39. echo ${answer}|jq -r '.count'
  40. if [ "$(echo ${answer}|jq -r '.count')" -eq 0 ]; then
  41. valuejson=$(jq -c --null-input --arg plname "${plname}" '{"name":$plname,"privacy_level":"instance"}')
  42. curl -s -X 'POST' https://${INSTANCE}/api/v1/playlists/ -H 'Content-Type: application/json' -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN} -d "${valuejson}"
  43. fi
  44. nplid=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plname} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
  45. echo ${nplid}|jq -r '.id'
  46. while IFS='#' read mbid title; do
  47. answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/tracks/?mbid=${mbid} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
  48. stitle=$(echo ${answer}|jq -r '.results[0].title')
  49. id=$(echo ${answer}|jq -r '.results[0].id')
  50. echo ${mbid} - ${title} - ${stitle} - ${id}
  51. done <<<$(jq -r '.results[].track|[.mbid,.title]|@csv' ${plid}.tracks.json |sed -e 's/","/"#"/g'|tr -d '"')
  52. done