SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2010-10-07 13:55:06

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

plain text weather on command line

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

Help fund CrunchBang, donate to the project!

#2 2010-12-03 19:08:27

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

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/&deg;/°/')
  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/&deg;/°/')

#  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/&deg;/°/')
  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/&deg;/°/')

  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

#3 2010-12-03 19:26:40

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 11,465

Re: plain text weather on command line

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.

Yes; as a matter of fact, I am a cop.  Why do you ask? cool
I'm a moderator here.  How are we doing?  Feedback is encouraged.

Online

#4 2010-12-04 10:10:23

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

pvsage wrote:

Have you tried weather-util?  It's in the Debian repos.

I don't need the weather forecast smile   I was just exercising with sed/grep/awk scripting and assigning values to array variables.  smile


sed 's/stress/relaxation/g'
Privacy & Security on #!

Offline

#5 2010-12-04 14:57:10

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 11,465

Re: plain text weather on command line

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!


Yes; as a matter of fact, I am a cop.  Why do you ask? cool
I'm a moderator here.  How are we doing?  Feedback is encouraged.

Online

#6 2011-02-21 10:54:14

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

luc wrote:

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

#7 2011-02-21 12:11:32

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: plain text weather on command line

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

#8 2011-02-21 12:34:53

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

luc wrote:

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 smile

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 smile

Tuna

Last edited by Tunafish (2011-02-21 13:00:20)


sed 's/stress/relaxation/g'
Privacy & Security on #!

Offline

#9 2011-02-21 12:54:43

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: plain text weather on command line

Tunafish wrote:

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.

Tunafish wrote:

I had to test it for Munich, it's a bit warmer in Nijmegen now smile

lucky you smile

Offline

#10 2011-02-21 13:00:08

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

@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

#11 2011-02-21 15:09:18

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: plain text weather on command line

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

#12 2011-02-21 15:33:04

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

luc wrote:

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.

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

#13 2011-02-21 15:58:55

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: plain text weather on command line

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; 
done

you can still get ridd of it like this :

weather | sed '1,2s/.$//'

Offline

#14 2011-02-24 05:59:44

popslee
#! Member
From: so cal
Registered: 2011-02-11
Posts: 51

Re: plain text weather on command line

I am following this closely - nice tip luc - thanks & keep us posted

Offline

#15 2011-02-24 12:37:12

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: plain text weather on command line

luc wrote:

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

#16 2013-01-21 03:17:12

observingly
#! Member
From: Manila, Philippines
Registered: 2013-01-13
Posts: 88
Website

Re: plain text weather on command line

In what directory should I paste the weather script?


"You see, but you do not observe." -Sherlock Holmes

Offline

#17 2013-01-21 04:09:21

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 10,163
Website

Re: plain text weather on command line

^ 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 | SolusOS

Words That Build Or Destroy

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo