Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- scriptname=$( basename "$0" )
- is_running=$( pgrep -c "$scriptname" )
- if (( $is_running > 1 )) ; then
- >&2 echo $scriptname is running.
- exit 0
- fi
- if [ -z $1 ]
- then purity=100 #safe for work
- LOCATION="/path/to/dir/"
- else purity=011 # nsfr
- LOCATION="/path/to/dir/"
- fi
- if [ ! -d "$LOCATION" ] ; then mkdir -p "$LOCATION" ; fi
- cd "$LOCATION" || exit
- function WGET {
- userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) "
- userAgent+="Gecko/20100101 Firefox/48.0"
- wget -q -U "$userAgent" --keep-session-cookies \
- --save-cookies=cookies.txt --load-cookies=cookies.txt "$@"
- }
- # login :
- USER="wallhaven user name"
- PASS='*******wallhaven password'
- WGET --referer=https://alpha.wallhaven.cc https://alpha.wallhaven.cc/auth/login
- token="$(grep 'name="_token"' login | sed 's:.*value="::' | sed 's/.\{2\}$//')"
- WGET --referer=https://alpha.wallhaven.cc/auth/login \
- --post-data="_token=$token&username=$USER&password=$PASS" \
- https://alpha.wallhaven.cc/auth/login
- #query url
- s1="search?page=1&categories=101&purity=$purity&"
- s1+="resolutions=1920x1080&ratios=&sorting=random&order=desc"
- #get page
- WGET --referer=https://alpha.wallhaven.cc -O tmp "https://alpha.wallhaven.cc/$s1"
- #get image id and download it
- imgID="$(grep -o 'th-[0-9]*' tmp | sed 's .\{3\} ' | head -1)"
- ! WGET --referer=https://alpha.wallhaven.cc/wallpaper/"$imgID" \
- https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-$imgID.jpg &&
- ! WGET --referer=https://alpha.wallhaven.cc/wallpaper/"$imgID" \
- https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-$imgID.png
- rm -f cookies.txt login login.1 tmp
- data=$(stat "$PWD/wallhaven-$imgID.jpg" 2>/dev/null)
- filesize=$(echo "$data" | awk 'NR==2{print $2}')
- if [ -z $filesize ] ; then filesize=0 ; fi
- if (( $filesize == 0 ))
- then
- rm -f "$PWD/wallhaven-$imgID.jpg"
- >&2 echo "empty image removed"
- else
- touch "$PWD/wallhaven-$imgID.jpg"
- echo "$PWD/wallhaven-$imgID.jpg"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement