wttr.in/lib/wttr_srv.py

269 lines
9 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
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
2018-10-05 18:19:49 +00:00
import wttrin_png
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
from globals import get_help_file, log, \
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
2018-10-05 14:54:14 +00:00
from wttr import get_wetter, get_moon
from wttr_line import wttr_line
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.DEBUG, format='%(asctime)s %(message)s')
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))
return text.decode('utf-8')
def client_ip_address(request):
2018-10-05 18:19:49 +00:00
"""
Return client ip address for `request`.
Flask related
2018-10-05 18:19:49 +00:00
"""
2018-10-05 14:54:14 +00:00
if 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 get_answer_language(request):
2018-10-05 18:19:49 +00:00
"""
Return preferred answer language based on
domain name, query arguments and headers
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
locale_q_pairs.append((language.strip(), "1"))
else:
locale = language.split(";")[0].strip()
weight = language.split(";")[1].split("=")[1]
locale_q_pairs.append((locale, weight))
except IndexError:
pass
return locale_q_pairs
def _find_supported_language(accepted_languages):
for lang_tuple in accepted_languages:
lang = lang_tuple[0]
if '-' in lang:
lang = lang.split('-', 1)[0]
if lang in SUPPORTED_LANGS:
return lang
return None
2018-10-05 14:54:14 +00:00
lang = None
hostname = request.headers['Host']
if hostname != 'wttr.in' and hostname.endswith('.wttr.in'):
lang = hostname[:-8]
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 = _find_supported_language(
_parse_accept_language(header_accept_language))
2018-10-05 14:54:14 +00:00
return lang
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
# FIXME
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
2018-11-02 17:14:48 +00:00
def cyclic_location_selection(locations, period):
"""
Return one of `locations` (: separated list)
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
index = int(time.time())/period % len(locations)
return locations[index]
2018-10-05 14:54:14 +00:00
def wttr(location, request):
2018-10-05 14:54:14 +00:00
"""
Main rendering function, it processes incoming weather queries.
Depending on user agent it returns output in HTML or ANSI format.
Incoming data:
request.args
request.headers
request.remote_addr
request.referrer
request.query_string
"""
2019-02-04 06:28:44 +00:00
def _wrap_response(response_text, html_output):
response = make_response(response_text)
response.mimetype = 'text/html' if html_output else 'text/plain'
return response
2018-10-07 10:14:23 +00:00
if is_location_blocked(location):
return ""
2018-10-05 14:54:14 +00:00
2018-10-07 10:14:23 +00:00
ip_addr = client_ip_address(request)
2018-10-05 14:54:14 +00:00
try:
2018-10-05 18:19:49 +00:00
LIMITS.check_ip(ip_addr)
except RuntimeError as exception:
2018-10-07 10:14:23 +00:00
return str(exception)
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]
2018-10-07 10:14:23 +00:00
lang = get_answer_language(request)
2018-10-05 14:54:14 +00:00
query = parse_query.parse_query(request.args)
html_output = get_output_format(request, query)
2018-10-05 14:54:14 +00:00
user_agent = request.headers.get('User-Agent', '').lower()
2018-10-07 10:14:23 +00:00
2018-10-05 18:19:49 +00:00
if location in PLAIN_TEXT_PAGES:
2018-10-07 10:14:23 +00:00
help_ = show_text_file(location, lang)
2018-10-05 14:54:14 +00:00
if html_output:
2019-02-04 06:28:44 +00:00
return _wrap_response(render_template('index.html', body=help_), html_output)
return _wrap_response(help_, html_output)
2018-10-05 14:54:14 +00:00
2018-11-02 17:14:48 +00:00
if location and ':' in location:
location = cyclic_location_selection(location, query.get('period', 1))
2018-10-05 14:54:14 +00:00
orig_location = location
2018-10-07 10:14:23 +00:00
location, override_location_name, full_address, country, query_source_location = \
2018-10-20 13:41:53 +00:00
location_processing(location, ip_addr)
2018-10-05 14:54:14 +00:00
us_ip = query_source_location[1] == 'United States' and 'slack' not in user_agent
2018-10-07 10:14:23 +00:00
query = parse_query.metric_or_imperial(query, lang, us_ip=us_ip)
2018-10-05 14:54:14 +00:00
2018-10-26 21:03:50 +00:00
# logging query
orig_location_utf8 = (orig_location or "").encode('utf-8')
location_utf8 = location.encode('utf-8')
use_imperial = query.get('use_imperial', False)
log(" ".join(map(str,
[ip_addr, user_agent, orig_location_utf8, location_utf8, use_imperial, lang])))
2018-10-05 14:54:14 +00:00
2018-10-07 10:14:23 +00:00
if country and location != NOT_FOUND_LOCATION:
location = "%s,%s" % (location, country)
2018-10-05 14:54:14 +00:00
2018-10-07 10:14:23 +00:00
# We are ready to return the answer
try:
if 'format' in query:
2019-02-04 06:28:44 +00:00
return _wrap_response(wttr_line(location, override_location_name, query), html_output)
2018-10-07 10:14:23 +00:00
if png_filename:
options = {
'lang': None,
'location': location}
2018-10-05 14:54:14 +00:00
options.update(query)
cached_png_file = wttrin_png.make_wttr_in_png(png_filename, options=options)
response = make_response(send_file(cached_png_file,
attachment_filename=png_filename,
mimetype='image/png'))
2018-10-26 20:34:20 +00:00
for key, value in {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
}.items():
response.headers[key] = value
2018-10-05 14:54:14 +00:00
# Trying to disable github caching
return response
if location == 'moon' or location.startswith('moon@'):
2019-02-01 11:14:01 +00:00
output = get_moon(location, html=html_output, lang=lang, query=query)
2018-10-05 14:54:14 +00:00
else:
2018-10-05 18:19:49 +00:00
output = get_wetter(location, ip_addr,
2018-10-05 14:54:14 +00:00
html=html_output,
lang=lang,
query=query,
location_name=override_location_name,
full_address=full_address,
url=request.url,
)
if query.get('days', '3') != '0' and not query.get('no-follow-line'):
if html_output:
output = add_buttons(output)
else:
2018-10-05 14:54:14 +00:00
#output += '\n' + get_message('NEW_FEATURE', lang).encode('utf-8')
output += '\n' + get_message('FOLLOW_ME', lang).encode('utf-8') + '\n'
2019-02-04 06:28:44 +00:00
return _wrap_response(output, html_output)
2018-10-05 14:54:14 +00:00
except RuntimeError as exception:
2018-10-07 10:14:23 +00:00
if 'Malformed response' in str(exception) \
or 'API key has reached calls per day allowed limit' in str(exception):
2018-10-05 14:54:14 +00:00
if html_output:
2019-02-04 06:28:44 +00:00
return _wrap_response(MALFORMED_RESPONSE_HTML_PAGE, html_output)
return _wrap_response(get_message('CAPACITY_LIMIT_REACHED', lang).encode('utf-6'), html_output)
2018-10-05 14:54:14 +00:00
logging.error("Exception has occured", exc_info=1)
return "ERROR"