moved to location_normalize() to location.py

pull/223/head
Igor Chubin 7 years ago
parent 0eabfb1218
commit 0782e99d6f

@ -1,11 +1,23 @@
""" """
All location related functions and converters. All location related functions and converters.
The main entry point is `location_processing`
which gets `location` and `source_ip_address`
and basing on this information generates
precise location description.
""" """
import os
import json import json
import re
import socket
import requests import requests
import geoip2.database import geoip2.database
from globals import GEOLITE, GEOLOCATOR_SERVICE, IP2LCACHE, IP2LOCATION_KEY, NOT_FOUND_LOCATION, \
ALIASES, BLACKLIST, IATA_CODES_FILE
GEOIP_READER = geoip2.database.Reader(GEOLITE) GEOIP_READER = geoip2.database.Reader(GEOLITE)
def ascii_only(string): def ascii_only(string):
@ -18,6 +30,33 @@ def ascii_only(string):
except UnicodeDecodeError: except UnicodeDecodeError:
return False return False
def is_ip(ip_addr):
"""
Check if `ip_addr` looks like an IP Address
"""
if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', ip_addr) is None:
return False
try:
socket.inet_aton(ip_addr)
return True
except socket.error:
return False
def location_normalize(location):
"""
Normalize location name `location`
"""
#translation_table = dict.fromkeys(map(ord, '!@#$*;'), None)
def _remove_chars(chars, string):
return ''.join(x for x in string if x not in chars)
location = location.lower().replace('_', ' ').replace('+', ' ').strip()
if not location.startswith('moon@'):
location = _remove_chars(r'!@#$*;:\\', location)
return location
def geolocator(location): def geolocator(location):
""" """

@ -7,8 +7,6 @@ Main wttr.in rendering function implementation
import logging import logging
import os import os
import re
import socket
from flask import render_template, send_file, make_response from flask import render_template, send_file, make_response
import wttrin_png import wttrin_png
@ -31,19 +29,6 @@ logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s
LIMITS = Limits(whitelist=[MY_EXTERNAL_IP], limits=(30, 60, 100)) LIMITS = Limits(whitelist=[MY_EXTERNAL_IP], limits=(30, 60, 100))
def is_ip(ip_addr):
"""
Check if `ip_addr` looks like an IP Address
"""
if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', ip_addr) is None:
return False
try:
socket.inet_aton(ip_addr)
return True
except socket.error:
return False
def show_text_file(name, lang): def show_text_file(name, lang):
""" """
show static file `name` for `lang` show static file `name` for `lang`
@ -62,19 +47,6 @@ def show_text_file(name, lang):
.replace('SUPPORTED_LANGUAGES', ' '.join(SUPPORTED_LANGS)) .replace('SUPPORTED_LANGUAGES', ' '.join(SUPPORTED_LANGS))
return text.decode('utf-8') return text.decode('utf-8')
def location_normalize(location):
"""
Normalize location name `location`
"""
#translation_table = dict.fromkeys(map(ord, '!@#$*;'), None)
def _remove_chars(chars, string):
return ''.join(x for x in string if x not in chars)
location = location.lower().replace('_', ' ').replace('+', ' ').strip()
if not location.startswith('moon@'):
location = _remove_chars(r'!@#$*;:\\', location)
return location
def client_ip_address(request): def client_ip_address(request):
""" """
Return client ip address for `request`. Return client ip address for `request`.

Loading…
Cancel
Save