[modules/sun] Update to latest API
This commit is contained in:
parent
bd27c05fc7
commit
87fb82965f
1 changed files with 48 additions and 65 deletions
|
@ -11,107 +11,90 @@ Parameters:
|
||||||
* cpu.lon : Longitude of your location
|
* cpu.lon : Longitude of your location
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
|
||||||
from suntime import Sun, SunTimeException
|
from suntime import Sun, SunTimeException
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
import requests
|
import requests
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
from dateutil.tz import tzlocal
|
from dateutil.tz import tzlocal
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import bumblebee.input
|
import core.module
|
||||||
import bumblebee.output
|
import core.widget
|
||||||
import bumblebee.engine
|
import core.decorators
|
||||||
|
|
||||||
|
import util.location
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
class Module(core.module.Module):
|
||||||
def __init__(self, engine, config):
|
@core.decorators.every(hours=1)
|
||||||
super(Module, self).__init__(
|
def __init__(self, config):
|
||||||
engine, config,
|
super().__init__(config, core.widget.Widget(self.suntimes))
|
||||||
bumblebee.output.Widget(full_text=self.suntimes)
|
|
||||||
)
|
self.__lat = self.parameter('lat', None)
|
||||||
self.interval(3600)
|
self.__lon = self.parameter('lon', None)
|
||||||
self._lat = self.parameter('lat', None)
|
|
||||||
self._lon = self.parameter('lon', None)
|
if not self.__lat or not self.__lon:
|
||||||
try:
|
self.__lat, self.__lon = util.location.coordinates()
|
||||||
if not self._lat or not self._lon:
|
|
||||||
location_url = 'http://ipinfo.io/json'
|
|
||||||
location = requests.get(location_url).json()
|
|
||||||
self._lat, self._lon = location['loc'].split(',')
|
|
||||||
self._lat = float(self._lat)
|
|
||||||
self._lon = float(self._lon)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
self.update(None)
|
|
||||||
|
|
||||||
def suntimes(self, _):
|
def suntimes(self, _):
|
||||||
if self._sunset and self._sunrise:
|
if self.__sunset and self.__sunrise:
|
||||||
if self._isup:
|
if self.__isup:
|
||||||
return u'\u21A7{} \u21A5{}'.format(
|
return u'\u21A7{} \u21A5{}'.format(
|
||||||
self._sunset.strftime('%H:%M'),
|
self.__sunset.strftime('%H:%M'),
|
||||||
self._sunrise.strftime('%H:%M'))
|
self.__sunrise.strftime('%H:%M'))
|
||||||
return u'\u21A5{} \u21A7{}'.format(self._sunrise.strftime('%H:%M'),
|
return u'\u21A5{} \u21A7{}'.format(self.__sunrise.strftime('%H:%M'),
|
||||||
self._sunset.strftime('%H:%M'))
|
self.__sunset.strftime('%H:%M'))
|
||||||
return '?'
|
return 'n/a'
|
||||||
|
|
||||||
def _calculate_times(self):
|
def __calculate_times(self):
|
||||||
self._isup = False
|
self.__isup = False
|
||||||
try:
|
try:
|
||||||
if not self._lat or not self._lon:
|
if not self.__lat or not self.__lon:
|
||||||
raise()
|
raise()
|
||||||
sun = Sun(self._lat, self._lon)
|
sun = Sun(float(self.__lat), float(self.__lon))
|
||||||
except Exception:
|
except Exception:
|
||||||
self._sunrise = None
|
self.__sunrise = None
|
||||||
self._sunset = None
|
self.__sunset = None
|
||||||
return
|
return
|
||||||
|
|
||||||
order_matters = True
|
order_matters = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._sunrise = sun.get_local_sunrise_time()
|
self.__sunrise = sun.get_local_sunrise_time()
|
||||||
except SunTimeException:
|
except SunTimeException:
|
||||||
self._sunrise = 'no sunrise'
|
self.__sunrise = 'no sunrise'
|
||||||
order_matters = False
|
order_matters = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._sunset = sun.get_local_sunset_time()
|
self.__sunset = sun.get_local_sunset_time()
|
||||||
except SunTimeException:
|
except SunTimeException:
|
||||||
self._sunset = 'no sunset'
|
self.__sunset = 'no sunset'
|
||||||
order_matters = False
|
order_matters = False
|
||||||
|
|
||||||
if not order_matters:
|
if not order_matters:
|
||||||
return
|
return
|
||||||
|
|
||||||
now = datetime.datetime.now(tz=tzlocal())
|
now = datetime.datetime.now(tz=tzlocal())
|
||||||
if now > self._sunset:
|
if now > self.__sunset:
|
||||||
tomorrow = (now + datetime.timedelta(days=1)).date()
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
||||||
try:
|
try:
|
||||||
self._sunrise = sun.get_local_sunrise_time(tomorrow)
|
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
||||||
self._sunset = sun.get_local_sunset_time(tomorrow)
|
self.__sunset = sun.get_local_sunset_time(tomorrow)
|
||||||
except SunTimeException:
|
except SunTimeException:
|
||||||
self._sunrise = 'no sunrise'
|
self.__sunrise = 'no sunrise'
|
||||||
self._sunset = 'no sunset'
|
self.__sunset = 'no sunset'
|
||||||
|
|
||||||
elif now > self._sunrise:
|
elif now > self.__sunrise:
|
||||||
tomorrow = (now + datetime.timedelta(days=1)).date()
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
||||||
try:
|
try:
|
||||||
self._sunrise = sun.get_local_sunrise_time(tomorrow)
|
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
||||||
except SunTimeException:
|
except SunTimeException:
|
||||||
self._sunrise = 'no sunrise'
|
self.__sunrise = 'no sunrise'
|
||||||
return
|
return
|
||||||
self._isup = True
|
self.__isup = True
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self ):
|
||||||
if not self._lat or not self._lon:
|
if not self.__lat or not self.__lon:
|
||||||
self._sunrise = None
|
self.__sunrise = None
|
||||||
self._sunset = None
|
self.__sunset = None
|
||||||
self._calculate_times()
|
self.__calculate_times()
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue