From 14115035afc65f44cd0edba68bf6db79a3103cb9 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 19 Mar 2014 08:30:51 +0100 Subject: add weather support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- DEPENDENCIES | 3 ++ TODO | 3 +- dist/archlinux/stable/PKGBUILD | 3 +- src/weather.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/weather.py diff --git a/DEPENDENCIES b/DEPENDENCIES index c0a386d..7469f77 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -19,6 +19,9 @@ RUNTIME DEPENDENCIES: linux, adjbacklight (optional) For permission-hasslefree backlight adjustments via sysfs + wget (optional) + For weather conditions + MAKE DEPENDENCIES: diff --git a/TODO b/TODO index 0d854a8..b677aec 100644 --- a/TODO +++ b/TODO @@ -5,8 +5,7 @@ High priority: Medium priority: Test and demo _ICC_PROFILE Demo functionise - Make it possible to have settings depend on sky condicitions. - (I have a GPL3 implmenetion of wheter observation in xpybar.) + Demo and document weather Low priority: Add a section in manual for information on which order diff --git a/dist/archlinux/stable/PKGBUILD b/dist/archlinux/stable/PKGBUILD index ccf5ce6..595f481 100644 --- a/dist/archlinux/stable/PKGBUILD +++ b/dist/archlinux/stable/PKGBUILD @@ -9,7 +9,8 @@ url="https://github.com/maandree/blueshift" license=('AGPL3' 'GPL3' 'custom:GFDL1.3') depends=(python3 argparser libxcb libxxf86vm libx11 libdrm) optdepends=('adjbacklight: for backlight adjustments without root requirements' - 'linux: for backlight support') + 'linux: for backlight support' + 'wget: for weather support') makedepends=(cython gcc python3 libxcb libxxf86vm libx11 libdrm make coreutils sed zip texinfo auto-auto-complete) install=blueshift.install source=($url/archive/$pkgver.tar.gz) diff --git a/src/weather.py b/src/weather.py new file mode 100644 index 0000000..c3aa2f9 --- /dev/null +++ b/src/weather.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from subprocess import Popen, PIPE + + +def weather(station): + ''' + Get a brief weather report + + @param station:str The station's International Civil Aviation Organization airport code + @return :(sky:str, visiblity:(:int, :float)?, weather:list) + The sky condition, visiblity and weather. Sky condition values include + ‘clear’, ‘mostly clear’, ‘overcast’, ‘partly cloudy’, ‘mostly cloudy’ + and ‘obscured’. The visibility consists of two parameters: the first + on indicates the the visiblity is a upper bound if the value is -1, + a lower bound if +1, and approximate if 0; the second parameter is the + visibility in kilometers. If the visibility is unknown the value will + be `None`. The weather is a list that can, and often is, empty. + ''' + url = 'http://weather.noaa.gov/pub/data/observations/metar/decoded/%s.TXT' + url %= station.upper() + proc = Popen(['wget', url, '-O', '-'], stdout = PIPE, stderr = PIPE) + output = proc.communicate()[0] + if not proc.returncode == 0: + return None + output = output.decode('utf-8', 'replace').split('\n') + output = [line.lower().split(': ') for line in output if ': ' in line] + output = dict([(line[0], ': '.join(line[1:])) for line in output]) + sky_conditions = 'clear' if 'sky conditions' not in output else output['sky conditions'] + visibility = None + try: + if 'visibility' in output: + visibility = output['visibility'].split(':')[0] + visibility = visibility.replace(' mile(s)', '') + visibility = visibility.replace(' miles', '') + visibility = visibility.replace(' mile', '') + visibility_eq = 0 + if visibility.startswith('greater than '): + visibility_eq = 1 + visibility = visibility[len('greater than '):] + if visibility.startswith('less than '): + visibility_eq = -1 + visibility = visibility[len('less than '):] + if len(list(filter(lambda c : not (('0' <= c <= '9') or (c in ' /')), visibility))) == 0: + visibility = sum([eval(v) for v in visibility.split(' ')]) + visibility = (visibility_eq, visibility * 1.609) + else: + visibility = None + except: + visibility = None + weather = '' if 'weather' not in output else output['weather'] + weather = weather.replace(',', ';').replace(' with ', ';') + weather = weather.replace(' in the vicinity', '') + weather = weather.replace(' observed', '') + weather = weather.replace(' during the past hour', '') + weather = [w.replace(';', '').strip() for w in weather.split(';') if not w == ''] + return (sky_conditions, visibility, weather) + -- cgit v1.2.3-70-g09d2