Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Created March 4, 2014 07:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwiser/9341757 to your computer and use it in GitHub Desktop.
Save bitwiser/9341757 to your computer and use it in GitHub Desktop.
Download Bing Image of the Day
import os
import sys
import urllib2
import json
import threading
import time
BING = {
"base": "http://bing.com",
"wall": "/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1393792328465&pid=hp&video=1"
}
def getWallpaperUrl():
url = BING['base']+BING['wall']
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = json.load(response)
img = ''
if data.has_key('images'):
if len(data['images'])==1:
if data['images'][0].has_key('url'):
img = data['images'][0]['url']
else:
print('No image url. Exiting...\n')
else:
print('No image url. Exiting...\n')
else:
print('No image url. Exiting...\n')
if img == '':
exit()
return img
def downloadWallpaper():
url = BING['base']+getWallpaperUrl()
path = url.split('/')
path = path[len(path)-1]
req = urllib2.Request(url)
resp = urllib2.urlopen(req)
out = open(path,'wb')
out.write(resp.read())
out.close()
if __name__ =='__main__':
img_thread = threading.Thread(target=downloadWallpaper)
img_thread.start()
st = '\rDownloading Image'
current = 1
while img_thread.is_alive():
sys.stdout.write(st+'.'*((current)%5))
current=current+1
time.sleep(0.3)
img_thread.join()
print('\nImage of the day downloaded.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment