9

[Admin note: Yeah, I'm pretty sure this doesn't work. The site doesn't handle pasting in code very well, in particular backslashes () are stripped out unless you put two instead. If anyone can figure out the code below and embed it in a geeklet file, let me know and I'll attach it to this post.]

I'm not sure if this will work well outside the US (though I'm sure someone can modify it to work, if needed), but I've been able to write a bash script to update my current conditions and Yahoo weather icon based on an IP Location lookup to get my current Zip code. Since I'm using the US Zip Code for the weather information, I also wrote it to build the weather.png URL from yahoo based on the condition code in the yahoo rss feed and by calculating the correct d/n extension for it (i.e. http://l.yimg.com/a/i/us/nws/weather/gr/.png). I'm still using Geektool 2, so I don't think I can export this as a geeklet, and I'm not great at scripting, so there's probably a better or more efficient way to do this:

#!/bin/bash
current=0
postal1=`curl -s http://ipinfodb.com/my_ip_location.php | awk '/Zip or postal code : /{print $6}';`
postal=`echo "$postal1" | tr -d 'r' | awk '{gsub(//,"")}; 1'`

curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | grep -e "Forecast:" -A2 -B2  |tail -n 4 | grep -v orecast | sed -e 's/
//' -e 's///' |  sed -n -E '2n;s/[ ]-[ ](.+)..+:[ ]([0-9]+).+:[ ]([0-9]+)/: 1, 2 | 3/;p'

imagecode=`curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | awk '/yweather:condition/' | sed -e 's/.*code="//' -e 's/".*//';`

sunsethour=`curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | awk '/yweather:astronomy/{print $4}' | sed -e 's/sunset="//' -e 's/:.*//' ;`
sunsetminute=`curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | awk '/yweather:astronomy/{print $4}' | sed -e 's/sunset="//' -e 's/.*://' ;`
:$((sunsethour=$sunsethour+12)); #convert to 24 hour
sunset=$(($sunsethour*60+$sunsetminute));
sunrisehour=`curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | awk '/yweather:astronomy/{print $2}' | sed -e 's/sunrise="//' -e 's/:.*//' ;` 
sunriseminute=`curl --silent "http://weather.yahooapis.com/forecastrss?p=$postal&u=f" | awk '/yweather:astronomy/{print $2}' | sed -e 's/sunrise="//' -e 's/.*://' ;`
sunrise=$(($sunrisehour*60+$sunriseminute));
currenthour=$(date +%k);
currentminute=$(date +%M);
current=$(($currenthour*60+$currentminute))

if (("$current" > "$sunrise"))
then
    if (("$current" < "$sunset"))
    then
        dt="d"
    else
        dt="n"
    fi
else
    dt="n"
fi
curl --silent "http://l.yimg.com/a/i/us/nws/weather/gr/$imagecode$dt.png" -o /tmp/weather.png

#EOF

Comments

User Avatar
imfloflo 143 days ago
I made some change no more IP adress cause not working in France but with new Yahoo website. Thanks;

#!/bin/bash

current=0

postal=589325;

# echo $postal;

#curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal" | grep -E 'l.yimg.com/a/i/us/we/52/.*gif' | head -1 | cut -f 2 -d '"' | cut -f 9 -d '/' | cut -f 1 -d'.'

imagecode=`curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal&u=c" | awk '/yweather:condition/' | sed -e 's/.*code="//' -e 's/".*//';`

sunsethour=`curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal&u=c" | awk '/yweather:astronomy/{print $4}' | sed -e 's/sunset="//' -e 's/:.*//' ;`

# echo sunsethour $sunsethour;

sunsetminute=`curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal&u=c" | awk '/yweather:astronomy/{print $4}' | sed -e 's/sunset="//' -e 's/.*://' ;`

# echo sunsetminute $sunsetminute;

sunsethour=$((sunsethour=$sunsethour+12));

# echo sunsethour24h $sunsethour;

sunset=$(($sunsethour*60+$sunsetminute));

# echo Sunset : $sunset;

sunrisehour=`curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal&u=c" | awk '/yweather:astronomy/{print $2}' | sed -e 's/sunrise="//' -e 's/:.*//' ;`

# echo sunrisehour $sunrisehour;

sunriseminute=`curl --silent "http://weather.yahooapis.com/forecastrss?w=$postal&u=c" | awk '/yweather:astronomy/{print $2}' | sed -e 's/sunrise="//' -e 's/.*://' ;`

# echo sunriseminute $sunriseminute;

sunrise=$(($sunrisehour*60+$sunriseminute));

# echo sunrise $sunrise;

currenthour=$(date +%k);

currentminute=$(date +%M);

current=$(($currenthour*60+$currentminute))

# echo current $current;

if (("$current" > "$sunrise"))

then

if (("$current" < "$sunset"))

then

dt="d"

else

dt="n"

fi

else

dt="n"

fi

curl --silent "http://l.yimg.com/a/i/us/nws/weather/gr/$imagecode$dt.png" -o /tmp/weather.png

#EOF

#export a=$(curl --silent "http://weather.yahooapis.com/forecastrss?w=589325" | grep -E 'l.yimg.com/a/i/us/we/52/.*gif' | head -1 | cut -f 2 -d '"' | cut -f 9 -d '/' | cut -f 1 -d'.')

#curl -s http://l.yimg.com/a/i/us/nws/weather/gr/${a}d.png -o /tmp/weather.png

Log in to comment or register here.