Advertisement
Guest User

Bing picture of the day on your mac desktop

a guest
Jul 21st, 2011
5,918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import md5
  4. import pprint
  5. import sys
  6. import subprocess
  7. from time import strftime
  8. from urllib import URLopener
  9. from urllib2 import urlopen
  10. from xml.dom.minidom import parseString
  11.  
  12. # Defines source and destination of image
  13. rss_feed = 'http://feeds.feedburner.com/bingimages';
  14. dst_dir = os.path.expanduser('~/Pictures/DeskFeed/')
  15.  
  16. SCRIPT = """/usr/bin/osascript<<END
  17. tell application "Finder"
  18. set desktop picture to POSIX file "%s"
  19. end tell
  20. END"""
  21.  
  22.  
  23. def set_desktop_background(destination):
  24.   subprocess.Popen(SCRIPT%destination, shell=True)
  25.  
  26. def parseFeed(rss):
  27.   destination = "%s%s.jpg" % (dst_dir, strftime( "%y-%m-%d"))
  28.   if os.path.exists(destination):
  29.     sys.exit(0)
  30.  
  31.  
  32.  
  33.   try:
  34.     rss_contents = urlopen( rss )
  35.   except:
  36.     print "Failed to read rss feed %s" % rss
  37.     return
  38.   rss_src = rss_contents.read()
  39.   rss_contents.close()
  40.   dom = parseString( rss_src )
  41.   firstitem = dom.getElementsByTagName('item')[0]
  42.   link = firstitem.getElementsByTagName( 'enclosure' )[0].getAttribute('url')
  43.   URLopener().retrieve(link, destination)
  44.   set_desktop_background(destination)
  45.  
  46.  
  47. def main():
  48.   parseFeed(rss_feed)
  49.  
  50. if __name__ == "__main__":
  51.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement