Guest User

Untitled

a guest
May 3rd, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.39 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Set admin password here
  4. password=""
  5. password_hash=$(echo -ne "choujin-steiner--$password--" | sha1sum | awk '{print $1}')
  6.  
  7.  
  8. SCRIPT_DIR=/srv/auto_tagger
  9. cd $SCRIPT_DIR
  10.  
  11. sleep 1
  12.  
  13. # Loop through spool/ content
  14. while true ; do
  15.     oldest_file="$(find spool -mindepth 1 -type f | sort -n | head -n 1)"
  16.  
  17.     # if nothing to process, exit
  18.     if [ -z "${oldest_file}" ]; then
  19.         rm /tmp/deepdanbooru_thread.lock
  20.         break
  21.     fi
  22.  
  23.     image_id=$(basename "$oldest_file")
  24.  
  25.     # Break if booru is not running
  26.     if ! curl "127.0.0.1:8080/post.json" --no-progress-meter > /dev/null ; then
  27.         rm /tmp/deepdanbooru_thread.lock
  28.         break
  29.     fi
  30.  
  31.     # Get page of post
  32.     max_id=$(curl 127.0.0.1:8080/post.json --no-progress-meter | jq -r '.[0] | .id')
  33.     dp=$(($max_id-$image_id))
  34.     page=$(($dp/40+1))
  35.  
  36.     # Get image tags
  37.     curl "127.0.0.1:8080/post.json?page=$page" --no-progress-meter | jq -r ".[] | select(.id==$image_id) | .tags + \" \""" > /tmp/autotagging_image_tags.txt
  38.    # Check if image contains 'auto_tagged' tag
  39.  
  40.    if cat /tmp/autotagging_image_tags.txt | grep -qF 'auto_tagged' ; then
  41.        # Strip auto_tagged and continue
  42.        sed -i 's/\<auto_tagged\>//g' /tmp/autotagging_image_tags.txt
  43.  
  44.        curl "127.0.0.1:8080/post/update.xml" \
  45.            --no-progress-meter \
  46.            -H "Expect:" \
  47.            -F 'login=admin' \
  48.            -F "password_hash=$password_hash" \
  49.            -F "id=$image_id" \
  50.            -F "post[tags]=</tmp/autotagging_image_tags.txt"
  51.    else
  52.        # Start deepdanbooru processing
  53.        image_path=$(cat "$oldest_file")
  54.        ./add_deepdanbooru_tags.sh "$image_path" /tmp/autotagging_image_tags.txt
  55.  
  56.        # Break if booru is not running
  57.        if ! curl "127.0.0.1:8080/post.json" --no-progress-meter > /dev/null ; then
  58.            rm /tmp/deepdanbooru_thread.lock
  59.            break
  60.        fi
  61.  
  62.        # Update tags
  63.        curl "127.0.0.1:8080/post/update.xml" \
  64.                        --no-progress-meter \
  65.                        -H "Expect:" \
  66.                        -F 'login=admin' \
  67.                        -F "password_hash=$password_hash" \
  68.                        -F "id=$image_id" \
  69.                        -F "post[tags]=</tmp/autotagging_image_tags.txt"
  70.        sleep 1
  71.    fi
  72.  
  73.    rm /tmp/autotagging_image_tags.txt
  74.    rm $oldest_file
  75. done
  76.  
Add Comment
Please, Sign In to add comment