Advertisement
nitestryker

Random Wallpaper Downloader Rev 3.5 *New*

Oct 14th, 2023 (edited)
1,134
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ######################################################################
  4. # Copyright (C) 2023  Nitestryker
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, version 3 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. # Revision 3.5 (October 2023)
  20. #
  21. # Enhancements:
  22. # - Restored the dialog window for an improved user experience.
  23. # - Addressed and resolved issues related to conditional statements, enhancing script reliability.
  24. # - Improved screen resolution detection to prevent downloaded images from being cut off at the top.
  25.  
  26. # Revision 3.4
  27. #
  28. # Enhancements:
  29. # - Introduced a visually appealing colored banner at the script's startup.
  30. # - Temporarily removed the dialogue window with plans to reintroduce it in a future release upon issue resolution.
  31.  
  32. # Revision 3.3
  33. #
  34. # Improvements:
  35. # - Conducted bug fixes, addressing minor issues to enhance script stability.
  36.  
  37. # Revision 3.2
  38. #
  39. # New Features:
  40. # - Implemented screen resolution detection to dynamically set the DESIRED_RESOLUTION variable.
  41. # - Added download statistics to provide insights into the download process.
  42. # - Resolved an issue related to calculating downloaded images, ensuring accurate tracking.
  43.  
  44. # Revision 3.1
  45. #
  46. # Functionalities:
  47. # - Introduced the capability to calculate MD5 hashes for downloaded images.
  48. # - Established a mechanism to store MD5 hashes in the download_history.txt file, replacing previous URL-based tracking.
  49. # - Implemented a check in the script to skip downloads of images already present in the download history.
  50.  
  51. # Revision 3
  52. #
  53. # Feature Additions:
  54. # - Introduced user-defined wallpaper resolution handling.
  55. # - Implemented a download history feature to record and manage downloaded wallpapers.
  56. # - Enhanced the script with the ability for users to choose between random and keyword-based wallpaper downloads.
  57.  
  58. # Define color codes
  59. HEADER="\033[95m"
  60. OKBLUE="\033[94m"
  61. OKGREEN="\033[92m"
  62. WARNING="\033[93m"
  63. FAIL="\033[91m"
  64. ENDC="\033[0m"
  65. BOLD="\033[1m"
  66. UNDERLINE="\033[4m"
  67.  
  68. # Script Title
  69. backtitle="Wallpaper Downloader"
  70. title="Wallpaper Downloader"
  71.  
  72. # Get the current logged-in user
  73. user=$(whoami)
  74. # Define the directory where wallpapers will be stored
  75. WALLPAPER_DIR="/home/${user}/Pictures"
  76.  
  77. # Detect screen resolution and set DESIRED_RESOLUTION
  78. SCREEN_RESOLUTION=$(xrandr | awk '/\*/ {print $1}' | sed 's/x/*/g')
  79. DESIRED_RESOLUTION="$SCREEN_RESOLUTION"
  80.  
  81. # Path to the download history file
  82. DOWNLOAD_HISTORY_FILE="$WALLPAPER_DIR/download_history.txt"
  83.  
  84. # Function to download the wallpaper with a specific keyword or at random
  85. download_wallpaper() {
  86.     # Generate a unique filename based on the current timestamp
  87.     UNIQUE_FILENAME="wallpaper_$(date +'%Y%m%d%H%M%S').jpg"
  88.  
  89.     if [ "$DOWNLOAD_MODE" == "random" ]; then
  90.         # Download a random image from Unsplash
  91.         IMAGE_URL=$(curl -Ls -w %{url_effective} -o /dev/null "https://source.unsplash.com/random/$DESIRED_RESOLUTION")
  92.     elif [ "$DOWNLOAD_MODE" == "keyword" ]; then
  93.         # Download an image with the specified keyword
  94.         IMAGE_URL=$(curl -Ls -w %{url_effective} -o /dev/null "https://source.unsplash.com/featured/$DESIRED_RESOLUTION/?$KEYWORD")
  95.     else
  96.         echo "Invalid download mode: $DOWNLOAD_MODE"
  97.         return 1
  98.     fi
  99.  
  100.     # Calculate the MD5 hash of the image
  101.     IMAGE_HASH=$(curl -Lf "$IMAGE_URL" | md5sum | awk '{print $1}')
  102.  
  103.     # Check if the image hash is already in the download history
  104.     if grep -q "$IMAGE_HASH" "$DOWNLOAD_HISTORY_FILE"; then
  105.         echo "Skipping an already downloaded wallpaper: $IMAGE_URL"
  106.         return 0
  107.     fi
  108.  
  109.     # Save the image to the wallpaper directory with the unique filename
  110.     curl -Lf "$IMAGE_URL" > "$WALLPAPER_DIR/$UNIQUE_FILENAME"
  111.  
  112.     # Append the image hash to the download history file
  113.     echo "$IMAGE_HASH" >> "$DOWNLOAD_HISTORY_FILE"
  114.  
  115.     # Increment the downloaded wallpaper counter
  116.     ((DOWNLOADED_WALLPAPERS++))
  117. }
  118.  
  119. # Function to show a countdown timer
  120. show_countdown() {
  121.     for ((i = SLEEP_DURATION; i >= 0; i--)); do
  122.         printf "\rDownloading wallpaper %d of %d... Time left: %02d:%02d " "$DOWNLOADED_WALLPAPERS" "$MAX_WALLPAPERS" $((i / 60)) $((i % 60))
  123.         sleep 1
  124.     done
  125.     echo ""
  126. }
  127.  
  128. ###  ENTRY POINT #######
  129. ########################
  130.  
  131. # Clear the screen before showing the banner
  132. clear
  133.  
  134. # Banner
  135. banner=$(cat << "EOF"
  136. ░█░█░█▀█░█░░░█░░░█▀█░█▀█░█▀█░█▀▀░█▀▄░░░█▀▄░█▀█░█░█░█▀█░█░░░█▀█░█▀█░█▀▄░█▀▀░█▀▄
  137. ░█▄█░█▀█░█░░░█░░░█▀▀░█▀█░█▀▀░█▀▀░█▀▄░░░█░█░█░█░█▄█░█░█░█░░░█░█░█▀█░█░█░█▀▀░█▀▄
  138. ░▀░▀░▀░▀░▀▀▀░▀▀▀░▀░░░▀░▀░▀░░░▀▀▀░▀░▀░░░▀▀░░▀▀▀░▀░▀░▀░▀░▀▀▀░▀▀▀░▀░▀░▀▀░░▀▀▀░▀░▀
  139. EOF
  140. )
  141.  
  142. echo -e "${OKBLUE}$banner${ENDC}"
  143. echo -e "${OKGREEN}Rev 3.5 by ${FAIL}Nitestryker${ENDC}"
  144.  
  145. # Wait for two seconds
  146. sleep 2
  147.  
  148. # Clear the screen again
  149. clear
  150.  
  151. ## GET USERS INPUT ###
  152. ######################
  153.  
  154. # Ask the user to choose the download mode (random or keyword)
  155. exec 3>&1;
  156. DOWNLOAD_MODE="$(dialog --backtitle "$backtitle" --inputbox "Choose the download mode (random/keyword):" 15 30 2>&1 1>&3)"
  157. [[ "$DOWNLOAD_MODE" ]] || exit
  158.  
  159. if [ "$DOWNLOAD_MODE" == "keyword" ]; then
  160.    exec 3>&1;
  161.    KEYWORD="$(dialog --backtitle "$backtitle" --inputbox "Enter a keyword for wallpapers:" 15 30 2>&1 1>&3)"
  162.    [[ "$KEYWORD" ]] || exit
  163. elif [ "$DOWNLOAD_MODE" != "random" ]; then
  164.    echo "Invalid download mode: $DOWNLOAD_MODE"
  165.    exit 1  # Exit the script with an error code
  166. fi
  167.  
  168. exec 3>&1;
  169. MAX_WALLPAPERS="$(dialog --backtitle "$backtitle" --inputbox "Enter the maximum number of wallpapers to download (default: 100)" 15 30 2>&1 1>&3)"
  170. [[ "$MAX_WALLPAPERS" ]] || exit
  171.  
  172. # Ask the user to input the sleep duration in seconds
  173. exec 3>&1;
  174. SLEEP_DURATION="$(dialog --backtitle "$backtitle" --inputbox "Enter the sleep duration in seconds between downloading wallpapers (default: 300)" 15 30 2>&1 1>&3)"
  175. [[ "$SLEEP_DURATION" ]] || exit
  176.  
  177. # If the user didn't enter any value, set the default value to 300 seconds (5 minutes)
  178. SLEEP_DURATION=${SLEEP_DURATION:-300}
  179.  
  180. # Sleep for one second
  181. sleep 1
  182.  
  183. # Clear the screen
  184. clear
  185.  
  186. # Create the directory if it doesn't exist
  187. if [ ! -d "$WALLPAPER_DIR" ]; then
  188.    mkdir "$WALLPAPER_DIR"
  189. fi
  190.  
  191. # Create the download history file if it doesn't exist
  192. touch "$DOWNLOAD_HISTORY_FILE"
  193.  
  194. # Set the initial value of the downloaded wallpaper counter to 0
  195. DOWNLOADED_WALLPAPERS=0
  196.  
  197. #### Main loop #####
  198. ####################
  199.  
  200. while [ "$DOWNLOADED_WALLPAPERS" -lt "$MAX_WALLPAPERS" ]; do
  201.    # Download the wallpaper based on the chosen mode (random or keyword)
  202.    download_wallpaper
  203.  
  204.    # Show a notice and countdown before sleeping
  205.    echo "Downloading wallpaper $DOWNLOADED_WALLPAPERS of $MAX_WALLPAPERS..."
  206.    show_countdown
  207. done
  208.  
  209. # Calculate the total download time
  210. TOTAL_DOWNLOAD_TIME=$((MAX_WALLPAPERS * SLEEP_DURATION))
  211.  
  212. # Calculate the total data consumed in bytes
  213. TOTAL_DATA_CONSUMED=$(du -c -b "$WALLPAPER_DIR" | grep "total$" | awk '{print $1}')
  214.  
  215. # Calculate the average download speed in bytes per second
  216. if [ "$TOTAL_DOWNLOAD_TIME" -eq 0 ]; then
  217.    AVERAGE_DOWNLOAD_SPEED=0
  218. else
  219.    AVERAGE_DOWNLOAD_SPEED=$(echo "scale=2; $TOTAL_DATA_CONSUMED / $TOTAL_DOWNLOAD_TIME" | bc)
  220. fi
  221.  
  222. # Convert download statistics to appropriate units (bytes to MB or GB, seconds to minutes)
  223. if (( $(echo "$TOTAL_DATA_CONSUMED > 1073741824" | bc -l) )); then
  224.    TOTAL_DATA_CONSUMED=$(echo "scale=2; $TOTAL_DATA_CONSUMED / 1073741824" | bc)
  225.    DATA_UNIT="GB"
  226. elif (( $(echo "$TOTAL_DATA_CONSUMED > 1048576" | bc -l) )); then
  227.    TOTAL_DATA_CONSUMED=$(echo "scale=2; $TOTAL_DATA_CONSUMED / 1048576" | bc)
  228.    DATA_UNIT="MB"
  229. else
  230.    TOTAL_DATA_CONSUMED=$(echo "scale=2; $TOTAL_DATA_CONSUMED / 1024" | bc)
  231.    DATA_UNIT="KB"
  232. fi
  233.  
  234. if (( $(echo "$AVERAGE_DOWNLOAD_SPEED > 1048576" | bc -l) )); then
  235.    AVERAGE_DOWNLOAD_SPEED=$(echo "scale=2; $AVERAGE_DOWNLOAD_SPEED / 1048576" | bc)
  236.    SPEED_UNIT="MB/s"
  237. elif (( $(echo "$AVERAGE_DOWNLOAD_SPEED > 1024" | bc -l) )); then
  238.    AVERAGE_DOWNLOAD_SPEED=$(echo "scale=2; $AVERAGE_DOWNLOAD_SPEED / 1024" | bc)
  239.    SPEED_UNIT="KB/s"
  240. else
  241.    SPEED_UNIT="B/s"
  242. fi
  243.  
  244. # Clear the screen
  245. clear
  246.  
  247. # Display download statistics
  248. echo "Download Statistics:"
  249. echo "Number of Wallpapers Downloaded: $DOWNLOADED_WALLPAPERS"
  250. echo "Total Data Consumed: $TOTAL_DATA_CONSUMED $DATA_UNIT"
  251. echo "Average Download Speed: $AVERAGE_DOWNLOAD_SPEED $SPEED_UNIT"
  252.  
  253. # Exit the script once the maximum number of wallpapers is downloaded
  254. echo "Maximum number of wallpapers ($MAX_WALLPAPERS) downloaded. Exiting..."
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement