wttr.in/lib/wttr_srv.py

359 lines
12 KiB
Python
Raw Normal View History

2018-10-05 14:54:14 +00:00
#!/usr/bin/env python
# vim: set encoding=utf-8
"""
Main wttr.in rendering function implementation
"""
2018-10-05 14:54:14 +00:00
import logging
2020-04-05 11:52:25 +00:00
import io
2018-10-05 14:54:14 +00:00
import os
2018-11-02 17:15:15 +00:00
import time
from flask import render_template, send_file, make_response
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
import fmt.png
2018-10-05 18:19:49 +00:00
import parse_query
2018-10-05 14:54:14 +00:00
from translations import get_message, FULL_TRANSLATION, PARTIAL_TRANSLATION, SUPPORTED_LANGS
from buttons import add_buttons
2020-04-05 09:51:36 +00:00
from globals import get_help_file, \
BASH_FUNCTION_FILE, TRANSLATION_FILE, LOG_FILE, \
2018-10-05 14:54:14 +00:00
NOT_FOUND_LOCATION, \
MALFORMED_RESPONSE_HTML_PAGE, \
PLAIN_TEXT_AGENTS, PLAIN_TEXT_PAGES, \
MY_EXTERNAL_IP, QUERY_LIMITS
from location import is_location_blocked, location_processing
2018-10-05 20:37:35 +00:00
from limits import Limits
2020-04-05 09:51:36 +00:00
from view.wttr import get_wetter
from view.moon import get_moon
2020-04-04 12:22:26 +00:00
from view.line import wttr_line
2018-10-05 14:54:14 +00:00
import cache
2018-10-05 14:54:14 +00:00
if not os.path.exists(os.path.dirname(LOG_FILE)):
os.makedirs(os.path.dirname(LOG_FILE))
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s %(message)s')
2018-10-05 14:54:14 +00:00
LIMITS = Limits(whitelist=[MY_EXTERNAL_IP], limits=QUERY_LIMITS)
2018-10-05 18:19:49 +00:00
2018-10-07 10:14:23 +00:00
def show_text_file(name, lang):
"""
show static file `name` for `lang`
"""
text = ""
if name == ":help":
text = open(get_help_file(lang), 'r').read()
text = text.replace('FULL_TRANSLATION', ' '.join(FULL_TRANSLATION))
text = text.replace('PARTIAL_TRANSLATION', ' '.join(PARTIAL_TRANSLATION))
elif name == ":bash.function":
text = open(BASH_FUNCTION_FILE, 'r').read()
elif name == ":translation":
text = open(TRANSLATION_FILE, 'r').read()
text = text\
.replace('NUMBER_OF_LANGUAGES', str(len(SUPPORTED_LANGS)))\
.replace('SUPPORTED_LANGUAGES', ' '.join(SUPPORTED_LANGS))
2020-04-03 19:49:51 +00:00
return text
2018-10-07 10:14:23 +00:00
2020-04-05 09:51:36 +00:00
def _client_ip_address(request):
"""Return client ip address for flask `request`.
2018-10-05 18:19:49 +00:00
"""
2018-10-05 14:54:14 +00:00
if request.headers.getlist("X-PNG-Query-For"):
ip_addr = request.headers.getlist("X-PNG-Query-For")[0]
if ip_addr.startswith('::ffff:'):
ip_addr = ip_addr[7:]
elif request.headers.getlist("X-Forwarded-For"):
ip_addr = request.headers.getlist("X-Forwarded-For")[0]
if ip_addr.startswith('::ffff:'):
ip_addr = ip_addr[7:]
else:
ip_addr = request.remote_addr
2018-10-05 14:54:14 +00:00
return ip_addr
2018-10-05 14:54:14 +00:00
def _parse_language_header(header):
2018-10-05 18:19:49 +00:00
"""
>>> _parse_language_header("en-US,en;q=0.9")
>>> _parse_language_header("en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
>>> _parse_language_header("xx, fr-CA;q=0.8, da-DK;q=0.9")
'da'
2018-10-05 18:19:49 +00:00
"""
2018-10-05 14:54:14 +00:00
def _parse_accept_language(accept_language):
languages = accept_language.split(",")
locale_q_pairs = []
for language in languages:
try:
if language.split(";")[0] == language:
# no q => q = 1
2019-05-26 02:24:12 +00:00
locale_q_pairs.append((language.strip(), 1))
else:
locale = language.split(";")[0].strip()
2019-05-26 02:24:12 +00:00
weight = float(language.split(";")[1].split("=")[1])
locale_q_pairs.append((locale, weight))
2019-05-26 02:24:12 +00:00
except (IndexError, ValueError):
pass
return locale_q_pairs
def _find_supported_language(accepted_languages):
2019-05-26 02:24:12 +00:00
def supported_langs():
"""Yields all pairs in the Accept-Language header
supported in SUPPORTED_LANGS or None if 'en' is the preferred"""
for lang_tuple in accepted_languages:
lang = lang_tuple[0]
if '-' in lang:
lang = lang.split('-', 1)[0]
if lang in SUPPORTED_LANGS:
yield lang, lang_tuple[1]
elif lang == 'en':
yield None, lang_tuple[1]
try:
2020-04-05 09:51:36 +00:00
return max(supported_langs(), key=lambda lang_tuple: lang_tuple[1])[0]
2019-05-26 02:24:12 +00:00
except ValueError:
return None
2018-10-05 14:54:14 +00:00
return _find_supported_language(_parse_accept_language(header))
2020-04-05 09:51:36 +00:00
def get_answer_language_and_view(request):
"""
Return preferred answer language based on
domain name, query arguments and headers
"""
lang = None
2020-04-05 09:51:36 +00:00
view_name = None
hostname = request.headers['Host']
if hostname != 'wttr.in' and hostname.endswith('.wttr.in'):
lang = hostname[:-8]
if lang == "v2":
2020-04-05 09:51:36 +00:00
view_name = "v2"
lang = None
2018-10-05 14:54:14 +00:00
if 'lang' in request.args:
lang = request.args.get('lang')
2018-10-05 14:54:14 +00:00
header_accept_language = request.headers.get('Accept-Language', '')
if lang is None and header_accept_language:
lang = _parse_language_header(header_accept_language)
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
return lang, view_name
2018-10-05 14:54:14 +00:00
def get_output_format(request, query):
"""
Return preferred output format: ansi, text, html or png
based on arguments and headers in `request`.
Return new location (can be rewritten)
"""
2018-10-05 14:54:14 +00:00
if 'format' in query:
return False
user_agent = request.headers.get('User-Agent', '').lower()
if query.get('force-ansi'):
return False
html_output = not any(agent in user_agent for agent in PLAIN_TEXT_AGENTS)
return html_output
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
def _cyclic_location_selection(locations, period):
"""Return one of `locations` (: separated list)
2018-11-02 17:14:48 +00:00
basing on the current time and query interval `period`
"""
locations = locations.split(':')
max_len = max(len(x) for x in locations)
locations = [x.rjust(max_len) for x in locations]
try:
period = int(period)
except ValueError:
period = 1
2020-04-05 09:51:36 +00:00
index = int(time.time()/period) % len(locations)
2018-11-02 17:14:48 +00:00
return locations[index]
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
def _response(parsed_query, query, fast_mode=False):
"""Create response text based on `parsed_query` and `query` data.
If `fast_mode` is True, process only requests that can
be handled very fast (cached and static files).
2018-10-05 14:54:14 +00:00
"""
2020-04-05 09:51:36 +00:00
answer = None
cache_signature = cache.get_signature(
parsed_query["user_agent"],
parsed_query["request_url"],
parsed_query["ip_addr"],
parsed_query["lang"])
answer = cache.get(cache_signature)
if parsed_query['orig_location'] in PLAIN_TEXT_PAGES:
answer = show_text_file(parsed_query['orig_location'], parsed_query['lang'])
if parsed_query['html_output']:
answer = render_template('index.html', body=answer)
if answer or fast_mode:
return answer
# at this point, we could not handle the query fast,
# so we handle it with all available logic
if parsed_query["view"] or 'format' in query:
response_text = wttr_line(query, parsed_query)
2020-04-05 09:51:36 +00:00
return cache.store(cache_signature, response_text)
if parsed_query.get('png_filename'):
options = {
'ip_addr': parsed_query['ip_addr'],
'lang': parsed_query['lang'],
'location': parsed_query['location']}
options.update(query)
2020-04-05 11:52:25 +00:00
output = fmt.png.make_wttr_in_png(
2020-04-05 09:51:36 +00:00
parsed_query['png_filename'], options=options)
else:
loc = (parsed_query['orig_location'] or "").lower()
if loc == 'moon' or loc.startswith('moon@'):
output = get_moon(query, parsed_query)
2020-04-05 09:51:36 +00:00
else:
output = get_wetter(query, parsed_query)
2020-04-05 11:52:25 +00:00
if query.get('days', '3') != '0' and not query.get('no-follow-line'):
if parsed_query['html_output']:
output = add_buttons(output)
else:
output += '\n' + get_message('FOLLOW_ME', parsed_query['lang']) + '\n'
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
return cache.store(cache_signature, output)
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
def parse_request(location, request, query, fast_mode=False):
"""Parse request and provided extended information for the query,
including location data, language, output format, view, etc.
Incoming data:
`location` location name extracted from the query url
`request.args`
`request.headers`
`request.remote_addr`
`request.referrer`
`request.query_string`
`query` parsed command line arguments
Return: dictionary with parsed parameters
"""
2018-10-05 14:54:14 +00:00
png_filename = None
if location is not None and location.lower().endswith(".png"):
png_filename = location
location = location[:-4]
2020-04-05 09:51:36 +00:00
if location and ':' in location and location[0] != ":":
location = _cyclic_location_selection(location, query.get('period', 1))
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
lang, _view = get_answer_language_and_view(request)
html_output = get_output_format(request, query)
2020-04-05 09:51:36 +00:00
ip_addr = _client_ip_address(request)
parsed_query = {
'ip_addr': ip_addr,
'user_agent': request.headers.get('User-Agent', '').lower(),
'lang': lang,
'view': _view,
'html_output': html_output,
'orig_location': location,
'location': location,
'request_url': request.url,
}
2020-04-05 11:52:25 +00:00
if png_filename:
parsed_query["png_filename"] = png_filename
2020-04-05 09:51:36 +00:00
if not png_filename and not fast_mode:
location, override_location_name, full_address, country, query_source_location = \
location_processing(location, ip_addr)
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
us_ip = query_source_location[1] == 'United States' \
and 'slack' not in parsed_query['user_agent']
query = parse_query.metric_or_imperial(query, lang, us_ip=us_ip)
2018-10-05 14:54:14 +00:00
if country and location != NOT_FOUND_LOCATION:
location = "%s,%s" % (location, country)
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
parsed_query.update({
'location': location,
'override_location_name': override_location_name,
'full_address': full_address,
'country': country,
'query_source_location': query_source_location})
return parsed_query
def wttr(location, request):
"""Main rendering function, it processes incoming weather queries,
and depending on the User-Agent string and other paramters of the query
it returns output in HTMLi, ANSI or other format.
"""
2020-04-05 11:52:25 +00:00
def _wrap_response(response_text, html_output, png_filename=None):
if not isinstance(response_text, str) and \
not isinstance(response_text, bytes):
2020-04-05 09:51:36 +00:00
return response_text
2020-04-05 11:52:25 +00:00
if png_filename:
response = make_response(send_file(
io.BytesIO(response_text),
attachment_filename=png_filename,
mimetype='image/png'))
for key, value in {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
}.items():
response.headers[key] = value
else:
response = make_response(response_text)
response.mimetype = 'text/html' if html_output else 'text/plain'
2020-04-05 09:51:36 +00:00
return response
if is_location_blocked(location):
return ""
2018-10-07 10:14:23 +00:00
try:
2020-04-05 09:51:36 +00:00
LIMITS.check_ip(_client_ip_address(request))
except RuntimeError as exception:
return str(exception)
query = parse_query.parse_query(request.args)
2018-10-05 14:54:14 +00:00
2020-04-05 09:51:36 +00:00
# first, we try to process the query as fast as possible
# (using the cache and static files),
# and only if "fast_mode" was unsuccessful,
# use the full track
parsed_query = parse_request(location, request, query, fast_mode=True)
response = _response(parsed_query, query, fast_mode=True)
try:
if not response:
parsed_query = parse_request(location, request, query)
response = _response(parsed_query, query)
# pylint: disable=broad-except
2019-12-28 17:08:06 +00:00
except Exception as exception:
2020-04-05 09:51:36 +00:00
logging.error("Exception has occured", exc_info=1)
if parsed_query['html_output']:
response = MALFORMED_RESPONSE_HTML_PAGE
else:
response = get_message('CAPACITY_LIMIT_REACHED', parsed_query['lang'])
2020-04-05 11:52:25 +00:00
# if exception is occured, we return not a png file but text
del parsed_query["png_filename"]
return _wrap_response(
response, parsed_query['html_output'],
png_filename=parsed_query.get('png_filename'))
if __name__ == "__main__":
import doctest
doctest.testmod()