#!/bin/bash

#https://e621.net/

#by: /u/BASH_SCRIPTS_FOR_YOU

#GPLv3 or later

SITE="https://e621.net/post/index/"

one=${1}

two=${2}

three=${3}

four=${4}

five=${5}

if [ -z "${one}" ]

then one=0

fi

if [ "${one}" == 2 ]

then two=0

three=0

four=0

five=0

fi

if [ -z "${two}" ]

then two=0

fi

if [ -z "${three}" ]

then three=0

fi

if [ -z "${four}" ]

then four=0

fi

if [ -z "${five}" ]

then five=0

fi

if [ "${one}" == 0 ] && [ "${two}" == 0 ] && [ "${three}" == 0 ] && [ "${four}" == 0 ] && [ "${five}" == 0 ]

then echo "You have not enter any arguments for this script"

echo "there are 5 arguments, like so: ./script.sh 1 1 1 1 1"

echo "the arguments are either 1 or 0, 1 is to do the action, 0 is to not"

echo "except the first argument, it has the option of 2, more on that later"

echo "first argument runs the site downloader. It'll output a database of info"

echo "every argument give you a blank line to enter tags, seperated by spaces"

echo "Put a negative infront of a tag to exclude"

echo "second argument creates a list of links from the database, this is useful for downloaders."

echo "for example, wget, to download them, like so"

echo "~\$wget -i filename.txt"

echo "third argument creates a list of tags from the database"

echo "forth argument creates a list of artists from the database"

echo "fifth argument downloads the lists of links from the second argument"

echo "first argument also has the option of 2, which lists the amount of pages, content, and popular tags"

echo "if 2 is used on the first argument, all other arguments will ignored, set to 0"

echo "if an argument is not given a value it gets turned into 0"

echo "by running this script with no arguments, it was assume to be 0 0 0 0 0"

echo "which displays this message"

echo "any combination except 0 0 0 0 0 will give you a blank line for tags, so it can find the right database file"

echo "a line in any list refernces the same line in other lists"

echo "EG: line 3 in links, Artists, Tags, and Database all refer to the same image"

echo "this script uses cat, awk, curl, sed, grep, printf, and echo, incase you don't have them installed"

echo "happy yiffing"

else read TAGS

fi

if [ "${one}" == 2 ]

then PAGE=0

PAGES=`curl --retry 8 -s -g "${SITE}1/${TAGS}" | sed '35!d' | tr '/' '

' | grep ^[1-9]`

if [ -z "${PAGES}" ]

then PAGES=1

fi

printf ""${PAGES}" pages. "

lastpagecontent=`curl --retry 8 -s -g "${SITE}${PAGES}/${TAGS}" | grep -c 'Post.register({"sample_url":'`

content=$(((((( ${PAGES} - 1 )) * 75 )) + ${lastpagecontent} ))

printf ""${content}" media

"

curl -s -g "${SITE}${PAGES}/${TAGS}" | grep '^<li class="' | sed 's/–/

/g' | sed '1d' | tr '<' '>' | awk -F'>' '{ print $5 "\t" $9 }'

fi

if [ "${one}" == 1 ] && [ -f "DB:${TAGS}.txt" ]

then printf "

"DB:${TAGS}.txt" already exists

"

fi

if [ "${one}" == 1 ] && [ ! -f "DB:${TAGS}.txt" ]

then PAGE=0

PAGES=`curl --retry 8 -s -g "${SITE}1/${TAGS}" | sed '35!d' | tr '/' '

' | grep ^[1-9]`

if [ -z "${PAGES}" ]

then PAGES=1

fi

printf "${PAGES} pages. "

lastpagecontent=`curl --retry 8 -s -g "${SITE}${PAGES}/${TAGS}" | grep -c 'Post.register({"sample_url":'`

content=$(((((( ${PAGES} - 1 )) * 75 )) + ${lastpagecontent} ))

printf ""${content}" media. Page"

while [ "${PAGE}" != "${PAGES}" ]

do PAGE=$(( ${PAGE} + 1 ))

printf ",${PAGE}"

curl -s -g "${SITE}${PAGE}/${TAGS}" | grep 'Post.register({"sample_url":' >>"DB:${TAGS}.txt"

done

fi

if [ "${two}" == 1 ] && [ -f "DB:${TAGS}.txt" ]

then sed 's/.*"file_url":"//g' "DB:${TAGS}.txt" | sed 's/....$//g' >> "Links:${TAGS}.txt"

fi

if [ "${two}" == 1 ] && [ ! -f "DB:${TAGS}.txt" ]

then printf ""DB:${TAGS}.txt" does not exist

"

fi

if [ "${three}" == 1 ] && [ -f "DB:${TAGS}.txt" ]

then awk -F'"' '{ print $10 }' "DB:${TAGS}.txt" >> "Tags:${TAGS}.txt"

fi

if [ "${three}" == 1 ] && [ ! -f "DB:${TAGS}.txt" ]

then printf ""DB:${TAGS}.txt" does not exist

"

fi

if [ "${four}" == 1 ] && [ -f "DB:${TAGS}.txt" ]

then sed "s/.*"artist":\[//g" "DB:${TAGS}.txt" | sed 's/\],"has_notes":.*//g' | sed 's/"//g' | sed "s/,/ /g" >> "Artists:${TAGS}.txt"

fi

if [ "${four}" == 1 ] && [ ! -f "DB:${TAGS}.txt" ]

then printf ""DB:${TAGS}.txt" does not exist

"

fi

if [ "${five}" == 1 ] && [ -f "Links:${TAGS}.txt" ]

then lines=0

linenumber=`cat "Links:${TAGS}.txt" | wc -l`

while [ "${lines}" != "${linenumber}" ]

do lines=$(( ${lines} + 1 ))

printf ""${lines}" of "${linenumber}"

"

sed -n "${lines}{p;q;}" "Links:${TAGS}.txt" | xargs curl -g -# -O

done

fi

if [ "${five}" == 1 ] && [ ! -f "Links:${TAGS}.txt" ]

then

printf ""Links:${TAGS}.txt" does not exist

"