Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import os
  4. import sys
  5. import ephem
  6. import datetime
  7. import pytz
  8. import colorlover as colours
  9. import yaml
  10. import math
  11.  
  12. def now(timezone_text):
  13. return datetime.datetime.now(pytz.timezone(timezone_text))
  14. def altitude(latitude, longitude, time):
  15. place = ephem.Observer()
  16. place.lat=str(latitude)
  17. place.lon=str(longitude)
  18. place.date=time.strftime("%Y-%m-%d %H:%M:%S")
  19. return ephem.Sun(place).alt / math.pi * 180
  20.  
  21. script_dir = os.path.dirname(os.path.abspath(__file__))
  22. timezoneList = script_dir + "/timezones.yaml"
  23. with open(timezoneList, 'r') as configFile:
  24. config = yaml.load(configFile)
  25. selectedName = os.getenv("BLOCK_INSTANCE") or 'uni'
  26. selectedTimezone = config[selectedName]['zone_name']
  27.  
  28. if os.getenv("BLOCK_BUTTON") == '1':
  29. import notify2
  30. notify2.init("datex")
  31. notify2.Notification(
  32. "Times across the world",
  33. "\n".join(
  34. ["{}: {}".format(i, now(config[i]['zone_name']).strftime("%H:%M"))
  35. for i in config.keys()])).show()
  36.  
  37. # Sunrise and sunset
  38. nowAltitude = altitude(config[selectedName]['latitude'],
  39. config[selectedName]['longitude'],
  40. now("UTC"))
  41. minaltitude = -7.5
  42. maxaltitude = 7.5
  43.  
  44. # 135, 206, 235
  45. if nowAltitude < minaltitude: # Night time
  46. foreground = "FFFFFF"
  47. background = "222222"
  48. elif minaltitude < nowAltitude < maxaltitude:
  49. # Sun is close to the horizon, time to colours.
  50. steps = 150
  51. index = math.floor((nowAltitude - minaltitude)
  52. / (maxaltitude - minaltitude) * steps)
  53. settingp = nowAltitude > altitude(config[selectedName]['latitude'],
  54. config[selectedName]['longitude'],
  55. now("UTC") + datetime.timedelta(minutes = 3))
  56. setCol = ['rgb(34,34,34)',
  57. 'rgb(175,65,14)',
  58. 'rgb(236,203,90)',
  59. 'rgb(114,136,124)',
  60. 'rgb(32,82,141)',
  61. 'rgb(135,206,235)']
  62. riseCol = ['rgb(34,34,34)',
  63. 'rgb(114,113,171)',
  64. 'rgb(189,128,161)',
  65. 'rgb(246,133,127)',
  66. 'rgb(135,206,235)']
  67. backgroundHSL = colours.interp(setCol if settingp else riseCol, steps)[index]
  68. backgroundHSLnum = colours.to_numeric([backgroundHSL.replace("%", "")])[0]
  69. backgroundRGBnum = colours.to_numeric(colours.to_rgb([backgroundHSL]))[0]
  70. background = "{0[0]:02X}{0[1]:02X}{0[2]:02X}".format(
  71. tuple(map(math.floor, backgroundRGBnum)))
  72. if backgroundHSLnum[2] > 50:
  73. foreground = "000000"
  74. else:
  75. foreground = "FFFFFF"
  76. else:
  77. foreground = "222222"
  78. background = "87CEEB"
  79.  
  80. # Final print
  81. pangoFmt ='<span fgcolor="#{}" bgcolor="#{}">{}</span>'
  82. print(pangoFmt.format(foreground, background,
  83. now(selectedTimezone).strftime("%Y-%m-%d %H:%M:%S %Z")))
  84. print(pangoFmt.format(foreground, background,
  85. now(selectedTimezone).strftime("%H:%M:%S")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement