You are not logged in.
Pages: 1
Topic closed
I found the idea of using an internet weather service to display weather information in a terminal here on the forum. The general idea is to use the "-dump" option of a terminal browser like elinks or lynx and parse its output.
My current version uses "google.com/search?hl=en&q=weather+city" and looks like this:
weather(){
elinks -dump "google.com/search?hl=en&q=weather+${1:-munich}" | \
sed -n '/Weather for/,${;s/^ *//;/iGoogle/d;s/-.*//;s/|.*//;p;/Humidity/q;}' | \
sed '2,$s/^/ /'
}and the output:
Weather for Munich, Germany
13°C
Current: Overcast
Wind: NE at 13 km/h
Humidity: 88%Question:
Do you now other websites which provide easy to parse weather information suitable for this?
(You may as well comment on other ways to improve this - e.g. i think the sed command is not very neat)
thank you for you help and ideas
luc
EDIT: new version (2011-02-21)
Last edited by luc (2011-02-21 12:13:30)
Offline
I was working on some weather conky today and took some info from accuweather.com... it gives you the weather forecast (in terminal)....
#! /bin/bash
curl http://www.accuweather.com/de/de/bayern/munich/forecast.aspx | grep -n 'Forecast' > /tmp/accuweather.txt
cat /tmp/accuweather.txt | grep -n 'ForecastDay' > /tmp/accuweatherday.txt
cat /tmp/accuweather.txt | grep -n 'ForecastNight' > /tmp/accuweathernight.txt
echo ''
count=1
while [ $count -le 7 ]
do
# img[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'ForecastIcon' | sed -n $"$count"p | awk -F'src="' '{print $2}' | awk -F'" ' '{print $1}')
# imgname[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'ForecastIcon' | sed -n $"$count"p | awk -F'blue/' '{print $2}' | awk -F'" ' '{print $1}')
day[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'textmedbold' | sed -n $"$count"p | awk -F'lblDate">' '{print $2}'| awk -F'</span></div>' '{print $1}')
desc[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'lblDesc' | sed -n $"$count"p | awk -F'lblDesc">' '{print $2}'| awk -F'</span></div>' '{print $1}')
label[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'Label1' | sed -n $"$count"p | awk -F'Label1">' '{print $2}'| awk -F'</span>' '{print $1}')
temp[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'Label1' | sed -n $"$count"p | awk -F'lblHigh">' '{print $2}' | awk -F'</span></div>' '{print $1}' | sed -e 's/°/°/')
feel[$count]=$(cat /tmp/accuweatherday.txt | grep -n 'RealFeelValue' | sed -n $"$count"p | awk -F'RealFeelValue">' '{print $2}'| awk -F'</span></div>' '{print $1}' | sed -e 's/°/°/')
# imgnight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'ForecastIcon' | sed -n $"$count"p | awk -F'src="' '{print $2}' | awk -F'" ' '{print $1}')
# imgnamenight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'ForecastIcon' | sed -n $"$count"p | awk -F'blue/' '{print $2}' | awk -F'" ' '{print $1}')
daynight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'textmedbold' | sed -n $"$count"p | awk -F'lblDate">' '{print $2}'| awk -F'</span></div>' '{print $1}')
descnight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'lblDesc' | sed -n $"$count"p | awk -F'lblDesc">' '{print $2}'| awk -F'</span></div>' '{print $1}')
labelnight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'Label1' | sed -n $"$count"p | awk -F'Label1">' '{print $2}'| awk -F'</span>' '{print $1}')
tempnight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'Label1' | sed -n $"$count"p | awk -F'lblHigh">' '{print $2}' | awk -F'</span></div>' '{print $1}' | sed -e 's/°/°/')
feelnight[$count]=$(cat /tmp/accuweathernight.txt | grep -n 'RealFeelValue' | sed -n $"$count"p | awk -F'RealFeelValue">' '{print $2}'| awk -F'</span></div>' '{print $1}' | sed -e 's/°/°/')
echo $count
echo ${day[$count]}
# echo ${img[$count]}
# echo ${imgname[$count]}
echo ${desc[$count]}
echo ${label[$count]} ${temp[$count]}
echo Realfeel ${feel[$count]}
echo ''
echo $count
echo ${daynight[$count]}
# echo ${imgnight[$count]}
# echo ${imgnamenight[$count]}
echo ${descnight[$count]}
echo ${labelnight[$count]} ${temp[$count]}
echo Realfeel ${feelnight[$count]}
echo ''
count=`expr $count + 1`
done
exit sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
Have you tried weather-util? It's in the Debian repos.
EDIT: Here's what weather-util gives for my local conditions and forecast:
Current conditions at Raleigh-Durham International Airport, NC (KRDU)
Last updated Dec 03, 2010 - 01:51 PM EST / 2010.12.03 1851 UTC
Temperature: 48.0 F (8.9 C)
Relative Humidity: 38%
Wind: from the WNW (290 degrees) at 7 MPH (6 KT)
Sky conditions: mostly cloudy
City Forecast for Raleigh Durham, NC
Issued Friday morning - Dec 3, 2010
Friday... Partly cloudy, high 48, 5% chance of precipitation.
Friday night... Low 27, 5% chance of precipitation.
Saturday... Mostly cloudy, high 47, 10% chance of precipitation.
Saturday night... Low 31.
Sunday... High 45.while ( ! ( succeed = try() ) );
We've earned a reputation as a nice, friendly community; please help us keep it that way.
Offline
Have you tried weather-util? It's in the Debian repos.
I don't need the weather forecast
I was just exercising with sed/grep/awk scripting and assigning values to array variables. 
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
Ah. Cool! Tried it with a similar US page from AccuWeather (tested the URL in Iceweasel to make sure it worked first); looks like the script would need some regional tweaking. The Deutsch version is bril though!
while ( ! ( succeed = try() ) );
We've earned a reputation as a nice, friendly community; please help us keep it that way.
Offline
I found the idea of using an internet weather service to display weather information in a terminal here on the forum. The general idea is to use the "-dump" option of a terminal browser like elinks or lynx and parse its output.
@Luc,
I just tried this, but the temperature today is below 0 and this part: s/-.*// deletes it.
If i leave this part out it will show other unwanted text.
Do you know a way to fix this? I tried, but don't know how to do it.
Thank you,
Tuna
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
hey tuna,
sorry I did not look at this thread for a long time (just this morning). I now checked about the bug for negative temp, and noticed that i have a new version of the function in my basrc since long ago:
weather () {
elinks -dump "google.com/search?hl=en&q=weather+${1:-munich}" | sed -n '/Weather for/,${/Weather/{s/^ *//;s/-.*//;};/iGoogle/d;s/|.*//;p;/Humidity/q;}'
}It does not break:
Weather for Munich, Germany
-2°C
Current: Mostly Cloudy
Wind: E at 14 km/h
Humidity: 80%and also it uses only one instance of sed. The thing that fixed the bug was putting the substitute command 's/-.*//' behind the condition '/Weather/'
I will have a look at www.accuweather.com and your script soon.
luc
Offline
hey tuna,
sorry I did not look at this thread for a long time (just this morning). I now checked about the bug for negative temp, and noticed that i have a new version of the function in my basrc since long ago:
Thanks, no need to say sorry, i posted some hours ago 
Your solution works... i've to examine it to see what exactly is happening. sed has a lot of mysteries to me.
I had to test it for Munich, it's a bit warmer in Nijmegen now 
Tuna
Last edited by Tunafish (2011-02-21 13:00:20)
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
Your solution works... i've to examine it to see what exactly is happening. sed has a lot of mysteries to me
i always look here when I need something in sed. with that and some testing I figured out all the sed I know so far.
I had to test it for Munich, it's a bit warmer in Nijmegen now
lucky you 
Offline
@luc:
One more question: at the end of the first and second line there's a space (i think). I tried to alignr in conky, but it leaves a small space at the end. I replaced every space (just to test it), but it's still there. Do you know what these blanks are?
I've bookmarked the tutorial! Thanks.
Tuna
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
yes the spaces are there for me too (doesn't matter to me). If you want to get rid of them
sed 's/ $//'(works for me)
how did you remove "every space"? b/c if i do this
weather | sed 's/ //g'there are no spaces left. also those at the eol are gone.
how are you looking for the spaces anyway? I take it you use an editor? b/c in the terminal I can select any place even if there has nothing been printed there jet.
Offline
yes the spaces are there for me too (doesn't matter to me). If you want to get rid of them
sed 's/ $//'(works for me)
how did you remove "every space"? b/c if i do thisweather | sed 's/ //g'there are no spaces left. also those at the eol are gone.
how are you looking for the spaces anyway? I take it you use an editor? b/c in the terminal I can select any place even if there has nothing been printed there jet.
The spaces (or whatever they are) at the end of line 1 and 2 are still there. I opened the output in gedit and only these two lines have something at the end... a tab maybe?
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
okay you are right that is no space. (my last post was backed up by a bsd version of sed which oddly just did delete it)
I have tested it like this now and as you can see it is not a space nor a tab but seems to be a two byte character.
weather > FILE
for a in {1..30} ; do
echo ;
echo char $a ;
for i in {1..5}; do
echo line $i;
sed -n ${i}p FILE | cut -b $a- | cmp -b - /dev/zero
done;
doneyou can still get ridd of it like this :
weather | sed '1,2s/.$//'Offline
I am following this closely - nice tip luc - thanks & keep us posted
Offline
you can still get ridd of it like this :
weather | sed '1,2s/.$//'
Thank you, luc.
sed 's/stress/relaxation/g'
Privacy & Security on #!
Offline
In what directory should I paste the weather script?
"You see, but you do not observe." -Sherlock Holmes
Offline
^ Posts to threads more than 6 months old is considered Necro Posting..
This one is almost 2 years since the last response... Please start a new thread and reference this one
/closed
VSIDO
If you build it, they will come...
Words That Build Or Destroy
Offline
Pages: 1
Topic closed
Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.