From da33d295d389dac93a04c2b666daf868e4f2b54a Mon Sep 17 00:00:00 2001 From: Lonesome byte Date: Sun, 11 Aug 2019 18:21:58 +0200 Subject: [PATCH] Sun module Module that shows the sunrise and sunset time for your location. http://ipinfo.io is used if parameter lat (latitude) or lon (longitude) is not provided, --- README.md | 3 +- bumblebee/modules/sun.py | 66 +++++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 3 ++ themes/icons/ionicons.json | 3 ++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 bumblebee/modules/sun.py diff --git a/README.md b/README.md index a20ad1f..12b9de0 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ Modules and commandline utilities are only required for modules, the core itself * psutil (for the modules 'cpu', 'memory', 'traffic') * netifaces (for the modules 'nic', 'traffic') -* requests (for the modules 'weather', 'github', 'getcrypto', 'stock', 'currency') +* requests (for the modules 'weather', 'github', 'getcrypto', 'stock', 'currency', 'sun') * power (for the module 'battery') * dbus (for the module 'spotify') * i3ipc (for the module 'title') @@ -195,6 +195,7 @@ Modules and commandline utilities are only required for modules, the core itself * docker (for the module 'docker_ps') * pytz (for the module 'datetimetz') * localtz (for the module 'datetimetz') +* suntime (for the module 'sun') # Required commandline utilities diff --git a/bumblebee/modules/sun.py b/bumblebee/modules/sun.py new file mode 100644 index 0000000..2f62079 --- /dev/null +++ b/bumblebee/modules/sun.py @@ -0,0 +1,66 @@ +# pylint: disable=C0111,R0903 + +"""Displays sunrise and sunset times + +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 + +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) + self._lat = self.parameter("lat", None) + self._lon = self.parameter("lon", None) + try: + 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, _): + if self._sunset and self._sunrise: + return u"\u21A5{} \u21A7{}".format(self._sunrise.strftime('%H:%M'), self._sunset.strftime('%H:%M')) + return "?" + + def update(self, widgets): + if not self._lat or not self._lon: + self._sunrise = None + self._sunset = None + try: + sun = Sun(self._lat, self._lon) + try: + self._sunrise = sun.get_local_sunrise_time() + except SunTimeException: + self._sunrise = 'no sunrise' + + try: + self._sunset = sun.get_local_sunset_time() + except SunTimeException: + self._sunset = 'no sunset' + except Exception: + self._sunrise = None + self._sunset = None + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index 9fbec5d..c6c9e3a 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -216,5 +216,8 @@ }, "system": { "prefix": "  " + }, + "sun": { + "prefix": "" } } diff --git a/themes/icons/ionicons.json b/themes/icons/ionicons.json index a21e264..6539986 100644 --- a/themes/icons/ionicons.json +++ b/themes/icons/ionicons.json @@ -179,5 +179,8 @@ }, "system": { "prefix": " \uf2a9 " + }, + "sun": { + "prefix": "\uf3b0" } }