Created
March 18, 2013 18:12
-
-
Save anonymous/5189417 to your computer and use it in GitHub Desktop.
Download the bing image of the day and set it as wallpaper with python & mac os
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
@author: Adrian Pop | |
@email: contact [@] adrianpop.com | |
1. sudo easy_install appscript | |
2. python bing.py | |
PS. first python script :D | |
PS 2. it was easier if I could change the wallpaper from PHP or jQuery | |
""" | |
import sys, urllib2, json, os, datetime, random | |
from appscript import app, mactypes | |
from pprint import pprint | |
from array import array | |
""" download the image with max resolution """ | |
resolution = "1920x1200" | |
""" the folder BingImages must exists, or change the path """ | |
savePath = "/Users/" + os.getlogin() + "/Pictures/BingImages/" | |
""" market to download the picture from, should've been more """ | |
markets = ['en-US'] | |
mkt = random.choice(markets) | |
file = urllib2.urlopen("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=" + mkt) | |
data = file.read() | |
file.close() | |
data = json.loads(data) | |
urlbase = data[u'images'][0][u'urlbase'] | |
bingImage = "http://bing.com" + urlbase + "_" + resolution + ".jpg" | |
""" 2013-03-18_en-US.jpg """ | |
localFile = savePath + datetime.datetime.now().strftime("%Y-%m-%d") + "_" + mkt + ".jpg" | |
fh = open(localFile, 'wb') | |
request = urllib2.Request(bingImage, None, { 'User-Agent' : 'Mozilla/5.0' }) | |
fh.write(urllib2.urlopen(request).read()) | |
fh.close() | |
app('Finder').desktop_picture.set(mactypes.File(localFile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment