From dd19653e7807961da76e0c96aa24503f55c6e464 Mon Sep 17 00:00:00 2001 From: Andrey Prokhorov Date: Mon, 21 Aug 2017 16:44:31 +0300 Subject: [PATCH] First commit --- getmetar.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ start_uwsgi.sh | 4 +++ uwsgi_metar.py | 37 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 getmetar.py create mode 100755 start_uwsgi.sh create mode 100644 uwsgi_metar.py diff --git a/getmetar.py b/getmetar.py new file mode 100644 index 0000000..88e9c31 --- /dev/null +++ b/getmetar.py @@ -0,0 +1,69 @@ +from bs4 import BeautifulSoup +import urllib.request + +def extract_metar(icao, info="metar"): + if info == "": + __info = "metar" + else: + __info = info.lower() + if __info != "metar" and __info != "taf" and __info != "all": + print("Sorry, wrong parameter specified") + quit() + + if len(str(icao)) > 4: + print("Sorry, " + icao + " is not correct ICAO airport code") + quit() + + code = str(icao) + metar_url = 'https://www.aviationweather.gov/metar/data?ids=' + metar_url_params = '&format=raw&date=0&hours=0&taf=on' + try: + page = urllib.request.urlopen(metar_url + code + metar_url_params).read() + except urllib.error.URLError: + return None + soup = BeautifulSoup(page, "html.parser") + + if __info == "metar" or __info == "all": + texts = soup.findAll(text=True) + METAR = None + for item in texts: + if item.strip().find(code) == 0: + METAR = item.strip() + break + if __info == "metar": + if METAR is None: + return None + else: + METAR = METAR.replace("\r", "") + METAR = METAR.replace("\n", "") + return METAR + + if __info == "taf" or __info == "all": + try: + TAF = soup.body.code.get_text() + TAFc = "" + flag_160 = False + for i in TAF: + if ord(i) != 160: + TAFc += i + flag_160 = False + else: + if flag_160 == False: + TAFc += "\n\t" + flag_160 = True + else: + pass + except AttributeError: + return None + if __info == "taf": + return TAFc + + if __info == "all": + if METAR is None: + return "No METAR info \n" + TAFc + else: + return METAR + "\n" + TAFc + + +if __name__ == "__main__": + print(extract_metar("UKDD", "metar")) diff --git a/start_uwsgi.sh b/start_uwsgi.sh new file mode 100755 index 0000000..8c469c5 --- /dev/null +++ b/start_uwsgi.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cd /home/stat/metar +uwsgi --socket 127.0.0.1:9000 --wsgi-file uwsgi_metar.py --master --process 1 --threads 1 --stats 127.0.0.1:9001 > /dev/null 2>> uwsgi.log & diff --git a/uwsgi_metar.py b/uwsgi_metar.py new file mode 100644 index 0000000..f44086d --- /dev/null +++ b/uwsgi_metar.py @@ -0,0 +1,37 @@ +import getmetar +from cgi import parse_qs + +def application(env, start_response): + d = parse_qs(env['QUERY_STRING']) + try: + station = d.get('station', '')[0] + except: + station = "" + #start_response('200 OK', [('Content-Type', 'text/plain')]) +# return str.encode(station) + metar = "" + if station == "": + metar = getmetar.extract_metar("UKBB") + else: + metar = getmetar.extract_metar(station.upper()) + html = "" + html += "" + html += "" + html += "" + html += "" + html += metar + "
" + html += "" + start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(html)))]) + + return str.encode(html) + +def empty_func(a,b): + return None + + +if __name__ == "__main__": + x = None + y = empty_func + print(application(x,y))