Skip to content

Commit

Permalink
Use class to do it; fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
lord63 committed Sep 6, 2014
1 parent 6a80105 commit 7da3df0
Showing 1 changed file with 60 additions and 78 deletions.
138 changes: 60 additions & 78 deletions wonderful_bing/wonderful_bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pictures from Bing and set as wallpaper. My first python program :)
:copyright: (c) 2014 by lord63.
:license: MIT, see LICENSE for more details.
:license: MIT, see LICENSE for more details.
"""

__title__ = "wonderful_bing"
Expand All @@ -27,92 +27,74 @@
import requests


def show_notify(is_ZH):
r = requests.get('http://www.bing.com')
if is_ZH:
title = u'今日图片故事'
else:
class WonderfulBing(object):
def __init__(self,config):
# We can get all the information we need from this url, see issue#7
self.url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=\
1&nc=1409879295618&pid=hp"
self.reponse = requests.get(self.url)
self.story = requests.get(self.url).json()["images"][0]["copyright"]
self.picture_url = requests.get(self.url).json()["images"][0]["url"]
self.config = config

def show_notify(self):
title = "Today's Picture Story"
story_name = re.search(
'((?<=id="sh_cp" title=")|(?<=class="sc_light" title=")).*(?=\(\\xa9)',
r.text).group()
n = pynotify.Notification(
title, story_name,
os.path.dirname(os.path.realpath(__file__))+'/img/icon.png')
n.show()


def get_picture_url(page_url):
r = requests.get(page_url)
match = re.search(
"/az/hprichbg/rb/.+?(?=')", r.text)
picture_url = match.group()
if r.history:
ZH = 1
picture_url = 'http://s.cn.bing.net' + picture_url
else:
ZH = 0
picture_url = 'http://www.bing.com' + picture_url
return picture_url, ZH


def download_and_set(picture_url, is_ZH):
picture_name = get_picture_name(picture_url)
picture_path = config['directory'] + picture_name
if os.path.exists(picture_path):
print "You have downloaded the picture before."
print "Have a look at it --> " + picture_path
return

# sleep for two seconds, otherwise the newly setted wallpaper will be
# setted back by the system when your system boots up if you have added
# this script to autostart.
time.sleep(2)
r = requests.get(picture_url, stream=True) # To get the raw content
with open(picture_path, "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
print "Successfully download the picture to --> " + picture_path
set_wallpaper(picture_path)
print "Successfully set the picture as the wallpaper. :)"

show_notify(is_ZH)


def get_picture_name(picture_url):
match = re.search(
"(?<=/az/hprichbg/rb/).+?(?=_)", picture_url)
picture_name = match.group() + '.jpg'
return picture_name


def set_wallpaper(picture_path):
os.system('DISPLAY=:0 GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.'
'gnome.desktop.background picture-uri file://' + picture_path)
notification = pynotify.Notification(
title, self.story,
os.path.dirname(os.path.realpath(__file__))+'/img/icon.png')
notification.show()

def get_picture_name(self):
match = re.search(
"(?<=/az/hprichbg/rb/).+?(?=_)", self.picture_url)
picture_name = match.group() + '.jpg'
return picture_name

def set_wallpaper(self, picture_path):
os.system('DISPLAY=:0 GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.'
'gnome.desktop.background picture-uri file://' + picture_path)

def download_and_set(self):
picture_name = self.get_picture_name()
picture_path = self.config['directory'] + picture_name
if os.path.exists(picture_path):
print "You have downloaded the picture before."
print "Have a look at it --> " + picture_path
return
# sleep for two seconds, otherwise the newly setted wallpaper will be
# setted back by the system when your system boots up if you have added
# this script to autostart.
time.sleep(2)
r = requests.get(self.picture_url, stream=True) # To get the raw content
with open(picture_path, "wb") as f:
for chunk in r.iter_content(1024):
f.write(chunk)
print "Successfully download the picture to --> " + picture_path
self.set_wallpaper(picture_path)
print "Successfully set the picture as the wallpaper. :)"
self.show_notify()


def main():
parser = argparse.ArgumentParser(
prog='wonderful_bing',
description="Wonderful_bing's configuration")
parser.add_argument('-V', '--version', action='version',
version='%(prog)s {}'.format(__version__))
parser.add_argument(
'-d', dest='directory',
help="set the directory to save Bing's imgs, end with '/'")
config = vars(parser.parse_args())

bing = WonderfulBing(config)
try:
picture_url, ZH = get_picture_url("http://www.bing.com")
download_and_set(picture_url, ZH)
bing.download_and_set()
except requests.exceptions.ConnectionError:
print "ConnectionError,check your network please."
print "Will try again after 5 minutes."
time.sleep(300)
picture_url, ZH = get_picture_url("http://www.bing.com")
download_and_set(picture_url, ZH)
except TypeError:
print "Set the directory to save Bing's imgs first."
print "For more information, use --help."


parser = argparse.ArgumentParser(prog='wonderful_bing',
description="Wonderful_bing's configuration")
parser.add_argument('-V', '--version', action='version',
version='%(prog)s {}'.format(__version__))
parser.add_argument('-d', dest='directory',
help="set the directory to save Bing's imgs, end with '/'")
config = vars(parser.parse_args())
bing.download_and_set()


if __name__ == '__main__':
main()

0 comments on commit 7da3df0

Please sign in to comment.