Add updated weather to your BASH prompt

January 4th, 2010 | by Pyro222 |

This post was inspired by a friend of mine. A complete weather junkie who is constantly checking the weather online. I once asked him if he would pay for a digital picture frame that only displayed live Doppler all day and he undoubtedly replied, “absolutely”. That project may be a ways off however, I’ve been reading up on how to get more out of your bash prompt so I thought, for a start, why not put the weather in your prompt. Not sure of its practicality but I can honestly say I know folks crazy enough to enjoy this. Read on for step by step instructions.

The rundown

The concept is fairly simple. Schedule a custom php script under crontab that will parse a weather rss feed and create a file containing weather info that we can use in our custom prompt. For the seasoned geek, thats probably all the info you needed, however, if you are new to bash or php, follow the tutorial below and I will outline a simple way to accomplish this.

Writing the PHP script

To proceed you will need to have a php-cli installed on your system. This will enable us to execute/script php code from the command line. For my Ubuntu system, I have installed the “php5-cli” package.

  1. First you need to setup a weather feed from WeatherBug. Go to http://rss.weatherbug.com/listfeeds.aspx and put in your location or postal code and hit the “Get Feed URL” button to get a weather feed url. The url will appear at the bottom textbox of the page. Be sure you check only the “Current Conditions (Text Only Version)” box and also be sure to select Fahrenheit, or Celsius.
  2. Create a folder “~/weatherbash/” , under that folder create a new file called “weatherbash.php” and open it in your text editor of choice.
  3. Paste the following code into “weatherbash.php”.
  4. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    <?
    $w_unit = "f";
    $w_feed = 'http://feeds.weatherbug.com/rss.aspx?zipcode=95670&feed=currtxt&zcode=z4641';
    $data = file_get_contents($w_feed);
    $s_pos = stripos($data, "<item>");
    $e_pos = stripos($data, "</item>");
    $data = strip_tags(html_entity_decode (htmlspecialchars_decode(substr($data, $s_pos, $e_pos))));
    preg_match_all('/([\d]+)/', $data, $match);
    $fp = fopen('wf.dat', "w");
    $w_data = array();
    $w_data = $match[0];
    if (!empty($w_data))fwrite($fp, "t:".$w_data[0].".".$w_data[1] . "$w_unit" ." h:".$w_data[2] ."% "."w:".$w_data[3]."mph"." r:".$w_data[8].".".$w_data[9].'"');
    fclose($fp);
    ?>
  5. Change the “$w_feed” variable on line 3 to = your new feed URL from the previous step. Also make sure you have the correct “$w_unit” on line 2 and you may also wish to change the filename and/or location on line 9.
  6. Save and copy “weatherbash.php” to anywhere you wish. I just put mine in “~/weatherbash”.

As you can see the script simply retrieves and extracts the “Temperature:” data from the feed. It then creates a file called wf.dat that will contain the values obtained from our feed. We will use this file in our prompt.

Scheduling the php script

Now we will set our new php script to run every 15 min. You can change this interval to more or less by changing the code below.

  1. First, open a terminal and type “crontab -e” (you may wish to be root for this section in which case make sure you use the complete path to the weatherbash.php script).
  2. Paste in the following code. Change your weatherbash.php location accordingly.
  3. 1
    
    15,30,45,59 * * * * php ~/weatherbash/weatherbash.php
  4. Press “ctl”+”x” to exit, type “y” to save and enter to confirm filename.

Thats it for scheduling. Our script will now run every 15 minutes keeping our system weather file up-to-date.

Set the bash prompt

  1. To temporary use the prompt, simply type in the following code into your terminal session. (make sure you have run the php script at least once)
    1
    
    PS1="(\[\033[36m\]\$(head ~/weatherbash/wf.dat)\[\033[m\])\n$PS1"
  2. To permanently set the prompt, simply find the “PS1 =” line in your “~/.bashrc” file and paste in the following code after the first quotation mark.
  3. 1
    
    PS1="(\[\033[36m\]\$(head ~/weatherbash/wf.dat)\[\033[m\])\n***the rest of your current prompt goes here***"

You should now have a prompt that displays the current weather contitions.

Note: You can change the color of the prompt by changing the “36m” value. Also make sure your file locations are correct.

Updates

I’ve also recently plugged this data into my ~/.conkyrc and schedule conky to execute the script rather than cron.

1
2
${color orange}WEATHER${hr 2}$color 
${color white}${execi 300 php /path_to/weatherbash.php > /path_to/err.dat ; head /path_to/wf.dat | fold -s -w50}

the “> /path_to/err.dat” portion is to prevent the script from echoing any error data to conky and redirect it to a log file.


For the serious geek, you could modify your fetch script to output raw data and play with those numbers using a huge number of Conky’s huge Cadillac array of objects.
You can also use the “rss” variable from conky to parse the feed into conky as well.

Sources

Similar Entries

Sorry, comments for this entry are closed at this time.