Created
March 4, 2014 07:21
-
-
Save bitwiser/9341757 to your computer and use it in GitHub Desktop.
Download Bing Image of the Day
This file contains hidden or 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
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