So I Made A Python Script Which Sets My Desktop Background As An Image From Reddit
For the past few months I have been experimenting with Python. Coming from a background in Java and C#, I was amazed about how easy it is to learn and do from the simplest stuff like writing a “Hello World” program to more complex tasks like calling public APIs and manipulating their data.
After a couple of months of playing around doing some nonsense experiments with Python, I’ve finally built a script that would be of some use for me. A script that the top rated image from the last 24 hours from a reddit subreddit and set my desktop background to that image.
So How Does It Work?
In general the script works as follows:
- Get the top image from the a subreddit specified by the user (eg. spaceporn or japanpics) via the reddit API
- download the image to the user’s computer
- set the image as the user’s desktop background
The whole code for the project is available on GitHub. Go ahead, download the code, play around with it or even add your own touch to it!
Now I’m going to break down the code line by line:
These are the libraries that need to be imported to the project for the script to work properly. These are:
- urllib.request: I used this library to get the response for the call to the reddit API, as plain JSON text
- json: this library decodes the JSON text into a Python object
- sys: the subreddit will be provided by the user in the command line. The sys library provides functions for grabbing the text provided by the user.
- ctypes: provides functionality to set the Windows background to an image of your choice.
- time: provides the sleep() function to make the script wait for a certain amount of time.
2. Get Subreddit URL
getSubredditURL = lambda subreddit: “https://www.reddit.com/r/" + subreddit + “/search.json?q=url%3A.jpg+OR+url%3A.png&sort=top&restrict_sr=on&t=day”
This lines creates a function called getSubredditURL() which takes the subreddit as parameter. The URL gives the top images of the last 24 hours in .jpg or .png format, as JSON text.
For example, for /r/EarthPorn, the URL of the top images is:
3. Get Text
getText = lambda url: urllib.request.urlopen(url).read()
getText() is a function for getting the text of the address above and returning it as a string.
4. Get JSON From Text
getJSONFromText = lambda text: json.loads(text.decode(‘utf-8’))
Next, we need to get the JSON formatted text returned from getText() and convert into a Python object. getJSONFromText() gets the JSON text as a parameter and returns the Python object.
5. Get Children From JSON
getChildrenFromJSON = lambda json: json[“data”][“children”]
In the reddit API, the individual posts (whether link or self posts) are known as children. To retrieve these, we must go to JSON’s “data” node and then “children”. The getChildrenFromJSON() takes in the Python object and returns the list of all the “children” retrieved from JSON.
6. Get Data Of Child
getDataOfChild = lambda children, index: children[index][“data”]
Now we need to get the data for a specific image. This will help us get details about the image, such as its URL, title, score etc. getDataOfChild() takes as parameters, the “children” list and an index. The index specifies which child to get data from.
7. Get Image Name From URL
getImageNameFromURL = lambda url: url.split(“/”)[-1].split(“?”)
The image URL will be returned in the form of either http://domain.com/image.extension or http://domain.com/image.extension?someparameters. This simple function cuts out the domain and parameters part and keeps the image file name with its extension. For example, from “http://i.imgur.com/abcde.jpg”, it keeps the “abcde.jpg” part only.
8. Store Image
storeImage = lambda url, fileName: open(fileName, “wb”).write(urllib.request.urlopen(url).read())
The storeImage function simply stores the image with the specified URL on the user’s computer with the specified fileName.
9. Set Image As Background
setImageAsBackground = lambda image: ctypes.windll.user32.SystemParametersInfoW(20, 0, image , 0)
The final step is to set the downloaded image as the desktop background for the user.
10. All Together Now
The final function setBackground() takes all the previously mentioned functions and runs them together to run the whole process, from getting the image from reddit, to setting it as the user’s background.