Newbie Project: Download wallpaper from Bing for Win 7

This is the place for queries that don't fit in any of the other categories.

Newbie Project: Download wallpaper from Bing for Win 7

Postby qbektrix » Wed Mar 20, 2013 5:10 pm

I am a newbie to Python. I just have 2yrs experience in C# Asp.net Web form.
I have gone through a couple of tutorials but I am still not sure. So, I decided to do a project as a way of learning better and I need help and guidance to get it done
My project is an application to download background image from bing.com, save it to my hard disk and make it a wallpaper in my Windows 7 machine.
The steps are:
    >Goto bing.com and get the url location of the background image.
    >Save it a specific folder in my PC
    >Make it the wallpaper in my Windows 7
I guess, a scheduler can be used to run it every 24 hrs.
Couple of project I saw on net deal with ubuntu or mac and they get the image from a RSS feed which is stopped updating now.
Where do I start from?
qbektrix
 
Posts: 2
Joined: Wed Mar 20, 2013 5:05 pm

Re: Newbie Project: Download wallpaper from Bing for Win 7

Postby setrofim » Wed Mar 20, 2013 7:14 pm

  • For downloading images from Bing, look into requests and lxml.
  • For how to write it to disk, see the tutorial.
  • If you want to do something with the images (e.g. check that they are the right size/aspect ratio for your desktop), PIL is what you want.
  • To change wallpaper on Windows, you'll need to load user32.dll and do it through that; see this.
  • To do the above periodically, time.sleep.
  • To run your script in the background, you make it into a Windows Service (or you could just stick it into your startup folder).
Good luck.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Newbie Project: Download wallpaper from Bing for Win 7

Postby snippsat » Thu Mar 21, 2013 1:58 pm

setrofim exploitation is very good,but there is a problem and that is Bing.
Bing use javaScript to execute a new image every day,then you can not parse it with good parser like lxml and Beautiful Soup.
JavaScript get executed by the Dom render in browser.

This make this task more complicated,but there are some option.
There are a option to simulate browser behavior by using tool like Webkit,can be used trough PyQt4/Pyside or ghost.py(looks good)

I have a go at this without simulate that i am a browser,when downloading site contend i get the unexecuted JavaScript contend of image address.
Code: Select all
g_img={url:'/az/hprichbg/rb/SiosehBridge_ROW9116275756_1366x768.jpg',id:'bgDiv',d:'200',cN:'_SS',crN:'bIm',hash:'756',del:'50'}

This is a good result because i can see image adress,then i can write a regex to get image address and and then concatenate with url.

So here how it looks,it will download image in folder you run script from.
For me it works,and it change my desktop background to bing background image.
Code: Select all
import re
from urllib2 import urlopen
from urllib import urlretrieve
import ctypes

url = urlopen('http://www.bing.com/')
url_read = url.read()
pattern = re.search(r"g_img={url:'(.*)',id", url_read)
pic = pattern.group(1)
pic_adress = '{}{}'.format(url.url, pic)
print pic_adress # Adress of bing background image

# Take out filename from pic_adress and save it
filename = pic_adress.strip().split('/')[-1]
urlretrieve(pic_adress, filename)
print("image saved as {}".format(filename))

# Set desktop background
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, filename , 0)
User avatar
snippsat
 
Posts: 294
Joined: Thu Feb 21, 2013 12:04 am

Re: Newbie Project: Download wallpaper from Bing for Win 7

Postby qbektrix » Fri Mar 22, 2013 6:59 pm

wow. Thank you setrofim & snippsat for your inputs. You both are really quick. I was planning a month long research.

Pls be patient with me. This is a hobby project of mine.

The input of bing.com was interesting. snippsat, will I able to use curl or wget?
qbektrix
 
Posts: 2
Joined: Wed Mar 20, 2013 5:05 pm

Re: Newbie Project: Download wallpaper from Bing for Win 7

Postby snippsat » Sat Mar 23, 2013 1:42 pm

will I able to use curl or wget?

No they don't execute javaScript so they will not download background image,there is no need to use these tool Python can do the same with urlretrieve()(as you see in my code).
My code works fine for me,and i have changed my desktop background last 3 days to bing background.
User avatar
snippsat
 
Posts: 294
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests