Hyper Expert /Blog
About
Archives

Python script to download Bing’s daily image for Mac

I previously wrote about downloading Bing’s daily image and set it as your wallpaper using PowerShell script. However, if you are a Mac user, this simple Python script will do a similar job.

I have already made the following script an app using Mac Automator. It basically creates a new directory for you, if one does not exist, under your “Pictures” directory called “Bing Wallpapers” and download Bing’s daily image there as “wallpaper.jpg”.

You can save the app in your Applications folder or anywhere you want.
Download app

Or, if you want to edit the script, download the source below and make your own app.
Download script

import urllib2
import json
import datetime
import os.path, time
from os.path import expanduser

#market options: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ
market = 'en-US'
resolution = '1920x1080'
BingDirectory= expanduser("~") +'/Pictures/Bing Wallpaper/'
WallpaperName = 'wallpaper.jpg'

loop_value = 1
while (loop_value == 1):
    try:
		urllib2.urlopen("http://google.com")
    except urllib2.URLError, e:
		time.sleep( 10 )
    else:
		loop_value = 0

		response = urllib2.urlopen("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=" + market)
		obj = json.load(response)
		url = (obj['images'][0]['urlbase'])
		url = 'http://www.bing.com' + url + '_' + resolution + '.jpg'

		if not os.path.exists(BingDirectory): 
			os.makedirs(BingDirectory)
		path = BingDirectory + WallpaperName

		if os.path.exists(path):
			todayDate = datetime.datetime.now().strftime("%m/%d/%Y")
			fileDate = time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(path)))
			if todayDate == fileDate:
				print "You already have today's Bing image"
			else:
				print ("Downloading Bing wallpaper to %s" % (path))
				f = open(path, 'w')
				bingpic = urllib2.urlopen(url)
				f.write(bingpic.read())
		else:
			print ("Downloading Bing wallpaper to %s" % (path))
			f = open(path, 'w')
			bingpic = urllib2.urlopen(url)
			f.write(bingpic.read())

To make the script an app using Automator, follow these step by step.

– From Spotlight, type automator and select the Automator application
Automator

– Select “Application” from the dialog box and click “Choose”
Automator Choose Application

– On the left under Actions, select “Utilities” -> Run Shell Script.
Automator Run Shell Script

– Drag and drop the script to the right side where it says “Drag actions or files here to build your workflow.”
Automator Run Shell Script

– Save the script to Desktop, or anywhere you want.
Automator Save Script

That should be it! If you want to go the extra mile and change the app icon to Bing’s logo. Do this:

First download an icon. I have included the Bing icon I used if you would like to use it.

Bing Icon

– Right-Click on the created Automator app and select “Get Info”
Get Info

– Open your Icon file, press “Command + C” or go to Edit menu and copy the icon. Go back to the app “Get Info” window, click on the top left where the small automator icon is and paste “Command + V”
Get Info

Get Info

Your new automator app should now have the new Bing icon.
Bing Icon

That should be it. Enjoy your Bing images 😉

To set the script to run every time you log in. Go to your “System Preferences” -> “Users & Groups”, select your username, select “Login Items” and click on the “+” sign to add an application to run at login

/////

Hi, my name is Ali. I am a Systems Engineer. I live in the beautiful Evergreen state. I have a Bachelor degree of science in Information Technology with a handful of accompanying I.T. certificates. I also have a degree in Computer Networking. I am an Apple Certified Technician, Microsoft MCITP and a Linux expert.

4 Comments

  1. Shell script to download Bing’s daily image as a wallpaper for Mac | Hyper Expert · September 30, 2015

    […] downloading Bing’s daily image and set it as your wallpaper using PowerShell for Windows. Or, Python for Mac. This time around, I will show you how to do a Shell script to achieve the same thing. Also, set […]

    Reply
  2. darius · January 22, 2016

    This is great, thanks much!

    Unfortunately the script only seems to download the latest image if there’s no bing pictures folder. Any idea what the issue might be? cheers, d.

    Reply
    • Ali · January 22, 2016

      The script will only download the latest Bing image. It checks if the image you have is the latest, if it is, it won’t download a new one. If the image you have is old, it will rewrite your old image with the latest. It wont download a new one if you already have the latest.

      Hope this helps

      Reply

Would you like to share your thoughts?