From 74283de5f5a677b7ee039c395b8d2c3e93ed8d44 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sat, 7 Nov 2020 19:57:30 +0100 Subject: [PATCH] Bind v3 --- bin/srv.py | 10 +++++++++- lib/view/line.py | 6 +++++- lib/wttr_srv.py | 25 ++++++++++++++----------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/bin/srv.py b/bin/srv.py index 8d4f447..83bccf4 100644 --- a/bin/srv.py +++ b/bin/srv.py @@ -10,7 +10,7 @@ import sys import os import jinja2 -from flask import Flask, request, send_from_directory +from flask import Flask, request, send_from_directory, send_file APP = Flask(__name__) MYDIR = os.path.abspath( @@ -21,6 +21,7 @@ import wttr_srv from globals import TEMPLATES, STATIC, LISTEN_HOST, LISTEN_PORT # pylint: enable=wrong-import-position,wrong-import-order +from view.v3 import v3_file MY_LOADER = jinja2.ChoiceLoader([ APP.jinja_loader, @@ -29,6 +30,13 @@ MY_LOADER = jinja2.ChoiceLoader([ APP.jinja_loader = MY_LOADER +@APP.route('/v3/') +def send_v3(location): + filepath = v3_file(location) + if filepath.startswith("ERROR"): + return filepath.rstrip("\n") + "\n" + return send_file(filepath) + @APP.route('/files/') def send_static(path): "Send any static file located in /files/" diff --git a/lib/view/line.py b/lib/view/line.py index 24da1a2..086ebd6 100644 --- a/lib/view/line.py +++ b/lib/view/line.py @@ -28,6 +28,7 @@ import pytz from constants import WWO_CODE, WEATHER_SYMBOL, WIND_DIRECTION, WEATHER_SYMBOL_WIDTH_VTE from weather_data import get_weather_data from . import v2 +from . import v3 from . import prometheus PRECONFIGURED_FORMAT = { @@ -341,11 +342,14 @@ def format_weather_data(query, parsed_query, data): return prometheus.render_prometheus(data['data']) if format_line[:2] == "v2": return v2.main(query, parsed_query, data) + if format_line[:2] == "v3": + return v3.main(query, parsed_query, data) current_condition = data['data']['current_condition'][0] current_condition['location'] = parsed_query["location"] current_condition['override_location'] = parsed_query["override_location_name"] output = render_line(format_line, current_condition, query) + output = output.rstrip("\n").replace(r"\n", "\n") return output def wttr_line(query, parsed_query): @@ -358,7 +362,7 @@ def wttr_line(query, parsed_query): data = get_weather_data(location, lang) output = format_weather_data(query, parsed_query, data) - return output.rstrip("\n").replace(r"\n", "\n") + return output def main(): """ diff --git a/lib/wttr_srv.py b/lib/wttr_srv.py index 5ac1eac..f8a7697 100644 --- a/lib/wttr_srv.py +++ b/lib/wttr_srv.py @@ -133,7 +133,7 @@ def get_answer_language_and_view(request): hostname = request.headers['Host'] if hostname != 'wttr.in' and hostname.endswith('.wttr.in'): lang = hostname[:-8] - if lang.startswith("v2"): + if lang.startswith("v2") or lang.startswith("v3"): view_name = lang lang = None @@ -155,7 +155,9 @@ def get_output_format(query, parsed_query): Return new location (can be rewritten) """ - if ('view' in query and not query["view"].startswith("v2")) \ + if ('view' in query + and not query["view"].startswith("v2") + and not query["view"].startswith("v3")) \ or parsed_query.get("png_filename") \ or query.get('force-ansi'): return False @@ -215,18 +217,19 @@ def _response(parsed_query, query, fast_mode=False): output = get_wetter(parsed_query) if parsed_query.get('png_filename'): - # originally it was just a usual function call, - # but it was a blocking call, so it was moved - # to separate threads: - # - # output = fmt.png.render_ansi( - # output, options=parsed_query) - result = TASKS.spawn(fmt.png.render_ansi, cache._update_answer(output), options=parsed_query) - output = result.get() + if parsed_query.get("view") != "v3": + # originally it was just a usual function call, + # but it was a blocking call, so it was moved + # to separate threads: + # + # output = fmt.png.render_ansi( + # output, options=parsed_query) + result = TASKS.spawn(fmt.png.render_ansi, cache._update_answer(output), options=parsed_query) + output = result.get() else: if query.get('days', '3') != '0' \ and not query.get('no-follow-line') \ - and ((parsed_query.get("view") or "v2")[:2] in ["v2"]): + and ((parsed_query.get("view") or "v2")[:2] in ["v2", "v3"]): if parsed_query['html_output']: output = add_buttons(output) else: