I’ve written a Ruby script called bbc-weather.rb that parses the BBC weather RSS for a particular location and formats the data for conky. Here’s an example:
To use it insert the following in your .conkyrc:
${execpi 600 ruby /path/to/bbc-weather.rb 4197}
This will refresh every hour. Replace 4197 with the number of your local weather station. You can find this by looking at the BBC weather URL which is of the form weather/forecast/XXXX
. You also need to define two colours for the script to use, in the options section before TEXT, like so:
color1 white color2 grey
You might also need the following, as conky limits the about of text it will read from a sub-process:
text_buffer_size 2048
UPDATE: apparently this script no longer works with recent BBC Weather updates. Check out the comments for an improved version.
Love your script, thank you! Is there any way to make it show F instead of C in temperature?
May 3, 2011 @ 5:30 pm
I’ve updated the script so you can pass ‘c’ or ‘f’ as a second argument to put temperatures in centigrade or fahrenheit.
May 8, 2011 @ 12:46 pm
hi mate having trouble with this script
bbc-weather.rb:45: syntax error, unexpected $end, expecting keyword_end
“#{(9 * val.to_i) / 5 + 32}°F”
tried adding a few ends but no luck? is there a small error possibly?
November 26, 2011 @ 7:26 pm
sorted just add
# encoding: utf-8
at the top of the script and its working like a charm now thanks!
November 26, 2011 @ 7:31 pm
Thanks! I updated the script.
November 29, 2011 @ 7:22 am
It breaks for negative temparatures: I changed (\d+) to (-?\d+) in three places.
February 8, 2012 @ 3:55 am
Thanks for that. I’ve updated the script. Can’t test it at the moment (thankfully)
February 14, 2012 @ 8:25 pm
The BBC have moved the rss feeds – I’m getting 404 errors. I think the new url should be something like:
url = “http://open.live.bbc.co.uk/weather/feeds/en/#{loc}/#{type}.rss”
and both types (forecast and observations) have changed.
…
Now I’ve remedied the 404s but I’m getting Parse Errors, so I think the feeds have changed fundamentally enough that this script needs a full rewrite, and I don’t have enough ruby to do it.
December 21, 2012 @ 4:44 pm
This works with the current bbc rss format (observation and 3 day forecast):
#!/usr/bin/ruby
require ‘net/http’
require ‘xmlsimple’
# This script fetches the weather from the Yahoo! Weather RSS feed and prints it out.
# This can be used for Conky, for example.
# By Brian Carper
# http://briancarper.net
RSS_FEED_URL_CURRENT = ‘http://open.live.bbc.co.uk/weather/feeds/en/-your-number-/observations.rss’
RSS_FEED_URL_FORECAST = ‘http://open.live.bbc.co.uk/weather/feeds/en/-your-number-/3dayforecast.rss’
if RSS_FEED_URL_CURRENT == ” then
puts ‘Edit the script to specify’
puts ‘your RSS feed URL from’
puts ‘BBC Weatheer’
exit
end
if RSS_FEED_URL_FORECAST == ” then
puts ‘Edit the script to specify’
puts ‘your RSS feed URL from’
puts ‘BBC Weatheer’
exit
end
begin
text_current = Net::HTTP.get(URI.parse(RSS_FEED_URL_CURRENT))
rescue
puts “ERROR: Failed fetching RSS feed!”
exit
end
begin
text_forecast = Net::HTTP.get(URI.parse(RSS_FEED_URL_FORECAST))
rescue
puts “ERROR: Failed fetching RSS feed!”
exit
end
begin
xml_current = XmlSimple.xml_in(text_current)
channel_current = xml_current[‘channel’][0]
rescue
puts “Error: Could not parse the XML!”
exit
end
begin
xml_forecast = XmlSimple.xml_in(text_forecast)
channel_forecast = xml_forecast[‘channel’][0]
rescue
puts “Error: Could not parse the XML!”
exit
end
puts “Current weather: ” + channel_current[‘item’][0][‘title’][0]
puts “” + channel_current[‘item’][0][‘description’][0]
puts “Forecast: ” + channel_forecast[‘item’][0][‘title’][0]
puts “” + channel_forecast[‘item’][1][‘title’][0]
puts “” + channel_forecast[‘item’][2][‘title’][0]
Run the script as usual, except without parameters. Place your weather location in the script
March 5, 2013 @ 4:26 pm
OK, just in case there is still interest in this. I’ve done two Ruby scripts, one for BBC Weather and the other for Yahoo! Weather. You can cut and paste:
1) conky-bbc-weather.rb:
#!/usr/bin/ruby
require ‘net/http’
require ‘xmlsimple’
# This script fetches the weather from the BBC Weather RSS feed and prints it out.
# This can be used for Conky, for example.
# Original version for Yahoo! Weather By Brian Carper
# http://briancarper.net
# Rewritten for BBC Weather by Kinley Dorji
# You need to ‘gem install xml-simple’ if you don’t have it
# BBC Weather is very verbose, and Conky has problems with wordiness and word wrap.
# So mgsub() helps us to rid ourselves of much repeated ‘Maximum Temperature’
# and ‘Minimum Temperature’
# and wrap() breaks up the sentences to your desired width (present default: 48 chars)
# Copy this script to conky-bbc-weather.rb and run it from within conky:
# ${execpi 1800 ruby /path/to/conky-bbc-weather.rb}
# Enjoy!
def wrap(s, width=48)
s.gsub(/(.{1,#{width}})(\s+|\Z)/, “\\1\n”)
end
class String
def mgsub(key_value_pairs=[].freeze)
regexp_fragments = key_value_pairs.collect { |k,v| k }
gsub(Regexp.union(*regexp_fragments)) do |match|
key_value_pairs.detect{|k,v| k =~ match}[1]
end
end
end
RSS_FEED_URL_CURRENT = ‘http://open.live.bbc.co.uk/weather/feeds/en/1252416/observations.rss’
RSS_FEED_URL_FORECAST = ‘http://open.live.bbc.co.uk/weather/feeds/en/1252416/3dayforecast.rss’
if RSS_FEED_URL_CURRENT == ” then
puts ‘Edit the script to specify’
puts ‘your RSS feed URL from’
puts ‘BBC Weatheer’
exit
end
if RSS_FEED_URL_FORECAST == ” then
puts ‘Edit the script to specify’
puts ‘your RSS feed URL from’
puts ‘BBC Weatheer’
exit
end
begin
text_current = Net::HTTP.get(URI.parse(RSS_FEED_URL_CURRENT))
rescue
puts “ERROR: Failed fetching RSS feed!”
exit
end
begin
text_forecast = Net::HTTP.get(URI.parse(RSS_FEED_URL_FORECAST))
rescue
puts “ERROR: Failed fetching RSS feed!”
exit
end
begin
xml_current = XmlSimple.xml_in(text_current)
channel_current = xml_current[‘channel’][0]
rescue
puts “Error: Could not parse the XML!”
exit
end
begin
xml_forecast = XmlSimple.xml_in(text_forecast)
channel_forecast = xml_forecast[‘channel’][0]
rescue
puts “Error: Could not parse the XML!”
exit
end
# Observation title
puts wrap(“Current: ” + channel_current[‘item’][0][‘title’][0])
# Observation description, very verbose. Uncomment at your own risk!
#puts wrap(“” + channel_current[‘item’][0][‘description’][0])
# 3 day Forecast – wrap words and eliminate some verbosity
puts wrap((“Forecast: ” + channel_forecast[‘item’][0][‘title’][0]).mgsub([[/Maximum Temperature/i, ‘Max’], [/Minimum Temperature/i, ‘Min’]]))
puts wrap((“” + channel_forecast[‘item’][1][‘title’][0]).mgsub([[/Maximum Temperature/i, ‘Max’], [/Minimum Temperature/i, ‘Min’]]))
puts = wrap((“” + channel_forecast[‘item’][2][‘title’][0]).mgsub([[/Maximum Temperature/i, ‘Max’], [/Minimum Temperature/i, ‘Min’]]))
2) conky-yahoo-weather.rb:
#!/usr/bin/ruby
require ‘net/http’
require ‘xmlsimple’
# This script fetches the weather from the Yahoo Weather RSS feed and prints it out.
# This can be used for Conky, for example.
# Original version for Yahoo! Weather By Brian Carper
# http://briancarper.net
# Rewritten by Kinley Dorji following changes in the Yahoo Weather feed
# You need to ‘gem install xml-simple’ if you don’t have it
# Copy this script to conky-yahoo-weather.rb and run it from within conky:
# ${execpi 1800 ruby /path/to/conky-yahoo-weather.rb}
# Enjoy!
RSS_FEED_URL = ‘http://xml.weather.yahoo.com/forecastrss?w=1887896&u=c’
if RSS_FEED_URL == ” then
puts ‘Edit the script to specify’
puts ‘your RSS feed URL from’
puts ‘http://weather.yahoo.com’
exit
end
begin
text = Net::HTTP.get(URI.parse(RSS_FEED_URL))
rescue
puts “ERROR: Failed fetching RSS feed!”
exit
end
begin
xml = XmlSimple.xml_in(text)
channel = xml[‘channel’][0]
rescue
puts “Error: Could not parse the XML!”
exit
end
temp_units = ‘ ‘ + channel[‘units’][0][‘temperature’]
baro_units = ‘ ‘ + channel[‘units’][0][‘pressure’]
wind_units = ‘ ‘ + channel[‘units’][0][‘speed’]
puts “Current weather: ” + channel[‘item’][0][‘condition’][0][‘text’] + ‘, ‘ + channel[‘item’][0][‘condition’][0][‘temp’] + temp_units
puts “Humidity: ” + channel[‘atmosphere’][0][‘humidity’] + ‘%’ + ” Wind: ” + channel[‘wind’][0][‘speed’] + wind_units + ” Barometer: ” + channel[‘atmosphere’][0][‘pressure’] + baro_units
puts “”
puts “Forecast: ” + channel[‘item’][0][‘forecast’][0][‘day’] + ‘, ‘ + channel[‘item’][0][‘forecast’][0][‘text’] + ‘, High ‘ + channel[‘item’][0][‘forecast’][0][‘high’] + temp_units + ‘ Low ‘ + channel[‘item’][0][‘forecast’][0][‘low’] + temp_units
puts “” + channel[‘item’][0][‘forecast’][1][‘day’] + ‘ ‘ + channel[‘item’][0][‘forecast’][1][‘date’] + ‘, ‘ + channel[‘item’][0][‘forecast’][1][‘text’] + ‘, High ‘ + channel[‘item’][0][‘forecast’][1][‘high’] + temp_units + ‘ Low ‘ + channel[‘item’][0][‘forecast’][1][‘low’] + temp_units
March 6, 2013 @ 3:36 am
The two scripts are updates over the one posted earlier. Also, there is an unnecessary ‘=’ in the last line of the bbc script. Please remove the it, the one in ‘puts = wrap’. It should just be puts wrap.
March 6, 2013 @ 3:45 am
Hello Kinleyd,
After running the script I see yahooweather.rb:2: invalid multibyte char (US-ASCII) or bbc-weather.rb:2: invalid multibyte char (US-ASCII) in the terminal.
What’s wrong? I installed the xml-simple package.
Thanks,
Lee
August 19, 2014 @ 10:08 pm