Topic: Weather script (for use with Conky)

When I went to use the weather command in Conky, I was annoyed that the closest location I could use was Philadelphia (I live about 20-30 minutes west of there).  Therefore, I decided to use AccuWeather's website to obtain the current temperature.  I will probably modify the script to also give the forecast, but that's for later.  As of now, the script stand like this:

#!/bin/bash                                                                                             
wget -q http://www.accuweather.com/us/pa/wayne/19087/city-weather-forecast.asp?unit=c
cat city-weather-forecast.asp?unit=c | grep '<div class="info"> <span class="cond">' > current.txt
cat current.txt | grep -o [0-9] > current2.txt
cat current2.txt | sed '/.*[^\.]$/N;s/\n *//'
rm city-weather-forecast.asp?unit=c
rm current.txt
rm curent2.txt

This script returns the weather in degrees celcius.  To change it to fahrenheit, simply remove the ?unit=c in the first, second, and fifth lines.  Save this script somewhere like your home directory or a scripts folder or something.

To use with Conky, place this line in your config file

Temperature: ${execi INTERVAL /path/to/above/script}C

And that's it!
Feel free to leave feedback!

- Chaanakya

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

I did something similar before. And i think you can leave out some temp files (or all of them):

curl -q http://www.accuweather.com/us/pa/wayne/ … asp?unit=c | grep '<div class="info"> <span class="cond">' | grep -o [0-9] | sed '/.*[^\.]$/N;s/\n *//'

Re: Weather script (for use with Conky)

you can also parse google like this.

Re: Weather script (for use with Conky)

Yeah...I just realized I can leave out the temp files...my new script looks like this:

#!/bin/bash
wget -q -O - http://www.accuweather.com/us/pa/wayne/19087/city-weather-forecast.asp?unit=c | grep '<div class="info"> <span class="cond">' | grep -o [0-9] | sed '/.*[^\.]$/N;s/\n *//'
Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

In fact, I even got it to display the degree symbol!  Here's how:

CHANGES TO THE SCRIPT:

The script now looks like this:

#!/bin/bash
WEATHER=`wget -q -O - [url]http://www.accuweather.com/us/pa/wayne/ZIPCODE/city-weather-forecast.asp?unit=c[/url] | grep '<div class="info"> <span class="cond">' | grep -o [0-9] | sed '/.*[^\.]$/N;s/\n *//'`
WEATHER=$WEATHER°

(I forgot to remove my zipcode tongue ) Change ZIPCODE to your zipcode.  If you live outside the U.S, your url may change...not sure though...

CHANGE TO CONKY CONFIG:

The Conky config line now looks like this:

Temperature: ${execi INTERVAL /path/to/script | iconv -f UTF8 -t ISO885915}C

Last edited by chaanakya (2011-02-21 15:28:26)

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

@chaanakya
why are you using the last sed command? it should be redundant as the "grep -o [0-9]" will (due to -o) only print the matching part of the line and as you are asking it for "[0-9]" only numbers there should be no spaces or dots in the output for sed to delete. (on one machine this sed command even deletes everything but i don't understand why hmm )
or am I misunderstanding something here?

Re: Weather script (for use with Conky)

The problem is that the grep line prints each match on a separate line.  I'm using sed to concatenate all of the numbers.  There's probably a better way to do it though big_smile

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

I also came up with a similar script which will print the current conditions as well:

#!/bin/bash
wget -q -O - http://www.accuweather.com/us/pa/wayne/ZIPCODE/city-weather-forecast.asp?unit=c | grep -o '<div class="info"> <span class="cond">[A-Z].*</span> <strong' | sed -e 's/.*<span class="cond".//' | sed -e 's/<\/span>.*//'

And to use with Conky, simply put

Conditions: ${execi INTERVAL /path/to/above/script}

See my last script update for notes on ZIPCODE.

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

you could replace the "ZIPCODE" with

${1:-19087}

explanation here (just remember that "$1" is the first argument of your script)

Re: Weather script (for use with Conky)

Oh thanks smile  I was thinking about doing that actually.  I will update that script and (I guess) post it back up here.

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Re: Weather script (for use with Conky)

Just realized...the URL could be completely different depending on the state...the URL should really be

http://www.accuweather.com/us/STATE/CITY/ZIPCODE/city-weather-forecast.asp?unit=c

replacing STATE, CITY, and ZIPCODE as appropriate.  Of course, the other way is to search your location on accuweather and substitute that URL for the one I have provided.

Last edited by chaanakya (2011-02-24 21:41:25)

Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1