Shell scripting to change wallpaper – Wallhaven API

1. Introduction

Changing your desktop wallpaper regularly can be a fun way to keep your workspace fresh and inspiring. Automating this task through shell scripting can save time and effort, allowing your system to change wallpapers based on …


This content originally appeared on DEV Community and was authored by Mitansh Panchal

1. Introduction

Changing your desktop wallpaper regularly can be a fun way to keep your workspace fresh and inspiring. Automating this task through shell scripting can save time and effort, allowing your system to change wallpapers based on your preferences without manual intervention. In this guide, we'll explore how to use shell scripting with the Wallhaven API to automatically change your wallpaper on a Linux system.

Image description

Image description

2. What is Shell Scripting?

Shell scripting is a way to automate tasks in a Unix-like operating system by writing a series of commands in a file that can be executed by the shell. It allows users to automate repetitive tasks, making it a powerful tool for system administration and everyday tasks.

Shell scripting is highly useful for automating small tasks in Linux, such as system maintenance, task scheduling, data processing, and more. It helps users perform operations more efficiently, reducing manual effort and the possibility of errors.

3. What is the Wallhaven API?

Introduction to Wallhaven: Wallhaven is a popular platform known for its vast collection of high-quality wallpapers. Users can explore a wide range of wallpapers, including themes like nature, abstract, anime, and more.

Wallhaven API: The Wallhaven API provides programmatic access to the Wallhaven database, allowing users to search for wallpapers based on specific criteria such as tags, categories, and resolutions. This API makes it possible to fetch wallpaper URLs and download images directly through scripts or applications.

API Key: To use the Wallhaven API, you need an API key, which you can obtain by creating a free account on Wallhaven. The API key is used to authenticate requests and access the API's features.

4. Using curl in Shell Scripting to Fetch API Data

Introduction to curl: curl is a command-line tool used for transferring data to or from a server using various protocols, including HTTP, HTTPS, FTP, and more. It's commonly used in shell scripting for interacting with APIs, downloading files, and testing server responses.

Basics of Using curl with APIs:

  • The basic syntax for making a GET request with curl is:
  curl -X GET "URL"
  • For APIs that require headers or authentication, you can add options like -H for headers and -d for data.

Fetching Data from Wallhaven API:

  • Use curl to fetch data from the Wallhaven API by sending a GET request to the API endpoint.
  • Handle headers and parse JSON responses using tools like jq, which helps extract specific data points from the JSON output.

5. Creating the Shell Script to Change the Wallpaper

Prerequisites:

  • Tools Needed: curl for making API requests, jq for parsing JSON responses, a command to set the wallpaper (feh, gsettings, swww for Wayland), aria2c for downloading images, and wal for generating themes.
  • API Key: A valid Wallhaven API key.

Step-by-Step Script Breakdown:

#!/bin/bash

# Define your Wallhaven API key
KEY="YOUR_API_KEY"

# Clear previous wallpapers from the cache
rm -rf /home/looph0le/.cache/wallhaven*

# Accept the wallpaper name or search keyword as an argument
wallname=$1

# Check if a search keyword is provided
if [[ $(echo "$wallname" | wc -c) -gt 1 ]]; then
  # Fetch wallpaper URL using the Wallhaven API
  imageUrl=$(curl -s "https://wallhaven.cc/api/v1/search?apikey=$KEY&purity=111&categories=111&ratios=16x9&sorting=random&q=$wallname" | jq .data[].path | shuf -n1)

  # Format the URL link
  imageLink=$(echo $imageUrl | cut -d'"' -f2)

  # Download the wallpaper
  echo $imageLink
  aria2c -d ~/.cache/ $imageLink

  # Generate a theme using pywal
  wal -i ~/.cache/wallhaven*

  # Set the wallpaper using swww (a wallpaper utility for Wayland)
  swww query || swww init
  swww img ~/.cache/wallhaven* --transition-fps 60 --transition-type outer --transition-duration 3

  # Reload Waybar (status bar for Wayland)
  sh ~/.config/waybar/launch.sh
fi

6. Testing and Running the Script

  • Execution: Save the script with a .sh extension, make it executable using:
  chmod +x change_wallpaper.sh
  • Run the script with a keyword to search for a specific type of wallpaper:
  ./change_wallpaper.sh "nature"
  • Scheduling with Cron: To automate the wallpaper change, add the script to your crontab:
  crontab -e

Add a line to run the script every hour:

  0 * * * * /path/to/change_wallpaper.sh "nature"

7. Conclusion

We have explored how to automate wallpaper changes using shell scripting and the Wallhaven API. By leveraging tools like curl, jq, and aria2c, and using utilities for setting wallpapers, you can easily customize your desktop with new wallpapers automatically.
Feel free to modify the script to suit your preferences, such as filtering wallpapers by categories, tags, or resolutions. Explore other API options and enhance your script with additional features.


This content originally appeared on DEV Community and was authored by Mitansh Panchal


Print Share Comment Cite Upload Translate Updates
APA

Mitansh Panchal | Sciencx (2024-09-01T18:26:24+00:00) Shell scripting to change wallpaper – Wallhaven API. Retrieved from https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/

MLA
" » Shell scripting to change wallpaper – Wallhaven API." Mitansh Panchal | Sciencx - Sunday September 1, 2024, https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/
HARVARD
Mitansh Panchal | Sciencx Sunday September 1, 2024 » Shell scripting to change wallpaper – Wallhaven API., viewed ,<https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/>
VANCOUVER
Mitansh Panchal | Sciencx - » Shell scripting to change wallpaper – Wallhaven API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/
CHICAGO
" » Shell scripting to change wallpaper – Wallhaven API." Mitansh Panchal | Sciencx - Accessed . https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/
IEEE
" » Shell scripting to change wallpaper – Wallhaven API." Mitansh Panchal | Sciencx [Online]. Available: https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/. [Accessed: ]
rf:citation
» Shell scripting to change wallpaper – Wallhaven API | Mitansh Panchal | Sciencx | https://www.scien.cx/2024/09/01/shell-scripting-to-change-wallpaper-wallhaven-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.