how to configure ip2location (#296)

This commit is contained in:
Igor Chubin 2019-05-09 17:40:22 +02:00
parent 6b11f2893a
commit 8142e30e53
6 changed files with 35 additions and 14 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ share/static/fonts/
data/
log/
.idea/
*.swp

View file

@ -234,8 +234,8 @@ To install the application:
1. Install external dependencies
2. Install Python dependencies used by the service
3. Get a WorldWeatherOnline API Key
4. Configure wego
3. Configure IP2Location (optional)
4. Get a WorldWeatherOnline API and configure wego
5. Configure wttr.in
6. Configure the HTTP-frontend service
@ -278,14 +278,24 @@ If `virtualenv` is used:
Also, you need to install the geoip2 database.
You can use a free database GeoLite2 that can be downloaded from (http://dev.maxmind.com/geoip/geoip2/geolite2/).
### Get a WorldWeatherOnline key
### Configure IP2Location (optional)
If you want to use the IP2location service for IP-addresses that are not covered by GeoLite2,
you have to obtain a API key of that service, and after that save into the `~/.ip2location.key` file:
```
$ echo 'YOUR_IP2LOCATION_KEY' > ~/.ip2location.key
```
If you don't have this file, the service will be silently skipped (it is not a big problem,
because the MaxMind database is pretty good).
### Get a WorldWeatherOnline key and configure wego
To get a WorldWeatherOnline API key, you must register here:
https://developer.worldweatheronline.com/auth/register
### Configure wego
After you have a WorldWeatherOnline key, you can configure `wego`:
$ cat ~/.wegorc

View file

@ -67,6 +67,7 @@ PLAIN_TEXT_AGENTS = [
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
IP2LOCATION_KEY = None
if os.path.exists(_IP2LOCATION_KEY_FILE):
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()

View file

@ -95,15 +95,18 @@ def ip2location(ip_addr):
if os.path.exists(cached):
location = open(cached, 'r').read()
else:
try:
ip2location_response = requests\
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
% (ip_addr, IP2LOCATION_KEY)).text
if ';' in ip2location_response:
open(cached, 'w').write(ip2location_response)
location = ip2location_response
except requests.exceptions.ConnectionError:
pass
# if IP2LOCATION_KEY is not set, do not the query,
# because the query wont be processed anyway
if IP2LOCATION_KEY:
try:
ip2location_response = requests\
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
% (ip_addr, IP2LOCATION_KEY)).text
if ';' in ip2location_response:
open(cached, 'w').write(ip2location_response)
location = ip2location_response
except requests.exceptions.ConnectionError:
pass
if ';' in location:
location = location.split(';')[3], location.split(';')[1]

View file

@ -46,3 +46,4 @@ Chicago : Chicago,IL
Paris : Paris,France
Giessen : Giessen, Germany
Braga : Braga, Portugal
Kashan : ~Kashan,Iran

View file

@ -8,3 +8,8 @@ do
mkdir "${CACHEDIR}/${dir}"
rm -rf "${CACHEDIR}/${dir}.old"
done
cd /wttr.in/log
mv main.log main.log.1
touch main.log