mirror of
https://github.com/chubin/wttr.in
synced 2025-01-11 11:38:45 +00:00
how to configure ip2location (#296)
This commit is contained in:
parent
6b11f2893a
commit
8142e30e53
6 changed files with 35 additions and 14 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ share/static/fonts/
|
||||||
data/
|
data/
|
||||||
log/
|
log/
|
||||||
.idea/
|
.idea/
|
||||||
|
*.swp
|
||||||
|
|
20
README.md
20
README.md
|
@ -234,8 +234,8 @@ To install the application:
|
||||||
|
|
||||||
1. Install external dependencies
|
1. Install external dependencies
|
||||||
2. Install Python dependencies used by the service
|
2. Install Python dependencies used by the service
|
||||||
3. Get a WorldWeatherOnline API Key
|
3. Configure IP2Location (optional)
|
||||||
4. Configure wego
|
4. Get a WorldWeatherOnline API and configure wego
|
||||||
5. Configure wttr.in
|
5. Configure wttr.in
|
||||||
6. Configure the HTTP-frontend service
|
6. Configure the HTTP-frontend service
|
||||||
|
|
||||||
|
@ -278,14 +278,24 @@ If `virtualenv` is used:
|
||||||
Also, you need to install the geoip2 database.
|
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/).
|
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:
|
To get a WorldWeatherOnline API key, you must register here:
|
||||||
|
|
||||||
https://developer.worldweatheronline.com/auth/register
|
https://developer.worldweatheronline.com/auth/register
|
||||||
|
|
||||||
### Configure wego
|
|
||||||
|
|
||||||
After you have a WorldWeatherOnline key, you can configure `wego`:
|
After you have a WorldWeatherOnline key, you can configure `wego`:
|
||||||
|
|
||||||
$ cat ~/.wegorc
|
$ cat ~/.wegorc
|
||||||
|
|
|
@ -67,6 +67,7 @@ PLAIN_TEXT_AGENTS = [
|
||||||
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
|
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
|
||||||
|
|
||||||
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
|
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
|
||||||
|
IP2LOCATION_KEY = None
|
||||||
if os.path.exists(_IP2LOCATION_KEY_FILE):
|
if os.path.exists(_IP2LOCATION_KEY_FILE):
|
||||||
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
|
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
|
||||||
|
|
||||||
|
|
|
@ -95,15 +95,18 @@ def ip2location(ip_addr):
|
||||||
if os.path.exists(cached):
|
if os.path.exists(cached):
|
||||||
location = open(cached, 'r').read()
|
location = open(cached, 'r').read()
|
||||||
else:
|
else:
|
||||||
try:
|
# if IP2LOCATION_KEY is not set, do not the query,
|
||||||
ip2location_response = requests\
|
# because the query wont be processed anyway
|
||||||
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
|
if IP2LOCATION_KEY:
|
||||||
% (ip_addr, IP2LOCATION_KEY)).text
|
try:
|
||||||
if ';' in ip2location_response:
|
ip2location_response = requests\
|
||||||
open(cached, 'w').write(ip2location_response)
|
.get('http://api.ip2location.com/?ip=%s&key=%s&package=WS3' \
|
||||||
location = ip2location_response
|
% (ip_addr, IP2LOCATION_KEY)).text
|
||||||
except requests.exceptions.ConnectionError:
|
if ';' in ip2location_response:
|
||||||
pass
|
open(cached, 'w').write(ip2location_response)
|
||||||
|
location = ip2location_response
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
pass
|
||||||
|
|
||||||
if ';' in location:
|
if ';' in location:
|
||||||
location = location.split(';')[3], location.split(';')[1]
|
location = location.split(';')[3], location.split(';')[1]
|
||||||
|
|
|
@ -46,3 +46,4 @@ Chicago : Chicago,IL
|
||||||
Paris : Paris,France
|
Paris : Paris,France
|
||||||
Giessen : Giessen, Germany
|
Giessen : Giessen, Germany
|
||||||
Braga : Braga, Portugal
|
Braga : Braga, Portugal
|
||||||
|
Kashan : ~Kashan,Iran
|
||||||
|
|
|
@ -8,3 +8,8 @@ do
|
||||||
mkdir "${CACHEDIR}/${dir}"
|
mkdir "${CACHEDIR}/${dir}"
|
||||||
rm -rf "${CACHEDIR}/${dir}.old"
|
rm -rf "${CACHEDIR}/${dir}.old"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
cd /wttr.in/log
|
||||||
|
mv main.log main.log.1
|
||||||
|
touch main.log
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue