2020-04-19 09:50:04 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays sunrise and sunset times
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
* suntime
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* cpu.lat : Latitude of your location
|
|
|
|
* cpu.lon : Longitude of your location
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
from suntime import Sun, SunTimeException
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
from dateutil.tz import tzlocal
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(
|
|
|
|
engine, config,
|
|
|
|
bumblebee.output.Widget(full_text=self.suntimes)
|
|
|
|
)
|
|
|
|
self.interval(3600)
|
2020-04-19 09:50:18 +02:00
|
|
|
self._lat = self.parameter('lat', None)
|
|
|
|
self._lon = self.parameter('lon', None)
|
2020-04-19 09:50:04 +02:00
|
|
|
try:
|
|
|
|
if not self._lat or not self._lon:
|
2020-04-19 09:50:18 +02:00
|
|
|
location_url = 'http://ipinfo.io/json'
|
2020-04-19 09:50:04 +02:00
|
|
|
location = requests.get(location_url).json()
|
2020-04-19 09:50:18 +02:00
|
|
|
self._lat, self._lon = location['loc'].split(',')
|
2020-04-19 09:50:04 +02:00
|
|
|
self._lat = float(self._lat)
|
|
|
|
self._lon = float(self._lon)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
self.update(None)
|
|
|
|
|
|
|
|
def suntimes(self, _):
|
|
|
|
if self._sunset and self._sunrise:
|
|
|
|
if self._isup:
|
2020-04-19 09:50:18 +02:00
|
|
|
return u'\u21A7{} \u21A5{}'.format(
|
2020-04-19 09:50:04 +02:00
|
|
|
self._sunset.strftime('%H:%M'),
|
|
|
|
self._sunrise.strftime('%H:%M'))
|
2020-04-19 09:50:18 +02:00
|
|
|
return u'\u21A5{} \u21A7{}'.format(self._sunrise.strftime('%H:%M'),
|
2020-04-19 09:50:04 +02:00
|
|
|
self._sunset.strftime('%H:%M'))
|
2020-04-19 09:50:18 +02:00
|
|
|
return '?'
|
2020-04-19 09:50:04 +02:00
|
|
|
|
|
|
|
def _calculate_times(self):
|
|
|
|
self._isup = False
|
|
|
|
try:
|
|
|
|
if not self._lat or not self._lon:
|
|
|
|
raise()
|
|
|
|
sun = Sun(self._lat, self._lon)
|
|
|
|
except Exception:
|
|
|
|
self._sunrise = None
|
|
|
|
self._sunset = None
|
|
|
|
return
|
|
|
|
|
|
|
|
order_matters = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._sunrise = sun.get_local_sunrise_time()
|
|
|
|
except SunTimeException:
|
2020-04-19 09:50:18 +02:00
|
|
|
self._sunrise = 'no sunrise'
|
2020-04-19 09:50:04 +02:00
|
|
|
order_matters = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._sunset = sun.get_local_sunset_time()
|
|
|
|
except SunTimeException:
|
2020-04-19 09:50:18 +02:00
|
|
|
self._sunset = 'no sunset'
|
2020-04-19 09:50:04 +02:00
|
|
|
order_matters = False
|
|
|
|
|
|
|
|
if not order_matters:
|
|
|
|
return
|
|
|
|
|
|
|
|
now = datetime.datetime.now(tz=tzlocal())
|
|
|
|
if now > self._sunset:
|
|
|
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
|
|
|
try:
|
|
|
|
self._sunrise = sun.get_local_sunrise_time(tomorrow)
|
|
|
|
self._sunset = sun.get_local_sunset_time(tomorrow)
|
|
|
|
except SunTimeException:
|
2020-04-19 09:50:18 +02:00
|
|
|
self._sunrise = 'no sunrise'
|
|
|
|
self._sunset = 'no sunset'
|
2020-04-19 09:50:04 +02:00
|
|
|
|
|
|
|
elif now > self._sunrise:
|
|
|
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
|
|
|
try:
|
|
|
|
self._sunrise = sun.get_local_sunrise_time(tomorrow)
|
|
|
|
except SunTimeException:
|
2020-04-19 09:50:18 +02:00
|
|
|
self._sunrise = 'no sunrise'
|
2020-04-19 09:50:04 +02:00
|
|
|
return
|
|
|
|
self._isup = True
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
if not self._lat or not self._lon:
|
|
|
|
self._sunrise = None
|
|
|
|
self._sunset = None
|
|
|
|
self._calculate_times()
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|