Create a TV Watch List





Create a TV Watch List

Combine Yahoo! TV with Yahoo! Calendar to build a custom TV schedule.

You can find lots of information about upcoming television shows at Yahoo! TV [Hack #44]. By clicking on a program title in your personalized listings, you can find a brief synopsis of the show, members of the cast, and even directors and producers. And, of course, Yahoo! lets you search this information. Browse to Yahoo! TV (http://tv.yahoo.com) and you'll see the search form labeled "Search listings by Keyword."

Imagine you're a Tom Hanks fan and you'd like to find out when a show or movie he's been in will be on. Type "Tom Hanks" into the form, and click Go!. Note that the quotes are important when you're looking for a full phrase. You'll see a list of shows that Tom Hanks appears in within the next 14 days, as shown in Figure.

Yahoo! TV listings search results for "Tom Hanks"


Who knew he was in old episodes of Happy Days? Click a title in the results and you'll find the detail page for the program. Alongside the channel and air time, you'll see an "Add to My Calendar" link. This link lets you add the show as an event on your Yahoo! Calendar so that you can track the shows you'd like to watch. And once a show is an event on your calendar, you'll receive reminders about the event in your email, which can help you remember to catch the show when it airs.

You could run the search for Tom Hanks yourself every couple of weeks at Yahoo! TV and add any programs that look interesting. But this hack shows you how to be lazy, letting a script do the work for you. In fact, this hack will run a Yahoo! TV search for any keyword and add every result to your Yahoo! Calendar. Then you can visit your Yahoo! Calendar periodically to see what Yahoo! has suggested that you view based on your favorite keyword.

The Code

This hack relies on the web automation module WWW::Mechanize (called Mech) and the WWW::Yahoo::Login module. This code logs in to Yahoo! TV with your Yahoo! ID and password, searches for the provided keyword, and adds all of the results to your Yahoo! Calendar. To get started, add the following code to a file called tv_watchlist.pl and include your Yahoo! ID and password in the code:

	#!/usr/bin/perl
	# tv_watchlist.pl
	# A script to gather programs from a Yahoo! TV search
	# and add them to a Yahoo! Calendar
	# Usage: tv_watchlist.pl <query>

	use strict;
	use WWW::Yahoo::Login qw( login logout );
	use WWW::Mechanize;

	# Set your account info
	my $user = "insert Yahoo! ID";
	my $pass = "insert Yahoo! Password";
	my $count = 0;

	# Grab the incoming query
	my $query = join(' ', @ARGV) or die "Usage: tv_watchlist.pl <query>\n";

	# Set login URL
	my $url = 'http://e.my.yahoo.com/config/login?';
	   $url .= '.intl=us&.src=tv&.done=http%3a//tv.yahoo.com/';
	   $url .= 'grid%3f.force=p%26setlineupcookie=true';

	# Log into Yahoo! TV
	my $mech = WWW::Mechanize->new();
	my $login = login(
		mech => $mech,
		uri  => $url,

		user => $user,
		pass => $pass,
	);

	# If login succeeded, add each program to calendar
	if ($login) {
		my $form = $mech->form_number(3);
		$mech->field("title", "\"$query\"");
		$mech->click();
		my $response = $mech->response()->content;
		while ($response =~ m!<a href="(http://tv.*?tvpdb.*?)">.*?</a>!gis) {
			my $showlink = $1;
			$showlink =~ s!\n!!gs;
			$mech->get( $showlink );
			$mech->follow_link( url_regex => qr/calendar/ );
			$mech->follow_link( text_regex => qr/Add to/ );
			my $result = $mech->response()->content;
			if ($result =~ m!<div class="alertbox">(.*?)</div>!gis) {
				$count++;
				my $msg = $1;
				$msg =~ s!<.*?>!!gs;
				$msg =~ s!\n!!gs;
				print "$msg\n";
			}
		}
	} else {
			warn $WWW::Yahoo::Login::ERROR;
	}

	if ($count == 0) {
		print "No shows added.";
	}

Because adding shows from Yahoo! TV search results to a Yahoo! Calendar can be accomplished by clicking a predictable series of links, the Mech function follow_link does most of the work in this script. A simple regular expression for either the URL or text of a link on the page tells Mech which links to follow.

Running the Hack

To run the hack, call it from the command line and pass in a search term. Remember to enclose complete phrases in quotes, like this:

	perl tv_watchlist.pl "Tom Hanks"

As the script runs, it will print a message with any shows added to your calendar, orif no shows were foundthe message "No shows added." You can pipe the output of the script to another file if you'd like to keep a log of the script's activities. Simply call the script, like so:

	perl tv_watchlist.pl "Tom Hanks" > tv_watchlist.log

Figure shows a Yahoo! Calendar with Tom Hanks shows automatically added. Each listing includes the time of the show and the channel it's on. You can even hover over a program title to see a brief synopsis of the show. And, of course, you can click the title to alter or delete the event.

TV shows with Tom Hanks on a Yahoo! Calendar


If you set the script to run every 14 days in Windows Scheduler or as a cron job, you can simply visit your Yahoo! Calendar to keep up with the watch list, or let the Yahoo! Calendar autoreminders send email messages that include program times.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows