Page 1 of 12 12311 ... LastLast
Results 1 to 10 of 111

Thread: Function to show weather forecast in the terminal

  1. #1
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Function to show weather forecast in the terminal

    Hi Guys,

    I found this function that works for the most part.

    Code:
    weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<LOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}
    See the page:http://www.commandlinefu.com/command...your-location.

    My questions are:

    1. Does anyone know how I can get rid of the &amp;dein the output: See below

    October 5, 2013: Overcast. High 10&amp;deg;C (50&amp;deg;F). Winds 14 kph SSW

    2. How I can get the name of the city to show at the top of the forecast like in their example. (See sample output on the link above.)
    Thank you,
    GG -----------

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    1. s/&amp;deg;/°/g; at the beginning of the perl code
    2. in that xml there is no info about the location other than the timezone
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Thanks - I figured you would know right off the bat how to sed that stuff out.
    I don't follow on #2. I thought the place called /<title> was supposed to show the name of the city if it were /location
    Thank you,
    GG -----------

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    no, <title> stores date.
    relevant parts of the xml look like this:
    Code:
    <title>October 5, 2013</title>
    <fcttext>Chance of Rain. High 19&deg;C (66&deg;F). Winds 25 kph SW</fcttext>
    the only thing that you can do is to print out the provided parameter or the default value hardcoded in the function, eg
    Code:
    weather()
    {
      (( $# )) || set -- "London,UK"
      echo "Location: ${1//,/, }"
      curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1" | perl -ne 's/&amp;deg;/°/g;
                                                                                                    s/ \(\d+°F\)//g;
                                                                                                    /<title>([^<]+)/ && printf "%s: ",$1;
                                                                                                    /<fcttext>([^<]+)/ && print $1,"\n"';
    }
    (oneliners are overrated )
    2nd line of the perl block strips Fahrenheits because we don't use them here. If you live in a Fahrenheit country, modify to strip °C with s/\d+°C \((\d+°F)\)/$1/g; but i don't think Sweden is one

    Code:
    $ weather
    Location: London, UK
    October 5, 2013: Chance of Rain. High 19°C. Winds 25 kph SW
    October 6, 2013: Chance of Rain. High 19°C. Winds 10 kph WNW
    October 7, 2013: Clear. High 20°C. Winds 10 kph WSW
    October 8, 2013: Clear. High 20°C. Winds 10 kph SW
    October 9, 2013: Clear. High 19°C. Winds 10 kph West
    October 10, 2013: Scattered Clouds. High 19°C. Winds 10 kph NNE
    $ weather Washington,DC
    Location: Washington, DC
    Today: Mostly sunny. Hot with highs in the upper 80s. Northwest winds around 5 mph. 
    Tonight: Mostly cloudy. Lows in the upper 60s. Southeast winds around 5 mph. 
    Sunday: Partly sunny. Hot with highs in the upper 80s. Southeast winds 5 to 10 mph. 
    Sunday Night: Partly cloudy in the evening...then becoming mostly cloudy. Lows in the mid 60s. South winds 5 to 10 mph. 
    Monday: Mostly cloudy. Showers...mainly in the afternoon. Highs in the upper 70s. South winds 10 to 15 mph. Chance of rain 80 percent.
    Last edited by Vaphell; October 5th, 2013 at 01:33 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Thanks man!

    Location: Falun, Sweden
    October 5, 2013: Overcast. High 10°C. Winds 14 kph SSW
    October 6, 2013: Chance of Rain. High 12°C. Winds 14 kph SSW
    October 7, 2013: Overcast. High 13°C. Winds 14 kph SW
    October 8, 2013: Partly Cloudy. High 16°C. Winds 14 kph WSW
    October 9, 2013: Overcast. High 15°C. Winds 25 kph WSW
    October 10, 2013: Scattered Clouds. High 14°C. Winds 21 kph NNW
    Thank you,
    GG -----------

  6. #6
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Quote Originally Posted by Vaphell View Post
    but i don't think Sweden is one
    We use it only if the month has 32 days.
    Thank you,
    GG -----------

  7. #7
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    @Vaphell

    OK Boss, I need some more guidance

    Can you explain why when I run this script:

    Code:
    #!/bin/bash
    echo
    {  curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$1" | perl -ne 's/&amp;deg;/°/g;
                                                                                                    s/ \(\d+°F\)//g;
                                                                                                    /<title>([^<]+)/ && printf "%s: ",$1;
                                                                                                    /<fcttext>([^<]+)/ && print $1,"\n"';
    }
    Code:
    weather Las Vegas
    I'll get a forecast (although in F)

    but more often than not particularly if the name of the city has Grand in it. Example: Grand Cayman,Cayman Islands
    the script returns nothing. In that case I need to add the %20 between the words.

    Is this because of the way the website is coded or something in the command I am issuing?
    Thank you,
    GG -----------

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    forecast for USian locations seems to be in a different format, it's more verbose. Probably a different data provider so these s/// related to degrees won't work. You'd have to extract the numbers and convert them by hand, but there is another pain - there are no indicators of degrees so you'd have to recognize which numbers are related to temperatures and which to winds.

    Either way when you have http://something/something/index.xml?query=something something,something something, the url will obviously end at the first space (as evidenced by the forum software ). In other words space is not a valid url char and has to be replaced with %20. Sure, you can type url with spaces into the webbrowser and it will work but it's a convenience for nublet webusers. Standards are there for a reason
    replace $1 with ${1// /%20}.

    You need to type the location as a single parameter, eg quoted because $1. weather Las Vegas will return 'Location: Las' because Vegas is $2 and doesn't matter.
    If you want your script/function more tolerant you'd have to code around the cases where you can feed multiple params
    something like
    Code:
    (( $# )) || set -- "Falun, Sweden"
    loc=$*
    echo "Location: $loc"
    url="http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${loc// /%20}"
      
    curl -s "$url" | perl -ne 's/&amp;deg;/°/g;
                               s/ \(\d+°F\)//g;
                               /<title>([^<]+)/ && printf "%s: ",$1;
                               /<fcttext>([^<]+)/ && print $1,"\n";'
    difference between $@ and $* is this
    Code:
    $ set -- a b c d e
    $ printf "[%s]\n" "$*"
    [a b c d e]
    $ printf "[%s]\n" "$@"
    [a]
    [b]
    [c]
    [d]
    [e]
    $* glues all parameters into a single string using first char of IFS as a glue (obviously space by default).
    Code:
    $ IFS=:; printf "[%s]\n" "$*"
    [a:b:c:d:e]
    Last edited by Vaphell; October 6th, 2013 at 01:26 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    4,952

    Re: Function to show weather forecast in the terminal

    Quote Originally Posted by GrouchyGaijin View Post
    http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?
    That service seems to be deprecated.
    Wunderground.com has moved to a purchased-key model for api access. See http://www.wunderground.com/weather/api/

    For US locations, I use data from the National Weather Service. No api key required. For how I do it, see http://cheesehead-techblog.blogspot....nfo-feeds.html

    The Ubuntu Phone weather app gets worldwide data from OpenWeatherMap.org. It also uses an api key for access, but the price is $0. See http://openweathermap.org/API#forecast
    Last edited by ian-weisser; October 6th, 2013 at 02:43 PM.

  10. #10
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Man, you are a WIZARD
    Thank you,
    GG -----------

Page 1 of 12 12311 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •