-
-
Save ki9us/4b032f5dc50b31e0ea6054fada0298b7 to your computer and use it in GitHub Desktop.
Change Wallpaper based on current weather condition and time of day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
####################################################\ | |
## ### | |
## flickr-weather-wallpaper.py ### | |
## v 2.0 ### | |
## Flickr Weather Wallpaper Setter ### | |
## For python 3.x ### | |
## ### | |
## Sets desktop background to image from flickr ### | |
## API based on current weather conditions and ### | |
## time of day. ### | |
## ### | |
## Keith Irwin 2016 ### | |
## (https://keithirwin.us/contact) ### | |
## ### | |
## Inspired by: kshwetabh ### | |
## (https://gist.github.com/kshwetabh/1579644) ### | |
## ### | |
##################################################### | |
####################################################/ | |
####################### IMPORTS ##################### | |
import requests | |
from random import choice | |
from os import system | |
from time import time | |
from xml.etree import ElementTree | |
##################################################### | |
################## SET THESE VALUES ################# | |
## Location | |
coordinates = "39.0000000,-74.0000000" | |
# Get a flickr api key at (https://www.flickr.com/services/apps/create/apply/) | |
flickr_api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
flickr_api_secret = "XXXXXXXXXXXXXXXX" | |
# Get a Dark Sky API key at (https://developer.forecast.io/) | |
darksky_api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" | |
##################################################### | |
## Get weather data | |
print("Getting weather...") | |
darksky_res = requests.get( | |
"https://api.forecast.io/forecast/{}/{}"\ | |
.format(darksky_api_key,coordinates)).json() | |
## And parse it | |
# see (https://developer.forecast.io/docs/v2) for more information | |
condition = str(darksky_res['currently']['summary']) | |
sunrise = int(darksky_res['daily']['data'][0]['sunriseTime']) | |
sunset = int(darksky_res['daily']['data'][0]['sunsetTime']) | |
## Get time of day | |
now = int(time()) | |
if abs(now-sunset)<1800: | |
altitude = "sunset" | |
elif abs(now-sunrise)<1800: | |
altitude = "sunrise" | |
elif now-sunrise>0 and now-sunset<0: | |
altitude = "day" | |
else: | |
altitude = "night" | |
condition = "astrophotography" | |
## Search for flickr images | |
flickr_res = requests.get( | |
"https://api.flickr.com/services/rest/?method={}&api_key={}&tags=wallpaper,{}&text={}"\ | |
.format("flickr.photos.search",flickr_api_key, altitude, condition)) | |
print("https://api.flickr.com/services/rest/?method={}&api_key={}&tags=wallpaper,{}&text={}"\ | |
.format("flickr.photos.search",flickr_api_key, altitude, condition)) | |
## And parse them | |
flickr_res_tree = ElementTree.fromstring(flickr_res.content) | |
photos = flickr_res_tree.findall("./photos/photo") | |
## Ensure at least one photo was returned | |
if len(photos)<1: | |
print("No photos found!") | |
print("Response: "+flickr_res) | |
else: | |
print("{} photos found".format(len(photos))) | |
## Pick one | |
photo = choice(photos).attrib | |
## Create photo url ('b' appears to be the highest quality flickr allows) | |
url = "https://farm{}.staticflickr.com/{}/{}_{}_b.jpg"\ | |
.format(photo['farm'],photo['server'],photo['id'],photo['secret']) | |
print('Setting wallpaper to '+url) | |
## Set wallpaper ## | |
# gnome (untested) | |
#system("\ | |
# gsettings set org.gnome.desktop.background picture-uri "+url) | |
# xfce >4.12.3 | |
system("\ | |
curl --silent {} > \ | |
/tmp/weather-wallpaper.jpg && \ | |
xfconf-query \ | |
--channel xfce4-desktop \ | |
--property /backdrop/screen0/monitor0/workspace0/last-image \ | |
--set /tmp/weather-wallpaper.jpg" | |
.format(url)) | |
print('Wallpaper set.') | |
##################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment