From 0d76ba4a3e112694665af6653040807835883b22 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Fri, 2 Nov 2018 18:52:42 +0100 Subject: [PATCH] new status line functions: moonphase, moonday --- lib/wttr_line.py | 27 ++++++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/wttr_line.py b/lib/wttr_line.py index e7e75d0..3f2c56f 100644 --- a/lib/wttr_line.py +++ b/lib/wttr_line.py @@ -15,6 +15,8 @@ Initial implementation of one-line output mode. import sys import re +import datetime +from astral import Astral from constants import WWO_CODE, WEATHER_SYMBOL, WIND_DIRECTION from weather_data import get_weather_data @@ -25,6 +27,10 @@ PRECONFIGURED_FORMAT = { '4': u'%l: %c 🌡️%t 🌬️%w', } +MOON_PHASES = ( + u"🌑", u"🌒", u"🌓", u"🌔", u"🌕", u"🌖", u"🌗", u"🌘" +) + def render_temperature(data): """ temperature (t) @@ -74,11 +80,30 @@ def render_location(data): return data['location'].title() +def render_moonphase(_): + """ + A symbol describing the phase of the moon + """ + astral = Astral() + moon_index = int( + int(32.0*astral.moon_phase(date=datetime.datetime.today())/28+2)%32/4 + ) + return MOON_PHASES[moon_index] + +def render_moonday(_): + """ + An number describing the phase of the moon (days after the New Moon) + """ + astral = Astral() + return str(int(astral.moon_phase(date=datetime.datetime.today()))) + FORMAT_SYMBOL = { 'c': render_condition, 't': render_temperature, 'w': render_wind, 'l': render_location, + 'm': render_moonphase, + 'M': render_moonday, } def render_line(line, data): @@ -101,7 +126,7 @@ def render_line(line, data): render_function = FORMAT_SYMBOL[symbol] return render_function(data) - return re.sub(r'%[^%]*[a-z]', render_symbol, line) + return re.sub(r'%[^%]*[a-zA-Z]', render_symbol, line) def format_weather_data(format_line, location, data): """ diff --git a/requirements.txt b/requirements.txt index 21f3a1b..21f40e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ gevent dnspython pylint cyrtranslit +astral