|
|
@ -36,7 +36,8 @@ from globals import GEOLITE, \
|
|
|
|
NOT_FOUND_LOCATION, \
|
|
|
|
NOT_FOUND_LOCATION, \
|
|
|
|
MALFORMED_RESPONSE_HTML_PAGE, \
|
|
|
|
MALFORMED_RESPONSE_HTML_PAGE, \
|
|
|
|
IATA_CODES_FILE, \
|
|
|
|
IATA_CODES_FILE, \
|
|
|
|
log, error
|
|
|
|
log, error, \
|
|
|
|
|
|
|
|
LISTEN_PORT, LISTEN_HOST
|
|
|
|
|
|
|
|
|
|
|
|
from wttr import get_wetter, get_moon
|
|
|
|
from wttr import get_wetter, get_moon
|
|
|
|
|
|
|
|
|
|
|
@ -197,73 +198,22 @@ reader = geoip2.database.Reader(GEOLITE)
|
|
|
|
def get_location(ip_addr):
|
|
|
|
def get_location(ip_addr):
|
|
|
|
response = reader.city(ip_addr)
|
|
|
|
response = reader.city(ip_addr)
|
|
|
|
|
|
|
|
|
|
|
|
if location == NOT_FOUND_LOCATION:
|
|
|
|
country = response.country.iso_code
|
|
|
|
location_not_found = True
|
|
|
|
|
|
|
|
location = DEFAULT_LOCATION
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
location_not_found = False
|
|
|
|
|
|
|
|
p = Popen( [ WEGO, '-location=%s' % location ], stdout=PIPE, stderr=PIPE )
|
|
|
|
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
|
|
|
|
if p.returncode != 0:
|
|
|
|
|
|
|
|
error( stdout + stderr )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dirname = os.path.dirname( filename )
|
|
|
|
|
|
|
|
if not os.path.exists( dirname ):
|
|
|
|
|
|
|
|
os.makedirs( dirname )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if location_not_found:
|
|
|
|
|
|
|
|
stdout += NOT_FOUND_MESSAGE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open( filename, 'w' ).write( stdout )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p = Popen( [ "bash", ANSI2HTML, "--palette=solarized", "--bg=dark" ], stdin=PIPE, stdout=PIPE, stderr=PIPE )
|
|
|
|
|
|
|
|
stdout, stderr = p.communicate( stdout )
|
|
|
|
|
|
|
|
if p.returncode != 0:
|
|
|
|
|
|
|
|
error( stdout + stderr )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open( filename+'.html', 'w' ).write( stdout )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_filename( location ):
|
|
|
|
|
|
|
|
location = location.replace('/', '_')
|
|
|
|
|
|
|
|
timestamp = time.strftime( "%Y%m%d%H", time.localtime() )
|
|
|
|
|
|
|
|
return "%s/%s/%s" % ( CACHEDIR, location, timestamp )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_wetter(location, ip, html=False):
|
|
|
|
|
|
|
|
filename = get_filename( location )
|
|
|
|
|
|
|
|
if not os.path.exists( filename ):
|
|
|
|
|
|
|
|
limits.check_ip( ip )
|
|
|
|
|
|
|
|
save_weather_data( location, filename )
|
|
|
|
|
|
|
|
if html:
|
|
|
|
|
|
|
|
filename += '.html'
|
|
|
|
|
|
|
|
return open(filename).read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_location( ip_addr ):
|
|
|
|
|
|
|
|
response = reader.city( ip_addr )
|
|
|
|
|
|
|
|
city = response.city.name
|
|
|
|
city = response.city.name
|
|
|
|
if city is None and response.location:
|
|
|
|
|
|
|
|
coord = "%s, %s" % (response.location.latitude, response.location.longitude)
|
|
|
|
|
|
|
|
location = geolocator.reverse(coord, language='en')
|
|
|
|
|
|
|
|
city = location.raw.get('address', {}).get('city')
|
|
|
|
|
|
|
|
if city is None:
|
|
|
|
|
|
|
|
print ip_addr
|
|
|
|
|
|
|
|
city = ip2location( ip_addr )
|
|
|
|
|
|
|
|
return city or NOT_FOUND_LOCATION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_aliases( aliases_filename ):
|
|
|
|
#
|
|
|
|
aliases_db = {}
|
|
|
|
# temporary disabled it because of geoip services capcacity
|
|
|
|
with open( aliases_filename, 'r' ) as f:
|
|
|
|
#
|
|
|
|
for line in f.readlines():
|
|
|
|
#if city is None and response.location:
|
|
|
|
from_, to_ = line.split(':', 1)
|
|
|
|
# coord = "%s, %s" % (response.location.latitude, response.location.longitude)
|
|
|
|
aliases_db[ from_.strip().lower() ] = to_.strip()
|
|
|
|
# try:
|
|
|
|
return aliases_db
|
|
|
|
# location = geolocator.reverse(coord, language='en')
|
|
|
|
|
|
|
|
# city = location.raw.get('address', {}).get('city')
|
|
|
|
location_alias = load_aliases( ALIASES )
|
|
|
|
# except:
|
|
|
|
def location_canonical_name( location ):
|
|
|
|
# pass
|
|
|
|
if location.lower() in location_alias:
|
|
|
|
if city is None:
|
|
|
|
return location_alias[location.lower()]
|
|
|
|
city = ip2location(ip_addr)
|
|
|
|
return location
|
|
|
|
return (city or NOT_FOUND_LOCATION), country
|
|
|
|
|
|
|
|
|
|
|
|
def parse_accept_language(acceptLanguage):
|
|
|
|
def parse_accept_language(acceptLanguage):
|
|
|
|
languages = acceptLanguage.split(",")
|
|
|
|
languages = acceptLanguage.split(",")
|
|
|
|