Sept. 27, 2008, 3:30 p.m.
posted by neverloop
Subscribe to Movie Showtimes
![]()
With some quick scripting, subscribe to your favorite theaters to keep up with the movies they're playing.
Even though there are a number of ways to keep up with ever-changing movie schedules at Yahoo! Movies [Hack #41], if you already use an RSS newsreader, you can start watching your favorite theaters with just a bit of scripting.
RSS is an XML syndication format that's great for keeping up with changing informationtypically news storiesand Yahoo! offers RSS for many features of its site. You can add any data that's available as RSS to a newsreader and see any changes or additions on a regular basis. Unfortunately, at the time of this writing, Yahoo! doesn't provide movie schedules via RSS, so if you want to see showtimes in your newsreader, you'll need to build a feed yourself.
Finding Your Theaters
Yahoo! doesn't offer movie schedules through their web services API, so this hack relies on screen scraping to gather the information. Screen scraping refers to downloading the HTML on the web site with a script and processing it to find relevant pieces of information. Keep in mind that screen scraping is a very brittle process, and if Yahoo! changes the HTML used to display movie times even slightly, the code in this hack will have to be modified to keep up with the changes.
To download the schedule page, you first need to know how to get there. Each theater listed at Yahoo! Movies has a unique internal ID number. But it's fairly easy to find this number, because it's exposed in the URL. Simply browse to Yahoo! Movies (http://movies.yahoo.com) and enter your Zip Code into the form labeled Get Showtimes and Tickets.
You should see a list of theaters in your area, along with movie showtimes. Under each theater title is link for Theater Info. Click the link and note the URL in your browser address bar, which will include the theater ID. As you'd expect, the four-to five-digit number following the variable named id in the URL is the theater ID for that theater. Jot down the IDs of your favorite theaters, because you'll need them to generate RSS feeds later.
Once you know the internal ID of a specific theater, you can link directly to the theater detail page on Yahoo! Movies with the following URL format:
http://movies.yahoo.com/showtimes/theater?id=insert theater ID
The theater detail page contains the theater's address and phone number, a list of services available, and the current schedule of movies. For this hack, the only relevant pieces of data are the theater title and the list of movies. Some Perl can isolate those elements and turn them into an RSS feed you can subscribe to.
The Code
This code relies on a single Perl module: XML::RSS::SimpleGen by Sean Burke. This module makes it easy to create an RSS feed with screen scraping, and it keeps the tough work of formatting the feed properly in the background.
Save the following code to a file called theater_rss.pl:
#!/usr/bin/perl
# theater_rss.pl
# Accepts a Yahoo! Movies theater ID and prints
# an RSS feed of currently playing movies.
# Usage: theater_rss.pl <theater_ID>
#
# You can find theater IDs at Yahoo! Movies
# at http://movies.yahoo.com/
use strict;
use XML::RSS::SimpleGen;
# Grab the incoming theater ID
my $tid = join(' ', @ARGV) or die "Usage: theater_rss.pl <theater_ID>\n";
my $theater_title = "My favorite theater";
# Set the theater schedule URL
my $url = "http://acid1.oa.yahoo.com/mbl/mov/tdet?tid=$tid";
# Download the schedule page
my $content = get_url($url);
# Find the theater name
if ($content =~ m!<dl><dt>(.*?)</dt>!sg) {
$theater_title = $1;
}
# Start the RSS Feed
rss_new($url, "$theater_title Schedule");
rss_language('en');
rss_webmaster('insert your email address');
rss_daily();
# Set the regular expression to find data
my $regex = '<table.*?>.*?mid=(.*?)">(.*?)</a></td>.*?';
$regex .= '<td>(.*?)</td>.*?</table>';
# Loop through the HTML, grabbing elements
while ($content =~ m!$regex!sg) {
# rss_item accepts url, title, description.
my $url = "http://movies.yahoo.com/shop?d=hv&cf=info&id=$1";
rss_item($url, $2, $3);
}
# Warn if nothing was found
die "No items in this content?! {{\n$_\n}}\nAborting"
unless rss_item_count();
# Save the rss file as <theater_ID>.rss
rss_save("$tid.rss");
exit;
This code accepts a theater ID, builds the appropriate Yahoo! Movies URL, downloads the HTML, and picks through the HTML with some regular expressions to find theater and movie information. The code uses the functions that are a part of XML::RSS::SimpleGen to create and save an RSS file based on the movie information.
The name of the RSS file this script generates is based on the incoming ID and will be theater_ID .rss. If you'd rather save the file to another location, just append the path to the filename where the script calls the rss_save function. Be sure that the file this script creates is in a location that's accessible via the Web.
Running the Hack
You can run the script once by passing in a theater ID on the command line, like this:
perl theater_rss.pl insert theater ID
But the real value of the script is that you can run it on a regular schedule on a web server to keep up with changes to the theater's schedule. Once per day should be enough to keep up with changes, and you can set the script to run regularly with Windows Scheduler or the Unix cron command.
On Windows servers, you can find the scheduler at Start
Settings
Control Panel
Scheduled Tasks. Click Add Scheduled Task at the top of the list to start the task wizard and set the program to run like this:
C:\perl\bin\perl.exe "C:\path\to\theater_rss.pl insert theater ID"
You might need to adjust the location of the Perl executable, depending on where it's installed on your server. You'll also need to include the full path to theater_rss.pl.
On Unix-based systems, you can run the script once per day by adding an entry like the following to your crontab file:
52 23 * * * ~/theater_rss.pl insert theater ID
If you want to subscribe to more than one theater in your area, set up a separate recurring task for each theater, using its unique theater ID.
Finally, add the new RSS feed to your favorite newsreader and you'll find out about any new movies playing at that theater. Figure shows a subscription to theater 8193Carmike Cinema 12 in Corvallis, Oregonin the latest version of the Safari browser (which doubles nicely as an RSS newsreader).
A theater schedule RSS feed in Safari
Clicking "Read more…"or the movie title in some newsreaderswill take you to the movie detail page at Yahoo! Movies, where you can find out more about that particular movie. And by subscribing to your favorite local theaters' schedules, you'll always be on top of new additions to their lineups.
- Comment