Advertisement
mosaid

Untitled

Mar 7th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.97 KB | None | 0 0
  1. #!/bin/bash
  2. scriptname=$( basename "$0" )
  3. is_running=$( pgrep -c "$scriptname" )
  4. if (( $is_running > 1 )) ; then
  5.     >&2 echo $scriptname is running.
  6.     exit 0
  7. fi
  8. if [ -z $1 ]
  9.     then purity=100  #safe for work
  10.     LOCATION="/path/to/dir/"
  11.     else purity=011 # nsfr
  12.     LOCATION="/path/to/dir/"
  13. fi
  14. if [ ! -d "$LOCATION" ] ; then mkdir -p "$LOCATION" ; fi
  15. cd "$LOCATION" || exit
  16. function WGET {
  17.     userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) "
  18.     userAgent+="Gecko/20100101 Firefox/48.0"
  19.     wget -q -U "$userAgent" --keep-session-cookies \
  20.         --save-cookies=cookies.txt --load-cookies=cookies.txt "$@"
  21. }
  22. # login :
  23. USER="wallhaven user name"
  24. PASS='*******wallhaven password'
  25. WGET --referer=https://alpha.wallhaven.cc https://alpha.wallhaven.cc/auth/login
  26. token="$(grep 'name="_token"' login | sed 's:.*value="::' | sed 's/.\{2\}$//')"
  27. WGET --referer=https://alpha.wallhaven.cc/auth/login \
  28.      --post-data="_token=$token&username=$USER&password=$PASS" \
  29.      https://alpha.wallhaven.cc/auth/login
  30. #query url
  31. s1="search?page=1&categories=101&purity=$purity&"
  32. s1+="resolutions=1920x1080&ratios=&sorting=random&order=desc"
  33. #get page
  34. WGET --referer=https://alpha.wallhaven.cc -O tmp  "https://alpha.wallhaven.cc/$s1"
  35. #get image id and download it
  36. imgID="$(grep -o 'th-[0-9]*' tmp | sed  's .\{3\}  ' | head -1)"
  37.  ! WGET --referer=https://alpha.wallhaven.cc/wallpaper/"$imgID" \
  38.         https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-$imgID.jpg &&
  39.  ! WGET --referer=https://alpha.wallhaven.cc/wallpaper/"$imgID" \
  40.         https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-$imgID.png
  41. rm -f cookies.txt login login.1 tmp
  42. data=$(stat "$PWD/wallhaven-$imgID.jpg" 2>/dev/null)
  43. filesize=$(echo "$data" | awk 'NR==2{print $2}')
  44. if [ -z $filesize ] ; then filesize=0 ; fi
  45. if (( $filesize == 0 ))
  46.     then
  47.         rm -f "$PWD/wallhaven-$imgID.jpg"
  48.         >&2 echo "empty image removed"
  49.     else
  50.         touch "$PWD/wallhaven-$imgID.jpg"
  51.         echo "$PWD/wallhaven-$imgID.jpg"
  52. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement