Today's Cinema Times

Note: This script has been expanded to include the Odeon chain of cinemas. Following on from yesterday's post about geektool, I wanted to include today's cinema times on my desktop.

I have two cinemas near me that I use regularly, an Odeon and a Vue cinema. Vue has kindly provided an RSS feed for the day's films. This makes things relatively straightforward. This wasn't my first attempt - I made a hash of that - but this is relatively neat and whilst there may be some optimisation it'd be fiddling at this stage - whereas my script yesterday can probably be much simpler.

This script should work for any Vue cinema in the UK, just change the feed used to match your cinema (you can see the link for this at the top of the listing for your cinema).

I want to have a similar script for the Odeon cinema chain, then combine the two results - but I can't find an nice source of the data.

I grab the RSS feed, find the film title, find the times, throw away everything else and presto. Essentially, I find each element I want, and add a newline and SAVEME to the front - then I throw out everything that doesn't have SAVEME - and remove that tag. Then I put everything on one line, and insert two new lines before each film, and put a new line before the times. I also tidy up some text, as there were some stray spaces in the certificate information.

(Nb, wordpress tries to tidy text up for me, and translates text - often useful but a bit of a pain with code - I've tried to ensure I've fixed this and for this post have installed syntax highlighter evolved - if I like it, I'll roll it back to earlier posts - but don't hold your breath as that'll take time!)

[sourcecode language="bash"] #!/bin/sh

cd ~/geektool/

# break out the title from the text and fix the classification which had a big space in it - then break out the times - discard everything else - using the 'saveme' tag I added # then use tr to put everything on one line, and use title to put each film on it's own line.

curl --silent http://rss.myvue.net/getfeed.aspx?cinema=77 | sed 's/<title>/\ \ SAVEME<title>/g' | sed 's/<\/title>/<\/title>\ \ /g' | sed 's/<pubDate>[[:print:]]*<\/pubDate>//g' | sed 's/[[:space:]]*)/)/g' | sed 's/\([0-9][0-9]:[0-9][0-9]\)/\ SAVEME\1, \ /g' | grep 'SAVEME' | sed 's/SAVEME//g' | tr -d '\n\r' | sed 's/<title>/\ \ /g' | sed 's/<\/title>/\ /g' > .cinemaworking

cat .cinemaworking [/sourcecode]

Geektool runs the script every 90 minutes to update the desktop.

The script ran a few minutes ago - and this is the result:

Aliens in the Attic (PG)
11:00,
Alvin and the Chipmunks: The Squeakquel (U)
13:00, 15:45, 18:15,
Avatar 2D (12A)
11:30,
Avatar 3D (12A)
12:30, 13:30, 16:00, 17:00, 19:30, 20:30, 23:50,
Daybreakers (15)
13:45, 16:10, 18:30, 21:15, 23:00, 23:50,
Did You Hear About the Morgans? (PG)
14:35, 16:50, 19:40, 22:50,
It's Complicated (15)
12:30, 15:15, 18:00, 21:00, 23:40,
Nine (12A)
20:00, 22:35,
Sherlock Holmes (12A)
13:15, 16:30, 19:50, 20:45, 22:40, 23:30,
St. Trinian's 2 - The Legend of Fritton's Gold (PG)
12:40, 15:10, 17:30,
The Road (15)
12:10, 15:00, 17:45, 20:15, 23:00,

What am I doing here? Take the section that reads

[sourcecode language="bash"] sed 's/<title>/\ \ /g' [/sourcecode]

This is actually one commend. Sed is the 'Stream EDitor' - and I am Substituting text, hence the s/ - and doing so everywhere, hence the /g

I am finding </title> and replacing it with a newline. The \ tells sed that what follows is not something it must understand, but it something it must substitute, and what follows is a newline. I put in two new lines.

The sed command looks like this:

[sourcecode language="bash"] sed 's/thing to change/thing to change it into/g' [/sourcecode]

The sed command can use regular expressions. These are extremely powerful (and tricksy) pattern recognition tools.

[sourcecode language="bash"] sed 's/\([0-9][0-9]:[0-9][0-9]\)/\ SAVEME\1, \ /g' [/sourcecode]

This command finds times, anything that is number number : number number, and adds a newline in front of them. The \( and \) ask sed to remember the time found and the \1 tells sed to use the first thing it remembered when doing the substitution.

The sed commands above won't do anything on their own - this is the job of |

That symbol 'pipes' the output of one command to the input of the next. So the output of curl is piped into sed, which is piped into .... and so on, until the result is written out to a file with >

Note: This script has been expanded to include the Odeon chain of cinemas.