#!/bin/bash
#
# script météo
# sources: Nanoseb
# http://crunchbang-fr.org/forum/viewtopic.php?pid=35678#p35678
###############################################################
fichier="$HOME/.temps"
ville=$(echo $* | sed 's/\ /\%20/g')
apikey="c032679388a8e1cdf1a05b90ca396"

wget -q -O "$fichier" "http://api.openweathermap.org/data/2.5/weather?q="$ville"&mode=xml&units=metric"

temperature=$(xmllint --xpath "//current/temperature/@value" "$fichier" | awk -F"\"" '{print $2}')
humidity=$(xmllint --xpath "//current/humidity/@value" "$fichier" | awk -F"\"" '{print $2}')
pressure=$(xmllint --xpath "//current/pressure/@value" "$fichier" | awk -F"\"" '{print $2}')
weather=$(xmllint --xpath "//current/weather/@value" "$fichier" | awk -F"\"" '{print $2}')
weathericon=$(xmllint --xpath "//current/weather/@icon" "$fichier" | awk -F"\"" '{print $2}')
lon=$(xmllint --xpath "//current/city/coord/@lon" "$fichier" | awk -F"\"" '{print $2}')
lat=$(xmllint --xpath "//current/city/coord/@lat" "$fichier" | awk -F"\"" '{print $2}')
city=$(xmllint --xpath "//current/city/@name" "$fichier" | awk -F"\"" '{print $2}')


wget -q -O "$fichier" "http://api.worldweatheronline.com/free/v2/tz.ashx?key=$apikey&q=$lat,$lon&format=xml"
localtime=$(xmllint --xpath "//data/time_zone/localtime" "$fichier" | sed 's/^<.*>\([^<].*\)<.*>$/\1/;s/-/\ /g' | awk '{print $4 " " $3 "/" $2 "/" $1}')

wget -q -O "$fichier" "http://openweathermap.org/img/w/${weathericon}.png"

echo "$city        $localtime"
echo
img2txt -f utf8 -W 50 -x 7 -y 14 "$fichier" | sed '/^\ *$/d'
echo
echo "Température : $temperature°C        Humidité : $humidity%"
echo "Pression : $pressure hPa          Temps : $weather"

rm "$fichier"