Register
It is currently Fri Oct 24, 2014 9:24 am

function to get the 10-day forcast


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue May 17, 2005 10:32 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Originally posted by Mekanik

Quote:
if you have internet access, then add this to your ~/.bashrc to have the 10-day forcast displayed, :D



Code:
get_weather() {
printf "Your 10 Day Weather Forcast as follows:\n"
printf "   Date    High / Low (°F)   Precip %%\n"
lynx -dump "http://www.w3.weather.com/weather/print/XXXXX" | sed -n '/%$/s/\[.*\]//p'
printf " \n"
}


Quote:
where XXXXX is your Zip code


Originally posted by jbsnake
Code:
lynx -dump "http://www.w3.weather.com/weather/print/30127" | sed -n '/%$/s/\[.*\]//p'
printf " \n"
}


i changed the above line of code to read
Code:
lynx -dump "http://www.w3.weather.com/weather/print/$1"

that way i could type:
Code:
get_weather 90210

and get different areas :)

o yea...on a side note...did weather.com change anything, cause it just doesn't work anymore :)
might be my network configuration...but nothing else seems affected (and no...it's not the coding changes :P)

Originally posted by Mekanik
yeah, they were doing some funky java coding and broke the code earlier today. even direct access to the URL was toast from a browser. i was having the same problems, but it's been fixed for like the past 3hrs.

*note* make sure to include the sed portion of the script or you won't get the decent ascii output...

here's an update rev,

since you can't use exit's in your functions because it'll log you out, here's one that you can use to dynamically validate the ZIP and extract the forecast data.

Code:
forecast() {
_ZIP=$1

if   [ $# = 1 ];then
     printf "$_ZIP\n" | egrep '^[0-9][0-9][0-9][0-9][0-9]$' >>/dev/null
     if   [ $? = 0 ];then
          printf "Your 10 Day Weather Forecast as follows:\n";
          lynx -dump "http://www.weather.com/weather/print/$_ZIP" | sed -n '/%$/s/\[.*\]//p';
          printf "\n"
     elif [ $? = 1 ];then
          printf "Bad ZIP code!\n"
     fi
elif [ $# != 1 ];then
     printf "You need to supply a ZIP code!\n"
fi
}


-mekanik


here's a patch to set a default ZIP code to extract the 10-day forecast data if you don't specify via the shell...this is also posted on USALUG...

Code:
--- .forecast1  Tue Feb 01 00:00:00 2005
+++ .forecast2  Tue Feb 01 00:00:00 2005
@@ -11,6 +11,8 @@
           printf "Bad ZIP code!\n"
      fi
elif [ $# != 1 ];then
-     printf "You need to supply a ZIP code!\n"
+     printf "Your 10 Day Weather Forecast as follows:\n";
+     lynx -dump "http://www.weather.com/weather/print/${_ZIP:-XXXXX}" | sed -n '/%$/s/\[.*\]//p';
+     printf "\n"
fi
}


enjoy :D,

-mekanik


Top
 Profile WWW  
 PostPosted: Tue Apr 01, 2008 12:46 pm   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
In shell script form, and tweaked a bit.
links worked better for me than lynx, and I know perl better than sed.

Code:
#!/bin/bash
DEFAULTZIP=02138

getforecast() {
        echo "Your 10 Day Weather Forecast as follows:"
        echo "Day, Weather, High/Low (F), Precip. %"
        links -dump "http://www.weather.com/weather/print/$1" | perl -ne '/\d %\s+$/ && s/DEG//g && print'
        echo ""
}

if   [ $# -eq 1 ]; then
        if (echo "$1" | egrep -q '^[0-9][0-9][0-9][0-9][0-9]$'); then
                getforecast $1
        fi 
else
        getforecast $DEFAULTZIP
fi


Top
 Profile  
 PostPosted: Wed Mar 18, 2009 11:31 am   

Joined: Fri Dec 02, 2005 7:05 pm
Posts: 5
Location: Concord, NH
I've taken the weather bot, written by Dave Crouse and adapted by Jeo, and I have combined it with the ten day forecast, written by Daenyth, and the result is the following script. It could be further optimized. Note that it calls two different text Web browsers and two different sites. It works, though. Feel free to modify it and post your improvements to it.

Code:
# forecast
#desc Find current weather stats and forecast for your zip code
#desc Ex: weather 03301
# weather 1.1 -Crouse
# With Updates by Jeo
# Modified to run stand alone by Brian Masinick
# NOTE: This tool uses the elinks text web browser
# Example:  !weather 03301
# Usage: weather + zipcode

  zipcode=$1
  if [ -z "$zipcode" ]; then
    echo "Please provide a zip code (Ex: weather 03301)"
  else
  unset response
# Add a backslash (\) after -dump-width 300 if this line splits
# across two lines; Should be one distinct line:
###################################################################################################################################################
  WEATHER="$(elinks -dump -dump-width 300 "http://mobile.wunderground.com/cgi-bin/findweather/getForecast?query=${zipcode}" | grep -A16 Updated)"

  if [ -z "$WEATHER" ]; then
    response="No Results for $zipcode"
    echo "${response}"
  else
    response[1]="$(echo "$WEATHER" | grep -Eo 'Observed.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print "Weather: " $1}')"
    response[2]="$(echo "$WEATHER" | grep -Eo 'Updated.*' |sed s/\ *\|\ */\|/g |awk -F\| '{print $1}')"
    response[3]="$(echo "$WEATHER" | grep -Eo 'Temperature.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}' | sed s/DEG/\ /g )"
    response[4]="$(echo "$WEATHER" | grep -Eo 'Windchill.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[5]="$(echo "$WEATHER" | grep -Eo 'Wind .*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[6]="$(echo "$WEATHER" | grep -Eo 'Conditions.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[7]="$(echo "$WEATHER" | grep -Eo 'Humidity.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[8]="$(echo "$WEATHER" | grep -Eo 'Dew.Point.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[9]="$(echo "$WEATHER" | grep -Eo 'Pressure.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"

    for index in `seq 1 9`; do
      if [ -n "${response[$index]}" ]; then
        echo "${response[$index]}"
      fi
      let "index = $index + 1"
    done
  fi
  fi

# This part was originally written by Daenyth

DEFAULTZIP=03301

getforecast() {
        echo "Your 10 Day Weather Forecast as follows:"
        echo "Day, Weather, High/Low (F), Precip. %"
        links -dump "http://www.weather.com/weather/print/$1" | perl -ne '/\d %\s+$/ && s/DEG//g && print'
        echo ""
}

if   [ $# -eq 1 ]; then
        if (echo "$1" | egrep -q '^[0-9][0-9][0-9][0-9][0-9]$'); then
                getforecast $1
        fi
else
        getforecast $DEFAULTZIP
fi


Top
 Profile WWW YIM  
 PostPosted: Tue Mar 24, 2009 7:35 pm   

Joined: Fri Dec 02, 2005 7:05 pm
Posts: 5
Location: Concord, NH
I had inadvertently omitted Daenyth in citing the original authors of the script that I pieced together.

Here is a version with updated documentation with the proper credit given. My apologies to Daenyth
for failing to cite his work in my original script.

Code:
#!/bin/bash
# forecast
#desc Find current weather stats and forecast for your zip code
#desc Ex: forecast 03301
# weather 1.1 -Crouse
# With Updates by Jeo
# Modified to run stand alone by Brian Masinick,
# and also added the forecast logic contributed by Daenyth.
# NOTE: This tool uses the links text web browser
# Example:  forecast 03301
# Usage: forecast zipcode

  zipcode=$1
  if [ -z "$zipcode" ]; then
    echo "Please provide a zip code (Ex: weather 03301)"
  else
  unset response
#  Should be one distinct line (using repeated slashes to help):
###################################################################################################################################################
  WEATHER="$(elinks -dump -dump-width 300 "http://mobile.wunderground.com/cgi-bin/findweather/getForecast?query=${zipcode}" | grep -A16 Updated)"

  if [ -z "$WEATHER" ]; then
    response="No Results for $zipcode"
    echo "${response}"
  else
    response[1]="$(echo "$WEATHER" | grep -Eo 'Observed.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print "Weather: " $1}')"
    response[2]="$(echo "$WEATHER" | grep -Eo 'Updated.*' |sed s/\ *\|\ */\|/g |awk -F\| '{print $1}')"
    response[3]="$(echo "$WEATHER" | grep -Eo 'Temperature.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}' | sed s/DEG/\ /g )"
    response[4]="$(echo "$WEATHER" | grep -Eo 'Windchill.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[5]="$(echo "$WEATHER" | grep -Eo 'Wind .*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[6]="$(echo "$WEATHER" | grep -Eo 'Conditions.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[7]="$(echo "$WEATHER" | grep -Eo 'Humidity.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[8]="$(echo "$WEATHER" | grep -Eo 'Dew.Point.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[9]="$(echo "$WEATHER" | grep -Eo 'Pressure.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"

    for index in `seq 1 9`; do
      if [ -n "${response[$index]}" ]; then
        echo "${response[$index]}"
      fi
      let "index = $index + 1"
    done
  fi
  fi

# This section of code was written by Daenyth.

DEFAULTZIP=03301

getforecast() {
        echo "Your 10 Day Weather Forecast as follows:"
        echo "Day, Weather, High/Low (F), Precip. %"
        links -dump "http://www.weather.com/weather/print/$1" | perl -ne '/\d %\s+$/ && s/DEG//g && print'
        echo ""
}

if   [ $# -eq 1 ]; then
        if (echo "$1" | egrep -q '^[0-9][0-9][0-9][0-9][0-9]$'); then
                getforecast $1
        fi
else
        getforecast $DEFAULTZIP
fi


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP