Skip to content

Instantly share code, notes, and snippets.

@queeup
Last active December 24, 2019 10:16
Show Gist options
  • Select an option

  • Save queeup/3c50398b5fd2dedd564555591b7c6d63 to your computer and use it in GitHub Desktop.

Select an option

Save queeup/3c50398b5fd2dedd564555591b7c6d63 to your computer and use it in GitHub Desktop.
Auto wallpaper changer for National Geographic - Photo of the day
[Desktop Entry]
Type=Application
Exec=natgeo-photo_of_the_day.py
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=NatGeo Photo of the day - Wallpaper
Comment=Auto wallpaper changer for National Geographic - Photo of the day
#!/usr/bin/env python3
import json
import os
import schedule # pip install schedule
import subprocess
import time
from base64 import b64decode
from datetime import datetime
from urllib import request
from urllib.error import URLError, HTTPError
DEBUG = False
URL = 'aHR0cHM6Ly93d3cubmF0aW9uYWxnZW9ncmFwaGljLmNvbS9waG90b2dyYXBoeS9waG90by1vZi10aGUtZGF5L19qY3JfY29udGVudC8uZ2FsbGVyeS5qc29u'
def _get_url(url):
# if network is not ready (wakeup from suspend etc.), loop until connection to establish.
while True:
try:
response = request.urlopen(url)
except URLError or HTTPError:
time.sleep(1)
else:
# TODO: Find a elegant way to check VPN connection is up after wake from suspend and then execute
time.sleep(2) # wait for VPN connection after wake from suspend.
data = json.load(response)['items'][0]['image']
return data['title'], data['id'], data['uri']
def _wallpaper_file():
title, _id, photo_of_the_day_url = _get_url(b64decode(URL).decode('utf-8'))
background_folder = '{}/.local/share/backgrounds/NatGeo - Photo of the Day/'.format(os.environ['HOME'])
background_file = '{}.jpg'.format(_id)
if not os.path.exists(background_folder):
os.mkdir(background_folder)
return background_folder + background_file, photo_of_the_day_url
def _download_picture(photo_of_the_day_url, background_file):
img_data = request.urlopen(photo_of_the_day_url)
with open(background_file, 'wb') as f:
f.write(img_data.read())
def _set_wallpaper(background_file):
file = "'file://{}'".format(background_file)
subprocess.call(['gsettings', 'set', 'org.gnome.desktop.background', 'picture-options', "'zoom'"])
subprocess.call(['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri', file])
def _changing_wallpaper(message):
background_file, photo_of_the_day_url = _wallpaper_file()
if not os.path.isfile(background_file):
if DEBUG:
print(message)
_download_picture(photo_of_the_day_url, background_file)
_set_wallpaper(background_file)
else:
if DEBUG:
print('NatGeo - Photo of the Day: Wallpaper already downloaded or not changed since last updated. Using downloaded wallpaper.')
_set_wallpaper(background_file)
def _bg_change_time():
# Photo of the day updated at 5AM UTC everyday. So calculate it for any timezone.
time_difference = datetime.utcnow() - datetime.utcnow().replace(hour=5, minute=0, second=0, microsecond=0)
return (datetime.now() - time_difference).strftime('%H:%M')
_changing_wallpaper('NatGeo - Photo of the Day: First run. Setting wallpaper.') # set wallpaper on first run
schedule.every().day.at(_bg_change_time()).do(_changing_wallpaper, 'NatGeo - Photo of the Day: Scheduled change wallpaper.')
while True:
schedule.run_pending()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment