update_skins_db.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. ####
  3. # Licenced under Attribution-NonCommercial-ShareAlike 4.0 International
  4. # http://creativecommons.org/licenses/by-nc-sa/4.0/
  5. #### ATTENTION ####
  6. ## This script requires that jq and coreutils are installed on your system ##
  7. ## In Debian-based distros, open a terminal and run
  8. ## sudo apt-get install jq coreutils
  9. ###################
  10. # == Set variables ===
  11. # ====================
  12. NUMPAGES="1" # Number of pages. Default is 1 page
  13. PERPAGE="2000" # Number of items per page. Default is 2000.
  14. JSONURL="http://minetest.fensta.bplaced.net/api/get.json.php?getlist&page=$NUMPAGES&outformat=base64&per_page=$PERPAGE" # The URL to the database
  15. PREVIEWURL="http://minetest.fensta.bplaced.net/skins/1/" # The url to the location of the previews.
  16. curpath="$(dirname $0)" # all path are relative to this script place
  17. temp="$curpath"/tmp # Where the temp folder will be. Default is $PWD/tmp, which means that the tmp folder will be put in the current folder
  18. METADEST="$curpath"/../meta # This is the folder where the meta data will be saved
  19. TEXTUREDEST="$curpath"/../textures # This is the folder where the skins and the previews will be saved
  20. # === Make a bunch of folders and download the db ===
  21. # ===================================================
  22. if [ -d "$temp" ]; then
  23. rm -r $temp # If the temp dir exists we will remove it and its contents.
  24. fi
  25. mkdir "$temp" # Make a new temp dir. Redundant? No. We will get rid of it later.
  26. if [ ! -d "$METADEST" ]; then # Check to see if the meta dir exists, and if not, create it
  27. mkdir "$METADEST"
  28. fi
  29. if [ ! -d "$TEXTUREDEST" ]; then # Check to see if the textures dir exists, and if not, create it
  30. mkdir "$TEXTUREDEST"
  31. fi
  32. wget "$JSONURL" -O "$temp"/rawdb.txt # Download the entire database
  33. # === Do the JSON thing ===
  34. # =========================
  35. i="0" # This will be the counter.
  36. while true; do
  37. ID=$(cat "$temp"/rawdb.txt | jq ".skins[$i].id")
  38. if [ "$ID" == "null" ]; then
  39. break
  40. fi
  41. if [ ! -f "$METADEST"/character_$ID.txt ] || [ "$1" == "all" ]; then
  42. # The next lines are kinda complex. sed is being used to strip the quotes from the variables. I had help...
  43. meta_name="$(jq ".skins[$i].name" < "$temp"/rawdb.txt | sed 's/^"//;s/"$//')"
  44. meta_author="$(jq ".skins[$i].author" <"$temp"/rawdb.txt | sed 's/^"//;s/"$//')"
  45. meta_license="$(jq ".skins[$i].license" <"$temp"/rawdb.txt | sed 's/^"//;s/"$//')"
  46. echo "# $ID name: $meta_name author: $meta_author license: $meta_license" # Verbosity to show that the script is working.
  47. echo "$meta_name" > "$METADEST"/character_$ID.txt # Save the meta data to files, this line overwrites the data inside the file
  48. echo "$meta_author" >> "$METADEST"/character_$ID.txt # Save the meta data to files, this line is added to the file
  49. echo "$meta_license" >> "$METADEST"/character_$ID.txt # Save the meta data to files, and this line is added to the file as well.
  50. # === Extract and save the image from the JSON file ===
  51. # ======================================================
  52. skin=$(jq ".skins[$i].img" < "$temp"/rawdb.txt | sed 's/^"//;s/"$//') # Strip the quotes from the base64 encoded string
  53. echo "$skin" | base64 --decode > "$TEXTUREDEST"/character_"$ID".png # Decode the string, and save it as a .png file
  54. # === Download a preview image whilst we're at it ===
  55. # ====================================================
  56. wget -nv "$PREVIEWURL/$ID".png -O "$TEXTUREDEST"/character_"$ID"_preview.png # Downloads a preview of the skin that we just saved.
  57. else
  58. echo -n "."
  59. fi
  60. i=$[$i+1] # Increase the counter by one.
  61. done
  62. # === Now we'll clean up the mess ===
  63. # ===================================
  64. rm -r "$temp" # Remove the temp dir and its contents.
  65. exit # Not strictly needed, but i like to use it to wrap things up.