commit b0122f8f538f475c2ade9ef553abcde81e6f6fcc
parent ccfb4ae95afd0d5aa11e57ecabbb015ec250736a
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date: Sat, 17 Oct 2015 23:10:14 +0200
Merge pull request #84 from sshaw/apikey
Add support for OpenWeather API key
Diffstat:
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
@@ -8,7 +8,6 @@ terminal, with support for ANSI colors and Unicode symbols.
Weather data comes from the `OpenWeatherMap` free weather API.
-
## Requirements
AnsiWeather requires the following dependencies :
@@ -51,6 +50,7 @@ AnsiWeather packages are available for :
Options are :
+ -k API key
-l Specify location
-u Specify unit system to use (metric or imperial)
-f Toggle forecast mode for the specified number of upcoming days
@@ -66,12 +66,15 @@ Options are :
Any configuration options may also be passed in as command line options :
- ./ansiweather -l Moscow,RU -u metric -s true -f 5 -d true
+ ./ansiweather -k APIKEY -l Moscow,RU -u metric -s true -f 5 -d true
## Configuration
+Accessing OpenWeatherMap requires an API key. To get a key you must [create an OpenWeatherMap account](http://home.openweathermap.org/users/sign_up).
+It's free.
+
The default config file is ~/.ansiweatherrc. The environment variable
ANSIWEATHERRC can be set to override this. The following configuration options
(detailed below) are available and should be set according to your location and
@@ -79,6 +82,7 @@ preferences.
Example : `~/.ansiweatherrc`
+ api_key: X1234567890
location:Moscow,RU
fetch_cmd:ftp -V -o -
geo_api_url:www.telize.com/geoip
diff --git a/ansiweather b/ansiweather
@@ -43,10 +43,11 @@ fetch_cmd=$(get_config "fetch_cmd" || echo "curl -s")
###[ Parse the command line ]##################################################
# Get config options from command line flags
-while getopts l:u:f:Fd:a:s:h option
+while getopts k:l:u:f:Fd:a:s:h option
do
case "${option}"
in
+ k) api_key=${OPTARG};;
l) location=${OPTARG};;
u) units=${OPTARG};;
f) forecast=${OPTARG};;
@@ -72,6 +73,7 @@ then
"" \
"Options are :" \
"" \
+ " -k API key" \
" -l Specify location" \
" -u Specify unit system to use (metric or imperial)" \
" -f Toggle forecast mode for the specified number of upcoming days" \
@@ -136,6 +138,14 @@ auto_locate() {
###[ Set options that are not set from command line ]##########################
# Location : example "Moscow,RU"
+[ -z "$api_key" ] && api_key=$(get_config "api_key")
+if [ -z "$api_key" ]
+then
+ echo "ERROR : API key required"
+ exit
+fi
+
+# Location : example "Moscow,RU"
[ -z "$location" ] && location=$(get_config "location" || auto_locate || echo "Moscow,RU")
# System of Units : "metric" or "imperial"
@@ -200,11 +210,11 @@ api_cmd=$([ "$forecast" != 0 ] && echo "forecast/daily" || echo "weather")
if [ "$location" -gt 0 ] 2> /dev/null
then
# Location is all numeric
- weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/$api_cmd?id=$location&units=$units")
+ weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/$api_cmd?id=$location&units=$units&APPID=$api_key")
else
# Location is a string
location=$(echo "$location" | sed "s/ /_/g")
- weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/$api_cmd?q=$location&units=$units")
+ weather=$($fetch_cmd "http://api.openweathermap.org/data/2.5/$api_cmd?q=$location&units=$units&APPID=$api_key")
fi
if [ -z "$weather" ]