/wallhaven.sh

http://github.com/sevensins/Wallbase-Downloader · Shell · 171 lines · 105 code · 17 blank · 49 comment · 23 complexity · 2831a0a158ade6031a51c685ec470c98 MD5 · raw file

  1. #!/bin/bash
  2. #
  3. ##################################
  4. ### Needed for NSFW/New ###
  5. ##################################
  6. # Enter your Username
  7. USER=""
  8. # Enter your password
  9. PASS=""
  10. #################################
  11. ### End needed for NSFW ###
  12. #################################
  13. #################################
  14. ### Configuration Options ###
  15. #################################
  16. # Where should the Wallpapers be stored?
  17. LOCATION=/location/to/your/wallpaper/folder
  18. # How many Wallpapers should be downloaded, should be multiples of 64 (right now they only use a fixed number of thumbs per page)
  19. WPNUMBER=64
  20. # Type standard (newest, oldest, random, hits, mostfav), search, favorites, useruploads
  21. TYPE=standard
  22. # From which Categories should Wallpapers be downloaded
  23. CATEGORIES=111
  24. # Which Purity Wallpapers should be downloaded
  25. PURITY=111
  26. # Which Resolution should be downloaded, leave empty for all
  27. RESOLUTION=
  28. # Which aspectratio should be downloaded, leave empty for all
  29. RATIO=
  30. # Which Type should be displayed (relevance, random, date_added, views, favorites)
  31. SORTING=random
  32. # How should the Wallpapers be ordered (desc, asc)
  33. ORDER=desc
  34. # Searchterm
  35. QUERY="nature"
  36. # User from which Wallpapers should be downloaded (only used for TYPE=useruploads)
  37. USR=AksumkA
  38. #################################
  39. ### End Configuration Options ###
  40. #################################
  41. if [ ! -d $LOCATION ]; then
  42. mkdir -p $LOCATION
  43. fi
  44. cd $LOCATION
  45. #
  46. # logs in to the wallhaven website to give the user more functionality
  47. # requires 2 arguments:
  48. # arg1: username
  49. # arg2: password
  50. #
  51. function login {
  52. # checking parameters -> if not ok print error and exit script
  53. if [ $# -lt 2 ] || [ $1 == '' ] || [ $2 == '' ]; then
  54. printf "Please check the needed Options for NSFW Content (username and password)\n\n"
  55. printf "For further Information see Section 13\n\n"
  56. printf "Press any key to exit\n"
  57. read
  58. exit
  59. fi
  60. # everythings ok --> login
  61. wget -q --keep-session-cookies --save-cookies=cookies.txt --referer=http://alpha.wallhaven.cc http://alpha.wallhaven.cc/auth/login
  62. token="$(cat login | grep 'name="_token"' | sed 's .\{180\} ' | sed 's/.\{16\}$//')"
  63. wget -q --load-cookies=cookies.txt --keep-session-cookies --save-cookies=cookies.txt --referer=http://alpha.wallhaven.cc/auth/login --post-data="_token=$token&username=$USER&password=$PASS" http://alpha.wallhaven.cc/auth/login
  64. } # /login
  65. #
  66. # downloads Page with Thumbnails
  67. #
  68. function getPage {
  69. # checking parameters -> if not ok print error and exit script
  70. if [ $# -lt 1 ]; then
  71. printf "getPage expects at least 1 argument\n"
  72. printf "arg1: parameters for the wget -q command\n\n"
  73. printf "press any key to exit\n"
  74. read
  75. exit
  76. fi
  77. # parameters ok --> get page
  78. wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc -O tmp "http://alpha.wallhaven.cc/$1"
  79. } # /getPage
  80. #
  81. # downloads all the wallpapers from a wallpaperfile
  82. # arg1: the file containing the wallpapers
  83. #
  84. function downloadWallpapers {
  85. URLSFORIMAGES="$(cat tmp | grep -o '<a href="http://alpha.wallhaven.cc/wallpaper/[0-9]*"' | sed 's .\{9\} ')"
  86. for imgURL in $URLSFORIMAGES
  87. do
  88. img="$(echo $imgURL | sed 's/.\{1\}$//')"
  89. number="$(echo $img | sed 's .\{36\} ')"
  90. if cat downloaded.txt | grep -w "$number" >/dev/null
  91. then
  92. printf "File already downloaded!\n"
  93. else
  94. echo $number >> downloaded.txt
  95. wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc $img
  96. cat $number | egrep -o "http://alpha.wallhaven.cc/wallpapers.*(png|jpg|gif)" | wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=http://alpha.wallhaven.cc/wallpaper/$number -i -
  97. rm $number
  98. fi
  99. done
  100. rm tmp
  101. } #/downloadWallpapers
  102. # login only when it is required ( for example to download favourites or nsfw content... )
  103. if [ $PURITY == 001 ] || [ $PURITY == 011 ] || [ $PURITY == 111 ] ; then
  104. login $USER $PASS
  105. fi
  106. if [ $TYPE == standard ]; then
  107. for (( count= 0, page=1; count< "$WPNUMBER"; count=count+64, page=page+1 ));
  108. do
  109. printf "Download Page $page"
  110. getPage "wallpaper/search?page=$page&categories=$CATEGORIES&purity=$PURITY&resolutions=$RESOLUTION&ratios=$RATIO&sorting=$SORTING&order=$ORDER"
  111. printf " - done!\n"
  112. printf "Download Wallpapers from Page $page"
  113. downloadWallpapers
  114. printf " - done!\n"
  115. done
  116. elif [ $TYPE == search ] ; then
  117. # SEARCH
  118. for (( count= 0, page=1; count< "$WPNUMBER"; count=count+"64", page=page+1 ));
  119. do
  120. printf "Download Page $page"
  121. getPage "wallpaper/search?page=$page&categories=$CATEGORIES&purity=$PURITY&resolutions=$RESOLUTION&ratios=$RATIO&sorting=relevance&order=desc&q=$QUERY"
  122. printf " - done!\n"
  123. printf "Download Wallpapers from Page $page"
  124. downloadWallpapers
  125. printf " - done!\n"
  126. done
  127. elif [ $TYPE == favorites ] ; then
  128. # FAVORITES
  129. # currently using sum of all collections
  130. favnumber="$(wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc http://alpha.wallhaven.cc/favorites -O - | grep -A 1 "<span>Favorites</span>" | grep -B 1 "<small>" | sed -n '2{p;q}' | sed 's/.\{9\}$//' | sed 's .\{23\} ')"
  131. for (( count= 0, page=1; count< "$WPNUMBER" && count< "$favnumber"; count=count+"64", page=page+1 ));
  132. do
  133. printf "Download Page $page"
  134. getPage "favorites?page=$page"
  135. printf " - done!\n"
  136. printf "Download Wallpapers from Page $page"
  137. downloadWallpapers
  138. printf " - done!\n"
  139. done
  140. elif [ $TYPE == useruploads ] ; then
  141. # UPLOADS FROM SPECIFIC USER
  142. for (( count= 0, page=1; count< "$WPNUMBER"; count=count+"64", page=page+1 ));
  143. do
  144. printf "Download Page $page"
  145. getPage "user/$USR/uploads?page=$page"
  146. printf " - done!\n"
  147. printf "Download Wallpapers from Page $page"
  148. downloadWallpapers
  149. printf " - done!\n"
  150. done
  151. else
  152. printf "error in TYPE please check Variable\n"
  153. fi
  154. rm -f cookies.txt login login.1