Created
December 3, 2017 14:38
-
-
Save isoraqathedh/774320bb60457a5a651129fda6c1f54f to your computer and use it in GitHub Desktop.
Sun clock
This file contains hidden or 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/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/%w"))) | |
| 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