coffeeAnon

Gelbooru download by tags

Jan 1st, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.43 KB | None | 0 0
  1. #!/bin/bash
  2. # Gelbooru parser
  3. # Download pictures from http://gelbooru.com by tags. Original script from https://pastebin.com/TXGfZFNX , modified to actually work again
  4. # Usage: ./gelbooru.sh [tags separated by +] [folder for pictures, if left empty creats a folder named after the tags]
  5. # Depends: bash, wget, xmlstarlet
  6. # License: Public Domain, http://creativecommons.org/publicdomain/mark/1.0/
  7.  
  8. GR='\033[38;5;82m' ; PN='\033[38;5;171m' ; BL='\033[38;5;27m' ; RD='\033[0;31m' ; PR='\033[0;35m' ; CY='\033[0;36m' ; NC='\033[0m'
  9. # ${}
  10.  
  11. # Check for parameters
  12. if [[ ! $1 || "$1" == "-h" || "$1" == "--help" ]]; then
  13.         echo -e "${CY}Gelbooru parser${NC}\nDownload pictures from http://gelbooru.com by tags\nUsage: ./${CY}gelbooru.sh${NC} ${BL}[tags separated by +]${NC} ${PR}[OutputDir, if left empty, creates a folder named after the tags]${NC}"
  14.         exit
  15. fi
  16. # Is wget installed?
  17. if [[ ! $(wget -V) ]]; then
  18.         echo "This script requires wget. Please read the manual for your distro and install it."
  19.         exit
  20. fi
  21. # Is xmlstarlet installed?
  22. if [[ ! $(xmlstarlet --version) ]]; then
  23.         echo "This script requires xmlstarlet. Please read the manual for your distro and install it."
  24.         exit
  25. fi
  26. # Are GelBooru cookies available?
  27. cookies=GelBooruCookies.txt
  28. if [[ ! -f "$cookies" ]]; then
  29.         printf "gelbooru.com%s\tFALSE%s\t/%s\tFALSE%s\t0%s\tfringeBenefits%s\tyup" > GelBooruCookies.txt
  30.         echo -e "\nprinted cookies"
  31. fi
  32. # Adjust folder name
  33. folder="$2"
  34. if [[ ! $folder ]]; then
  35.         folder=$(echo "$1" | sed -e 's/+/\ /')
  36. fi
  37. # Get count of pages and images
  38. pagecount="0"
  39. tags=$(echo "$1" | sed -e 's/ /%20/g')
  40. count=$(wget -q -O - --load-cookies ../GelBooruCookies.txt "http://gelbooru.com/index.php?page=dapi&s=post&q=index&tags=$tags" | xmlstarlet sel -t -v /posts/@count[1])
  41. # Ask for confirmation
  42. echo -e "\nQuery${BL} \"$1\" ${NC}containing${CY} $count ${NC}Files will be saved to ${PR}"$folder"${NC}"
  43. read -p $'press [enter] to start download, ctrl+C to cancel\x0a'
  44. # Create folder
  45. if [[ ! -d "$folder" ]]; then
  46.         mkdir "$folder"
  47. fi
  48. cd "$folder"
  49. # Download
  50. while [ "$count" -ge "0" ] ; do
  51.         url=$(wget -nv -O - --load-cookies ../GelBooruCookies.txt "http://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=100&pid=$pagecount&tags=$tags" | xmlstarlet sel -t -m /posts/post/@file_url -v "." -n$)
  52.                 wget -nc $url
  53.        
  54.         let count=count-100
  55.         let pagecount++
  56. done
Add Comment
Please, Sign In to add comment