Skip to content

Instantly share code, notes, and snippets.

@isoraqathedh
Created March 27, 2017 19:05
Show Gist options
  • Save isoraqathedh/4680a7a92c0f5136f959184bfe2f6d77 to your computer and use it in GitHub Desktop.
Save isoraqathedh/4680a7a92c0f5136f959184bfe2f6d77 to your computer and use it in GitHub Desktop.
Display script for datex.
#!/usr/bin/python3
import os
import sys
import ephem
import datetime
import pytz
import colorlover as colours
import yaml
import math
def now(timezone_text):
return datetime.datetime.now(pytz.timezone(timezone_text))
def altitude(latitude, longitude, time):
place = ephem.Observer()
place.lat=str(latitude)
place.lon=str(longitude)
place.date=time.strftime("%Y-%m-%d %H:%M:%S")
return ephem.Sun(place).alt / math.pi * 180
script_dir = os.path.dirname(os.path.abspath(__file__))
timezoneList = script_dir + "/timezones.yaml"
with open(timezoneList, 'r') as configFile:
config = yaml.load(configFile)
selectedName = os.getenv("BLOCK_INSTANCE") or 'uni'
selectedTimezone = config[selectedName]['zone_name']
if os.getenv("BLOCK_BUTTON") == '1':
import notify2
notify2.init("datex")
notify2.Notification(
"Times across the world",
"\n".join(
["{}: {}".format(i, now(config[i]['zone_name']).strftime("%H:%M"))
for i in config.keys()])).show()
# Sunrise and sunset
nowAltitude = altitude(config[selectedName]['latitude'],
config[selectedName]['longitude'],
now("UTC"))
minaltitude = -7.5
maxaltitude = 7.5
# 135, 206, 235
if nowAltitude < minaltitude: # Night time
foreground = "FFFFFF"
background = "222222"
elif minaltitude < nowAltitude < maxaltitude:
# Sun is close to the horizon, time to colours.
steps = 150
index = math.floor((nowAltitude - minaltitude)
/ (maxaltitude - minaltitude) * steps)
settingp = nowAltitude > altitude(config[selectedName]['latitude'],
config[selectedName]['longitude'],
now("UTC") + datetime.timedelta(minutes = 3))
setCol = ['rgb(34,34,34)',
'rgb(175,65,14)',
'rgb(236,203,90)',
'rgb(114,136,124)',
'rgb(32,82,141)',
'rgb(135,206,235)']
riseCol = ['rgb(34,34,34)',
'rgb(114,113,171)',
'rgb(189,128,161)',
'rgb(246,133,127)',
'rgb(135,206,235)']
backgroundHSL = colours.interp(setCol if settingp else riseCol, steps)[index]
backgroundHSLnum = colours.to_numeric([backgroundHSL.replace("%", "")])[0]
backgroundRGBnum = colours.to_numeric(colours.to_rgb([backgroundHSL]))[0]
background = "{0[0]:02X}{0[1]:02X}{0[2]:02X}".format(
tuple(map(math.floor, backgroundRGBnum)))
if backgroundHSLnum[2] > 50:
foreground = "000000"
else:
foreground = "FFFFFF"
else:
foreground = "222222"
background = "87CEEB"
# Final print
pangoFmt ='<span fgcolor="#{}" bgcolor="#{}">{}</span>'
print(pangoFmt.format(foreground, background,
now(selectedTimezone).strftime("%Y-%m-%d %H:%M:%S %Z")))
print(pangoFmt.format(foreground, background,
now(selectedTimezone).strftime("%H:%M:%S")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment