Skip to content

Instantly share code, notes, and snippets.

@rnbguy
Last active October 28, 2016 17:46
Show Gist options
  • Save rnbguy/8489a50098a0158d54f5be9c381bdd7c to your computer and use it in GitHub Desktop.
Save rnbguy/8489a50098a0158d54f5be9c381bdd7c to your computer and use it in GitHub Desktop.
Set images in wallpaper from Google EarthView
#!/usr/bin/env python3
"""
earthview_wallpaper.py
Get next images from Google EarthView (earthview.withgoogle.com)
and set it as the background wallpaper.
"""
import os
import shutil
import sys
import re
import json
import urllib.request
server_addr = "https://earthview.withgoogle.com"
wall_set_cmd = "gsettings set \
org.gnome.desktop.background \
picture-uri 'file://%s'"
download_folder = os.path.expanduser("~/Pictures/Wallpapers/earthview")
config_folder = os.path.expanduser("~/.config/earthview")
img_data = config_folder + "/last.json"
def create_dirs():
"""
Create necessary dirs
"""
os.makedirs(download_folder, 0o755, True)
os.makedirs(config_folder, 0o700, True)
def remove_dirs():
"""
Remove all dirs
"""
shutil.rmtree(download_folder)
shutil.rmtree(config_folder)
def get_link(main_page=False):
"""
Get URL of image
"""
if main_page:
print("fetching image from main page..")
rexp = r"<[^>]*intro__content__button explore[^<]*href=\"(.*)\"[^<]*>"
res = urllib.request.urlopen(server_addr)
raw_data = res.read().decode()
link_id = re.search(rexp, raw_data).group(1)
next_img = '/_api%s.json' % link_id
elif os.path.exists(img_data):
with open(img_data) as f:
json_data = json.load(f)
next_img = json_data['nextApi']
else:
next_img = '/_api/petermann-australia-2043.json'
res = urllib.request.urlopen(server_addr + next_img)
json_data = res.read().decode()
data = json.loads(json_data)
with open(img_data, 'w') as f:
f.write(json_data)
print(data['title'])
return data['downloadUrl']
def get_img(download_url):
"""
Download and save image
"""
filename = download_url.split("/")[-1]
local_path = download_folder + "/" + filename
urllib.request.urlretrieve(server_addr + download_url, local_path)
os.chmod(local_path, 0o755)
return local_path
def set_gnome_bg(img_path):
"""
Set wallpaper
"""
print("setting wallpaper..")
cmd = wall_set_cmd % img_path
status = os.system(cmd)
return status
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == '-d':
remove_dirs()
else:
create_dirs()
download_url = get_link(True)
local_path = get_img(download_url)
if set_gnome_bg(local_path) == 0:
print("wallpaper set.")
else:
print("something went wrong.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment