2018-10-05 20:37:16 +02:00
|
|
|
"""
|
|
|
|
global configuration of the project
|
2019-05-10 17:46:44 +02:00
|
|
|
|
|
|
|
External environment variables:
|
|
|
|
|
|
|
|
WTTR_MYDIR
|
|
|
|
WTTR_GEOLITE
|
|
|
|
WTTR_WEGO
|
|
|
|
WTTR_LISTEN_HOST
|
|
|
|
WTTR_LISTEN_PORT
|
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
"""
|
2018-12-02 09:41:29 +01:00
|
|
|
from __future__ import print_function
|
2018-10-05 20:37:16 +02:00
|
|
|
|
2017-03-31 20:38:35 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2020-04-05 11:50:58 +02:00
|
|
|
import re
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__')))
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2019-05-10 17:46:44 +02:00
|
|
|
if "WTTR_GEOLITE" in os.environ:
|
|
|
|
GEOLITE = os.environ["WTTR_GEOLITE"]
|
|
|
|
else:
|
|
|
|
GEOLITE = os.path.join(MYDIR, 'data', "GeoLite2-City.mmdb")
|
|
|
|
|
|
|
|
WEGO = os.environ.get("WTTR_WEGO", "/home/igor/go/bin/we-lang")
|
2018-10-23 18:53:34 +02:00
|
|
|
PYPHOON = "/home/igor/pyphoon/bin/pyphoon-lolcat"
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2018-10-27 00:17:16 +02:00
|
|
|
_DATADIR = "/wttr.in"
|
|
|
|
_LOGDIR = "/wttr.in/log"
|
|
|
|
|
|
|
|
IP2LCACHE = os.path.join(_DATADIR, "cache/ip2l/")
|
|
|
|
PNG_CACHE = os.path.join(_DATADIR, "cache/png")
|
2020-04-05 11:50:58 +02:00
|
|
|
LRU_CACHE = os.path.join(_DATADIR, "cache/lru")
|
2018-10-27 00:17:16 +02:00
|
|
|
|
|
|
|
LOG_FILE = os.path.join(_LOGDIR, 'main.log')
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
ALIASES = os.path.join(MYDIR, "share/aliases")
|
|
|
|
ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh")
|
|
|
|
BLACKLIST = os.path.join(MYDIR, "share/blacklist")
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
HELP_FILE = os.path.join(MYDIR, 'share/help.txt')
|
|
|
|
BASH_FUNCTION_FILE = os.path.join(MYDIR, 'share/bash-function.txt')
|
|
|
|
TRANSLATION_FILE = os.path.join(MYDIR, 'share/translation.txt')
|
2018-10-03 18:59:17 +02:00
|
|
|
|
|
|
|
IATA_CODES_FILE = os.path.join(MYDIR, 'share/list-of-iata-codes.txt')
|
2017-03-31 20:38:35 +02:00
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
TEMPLATES = os.path.join(MYDIR, 'share/templates')
|
|
|
|
STATIC = os.path.join(MYDIR, 'share/static')
|
2017-03-31 20:38:35 +02:00
|
|
|
|
|
|
|
NOT_FOUND_LOCATION = "not found"
|
|
|
|
DEFAULT_LOCATION = "oymyakon"
|
|
|
|
|
|
|
|
MALFORMED_RESPONSE_HTML_PAGE = open(os.path.join(STATIC, 'malformed-response.html')).read()
|
|
|
|
|
2018-10-07 12:11:59 +02:00
|
|
|
GEOLOCATOR_SERVICE = 'http://localhost:8004'
|
|
|
|
|
2018-11-01 18:23:32 +01:00
|
|
|
# number of queries from the same IP address is limited
|
|
|
|
# (minute, hour, day) limitations:
|
|
|
|
QUERY_LIMITS = (300, 3600, 24*3600)
|
|
|
|
|
2019-05-10 17:46:44 +02:00
|
|
|
LISTEN_HOST = os.environ.get("WTTR_LISTEN_HOST", "")
|
|
|
|
try:
|
|
|
|
LISTEN_PORT = int(os.environ.get("WTTR_LISTEN_PORT"))
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
LISTEN_PORT = 8002
|
2018-10-03 22:46:23 +02:00
|
|
|
|
2018-10-23 20:04:42 +02:00
|
|
|
PROXY_HOST = "127.0.0.1"
|
|
|
|
PROXY_PORT = 5001
|
2018-10-27 00:17:16 +02:00
|
|
|
PROXY_CACHEDIR = os.path.join(_DATADIR, "cache/proxy-wwo/")
|
2018-10-23 20:04:42 +02:00
|
|
|
|
2018-10-05 20:37:16 +02:00
|
|
|
MY_EXTERNAL_IP = '5.9.243.187'
|
|
|
|
|
2018-10-05 20:19:27 +02:00
|
|
|
PLAIN_TEXT_AGENTS = [
|
|
|
|
"curl",
|
|
|
|
"httpie",
|
|
|
|
"lwp-request",
|
|
|
|
"wget",
|
2018-11-06 14:05:12 +01:00
|
|
|
"python-requests",
|
2020-04-27 19:03:27 +02:00
|
|
|
"openbsd ftp",
|
|
|
|
"powershell",
|
2018-10-05 20:19:27 +02:00
|
|
|
]
|
|
|
|
|
2020-04-03 18:02:16 +02:00
|
|
|
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation', ':iterm2']
|
2018-10-05 20:19:27 +02:00
|
|
|
|
2018-10-26 19:35:13 +02:00
|
|
|
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
|
2019-05-09 17:40:22 +02:00
|
|
|
IP2LOCATION_KEY = None
|
2018-10-26 19:35:13 +02:00
|
|
|
if os.path.exists(_IP2LOCATION_KEY_FILE):
|
|
|
|
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
|
2018-10-23 20:04:42 +02:00
|
|
|
|
2018-10-27 23:54:37 +02:00
|
|
|
_WWO_KEY_FILE = os.environ['HOME'] + '/.wwo.key'
|
2019-05-10 17:46:44 +02:00
|
|
|
WWO_KEY = "key-is-not-specified"
|
2018-10-27 23:54:37 +02:00
|
|
|
if os.path.exists(_WWO_KEY_FILE):
|
|
|
|
WWO_KEY = open(_WWO_KEY_FILE, 'r').read().strip()
|
|
|
|
|
2017-03-31 20:38:35 +02:00
|
|
|
def error(text):
|
2018-10-05 20:37:16 +02:00
|
|
|
"log error `text` and raise a RuntimeError exception"
|
|
|
|
|
2017-03-31 20:38:35 +02:00
|
|
|
if not text.startswith('Too many queries'):
|
2018-12-02 09:41:29 +01:00
|
|
|
print(text)
|
2018-10-05 20:37:16 +02:00
|
|
|
logging.error("ERROR %s", text)
|
2017-03-31 20:38:35 +02:00
|
|
|
raise RuntimeError(text)
|
|
|
|
|
|
|
|
def log(text):
|
2018-10-05 20:37:16 +02:00
|
|
|
"log error `text` and do not raise any exceptions"
|
|
|
|
|
2017-03-31 20:38:35 +02:00
|
|
|
if not text.startswith('Too many queries'):
|
2018-12-02 09:41:29 +01:00
|
|
|
print(text)
|
2017-03-31 20:38:35 +02:00
|
|
|
logging.info(text)
|
|
|
|
|
2018-11-02 18:11:49 +01:00
|
|
|
def debug_log(text):
|
|
|
|
"""
|
|
|
|
Write `text` to the debug log
|
|
|
|
"""
|
|
|
|
|
|
|
|
with open('/tmp/wttr.in-debug.log', 'a') as f_debug:
|
|
|
|
f_debug.write(text+'\n')
|
|
|
|
|
2018-10-03 18:59:17 +02:00
|
|
|
def get_help_file(lang):
|
2018-10-05 20:37:16 +02:00
|
|
|
"Return help file for `lang`"
|
|
|
|
|
2018-10-03 18:59:17 +02:00
|
|
|
help_file = os.path.join(MYDIR, 'share/translations/%s-help.txt' % lang)
|
|
|
|
if os.path.exists(help_file):
|
|
|
|
return help_file
|
2018-10-05 20:37:16 +02:00
|
|
|
return HELP_FILE
|
2020-04-04 18:04:27 +02:00
|
|
|
|
|
|
|
def remove_ansi(sometext):
|
|
|
|
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
|
|
|
|
return ansi_escape.sub('', sometext)
|