Sunday 25 January 2015

Rendering images showing the weather forecast from an RSS Feed

I created this bash shell script to generate images showing the weather forecast for my region for the next 2 days, using the BBC Weather RSS feed as a source.

The script is scheduled hourly using crond on one of my Raspberry Pi devices, and outputs 2 images to the local Apache instance web root directory. The images are composed by first parsing the XML from the RSS feed, extracting, transforming and formatting the required values, then generating the resultant images using the ImageMagick convert tool.

I then point my SqueezeBox Radio Image Viewer screensaver at an .lst file containing the URLs of the 2 images, and now each day I wake up to a handy 2 day forecast to help me plan my day!

This script is far from optimised, but it does the job - feel free to try it yourself or suggest modifications and additions to improve it.


NOTES

  • The BBC Weather RSS feed only covers the UK, and you will also need to find out your 'area code' and change it in the supplied URL to get the forecast for your specific region.
  • The script heavily relies on the layout and format of the feed always being the same - if the BBC decide to change it I'll have to adapt the script accordingly.
  • The images are created at a fixed size (320 x 240) to allow display on the SqueezeBox radio screen - you can create them at any size required though.


SAMPLE OUTPUT





THE SCRIPT

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash

#CREATE IMAGE FOR TODAY
today_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[1])' -`

#text=`echo $text | sed -r 's/, /\\\n/g'`

weekday_today=`echo $today_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

today_text=`echo $today_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
today_text=`echo $today_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
today_text=`echo $today_text | sed -r 's/.*://g'`
today_text=`echo $today_text | sed -r 's/;/:/g'`
today_text=`echo $today_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$today_text" /var/www/weather_forecast_today.jpg
convert /var/www/weather_forecast_today.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_today" /var/www/weather_forecast_today.jpg



#CREATE FORECAST IMAGE FOR TOMORROW
tomorrow_text=`curl -s --location --header "Accept: application/rdf+xml" \
http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss | xmllint --format --xpath 'string((//channel/item/title)[2])' -`

weekday_tomorrow=`echo $tomorrow_text | sed -r 's/day:.*/day/g' | colrm 4 | \
sed -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`

tomorrow_text=`echo $tomorrow_text | sed -r 's/Maximum Temperature:/\\\nMax Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/Minimum Temperature:/\\\nMin Temp;/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/.*://g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/;/:/g'`
tomorrow_text=`echo $tomorrow_text | sed -r 's/,/\\\n/g'`

convert -background black -gravity center -fill white -font Helvetica -size 320x240 -pointsize 22 -interline-spacing 10 \
label:"\n$tomorrow_text" /var/www/weather_forecast_tomorrow.jpg
convert /var/www/weather_forecast_tomorrow.jpg -font Helvetica -pointsize 80 -fill rgba\(173,216,230,0.8\) -gravity northWest \
-annotate 0 "$weekday_tomorrow" /var/www/weather_forecast_tomorrow.jpg

No comments:

Post a Comment