June 27, 2008, 9:01 p.m.
posted by neverloop
View Movie Lists on Your Cell Phone
![]()
Next time you go to the video store, take a Yahoo! Movies list with you on your cell phone.
Imagine you find yourself in a video store, searching for the perfect movie among the thousands of choices and drawing a blank. Even though you might have a mental list of several movies you've been meaning to see, it always seems those crucial bits of information aren't available when you need them. If you have a cell phone with web access, Yahoo! Movies and some Perl scripting can get you out of this jam.
This hack takes advantage of the Lists feature at Yahoo! Movies [Hack #41], which is designed to put movies together into a group. By creating a list of movies you'd like to see, you'll have the movies in a convenient format for scripting. From there, the hack uses some Perl to convert the Yahoo! Movies list into Wireless Markup Language (WML) that you can view on your phone.
Creating Your List
To get started, you need to turn your mental list of movies you'd like to see into something more tangible. Browse to http://movies.yahoo.com/profiles and click Create New List. If you don't already have a Yahoo! ID [Hack #3], you'll need to create one in order to use the movie list feature. Choose "Movies I Want to See" from the suggestions list or create your own title. Now you'll have a blank list waiting for some entries.
At the top of the page, you should see a form titled Search Yahoo! Movies. Type in the name of a movie you'd like to see. If you don't have anything in mind, try typing Buckaroo Banzai. Click Search and you should see a result with the 1984 cult classic The Adventures of Buckaroo Banzai. Click the "Add to My Movies" link next to the title, and you'll see a list of your movie lists. Click the checkbox next to the list you just created and then click "Add to List." The movie will be on your list, and you can repeat the process as many times as you need to until your movie list is filled with films you'd like to see, as shown in Figure.
Once your movie list is ready to go, you just need to jot down its URL. Head back to your profile page by clicking the My Movies tab or browsing to http://movies.yahoo.com/profiles.Click the title of your movie list and copy the URL from your browser's address bar. The URL should look something like this:
http://movies.yahoo.com/mvc/dls?iid=7-2022263&lid=7-150274
With the movie list URL in hand, you're ready to build the script that will convert that list into a cell phonefriendly format.
A Yahoo! Movies list
The Code
This script needs to run on a publicly available web server that can execute Perl scripts. You'll need the nonstandard Perl module LWP::Simple, which will fetch the movie list from Yahoo! Movies. You'll also need HTML::TableExtract, which will do the tough work of deconstructing the HTML for you.
This script relies on screen scraping to gather the movies in a list, which means it's picking through the HTML to find relevant information. This also means that if Yahoo! changes their movie list HTML, even slightly, this script will likely fail. Keep in mind that you might need to tinker with the script to keep up with changes to Yahoo! Movies.
To keep your fingers from doing too much work when you're ready to bring it up on your phone, you'll want to keep the name of this script short. Save the following code to a file called m.cgi and be sure to include your unique movie list URL as the value of $listURL at the top of the script:
#!/usr/bin/perl # m.pl # Convert a Yahoo! Movies list into WML for cell phones # Usage: m.cgi use strict; use HTML::TableExtract; use LWP::Simple; # Set your Yahoo! Movie list URL my $listURL = "insert your movie list URL"; # Set the base movie URL my $movieURL = "http://acid1.oa.yahoo.com/mbl/mov/mdet?mid="; # Set the titles of the Yahoo! Movies table you're parsing. note # that if the title contains HTML, so too must these headers. my @tehs = ["#", "Movie Title", "User<br>Grade", "Avg. User<br>Grade", "Critics<br>Grade","Status"]; my $te = HTML::TableExtract->new(headers=>@tehs, keep_html=>1); # Fetch the HTML my $content = get($listURL); my ($wml,@moviedata); # Parse the table that matches the headers above. $te->parse($content); foreach my $ts ($te->table_states) { foreach my $r ($ts->rows) { next if @$r[0] =~ /grayText/; # final table footer. my ($title, $mid); # parse ID and title from "Movie Title" field. if (@$r[1] =~ m!.*?id=(.*?)"><b>(.*?)</b>.*?!gis) { $mid = $1; $title = $2; } my $thisMovie = { title => $title, mid => $mid, grade => &clean_text(@$r[2]), avg => &clean_text(@$r[3]), critics => &clean_text(@$r[4]), status => &clean_text(@$r[5]), }; push @moviedata, $thisMovie; } } # Assemble the WML by looping through the array of hashes for my $i ( 0 .. scalar(@moviedata)-1) { $wml .= "<anchor>$moviedata[$i]{title} "; $wml .= "<go href=\"$movieURL$moviedata[$i]{mid}\"/>"; $wml .= "</anchor><br />\n"; $wml .= "<b>Status:</b> $moviedata[$i]{status}<br />\n"; $wml .= "<b>Critics:</b> $moviedata[$i]{critics}<br />\n"; $wml .= "<b>Users:</b> $moviedata[$i]{avg}\n"; $wml .= "<br /><br />\n"; } # Send final WML to the client print "Content-Type: text/vnd.wap.wml\n\n"; print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""; print "\"http://www.wapforum.org/DTD/wml_1.1.xml\">\n"; print "<wml><card id=\"Menu\" title=\"Movie Wishlist\">\n"; print "<p><b>Movie Wishlist</b><br/><br />\n"; print $wml; print "</p></card></wml>\n"; # This function removes HTML, space entities, # linebreaks, and leading/trailing spaces from strings sub clean_text() { my $text = shift(@_); $text =~ s!<.*?>!!g; $text =~ s! !!g; $text =~ s!\n!!g; $text =~ s!^\s+!!; $text =~ s!\s+$!!; $text =~ s!\s{16}!, !; return $text; }
This script downloads the HTML from the URL you supply and picks relevant information from the HTML. When the script runs into a movie title, it also grabs the internal Yahoo! ID for that movie. Then, using the $movieURL as a base, the script assembles a link to that movie's detail page at Yahoo! Mobile. This means that if you're ever browsing your list on your phone and can't quite remember what that particular movie is about, you can simply click through to Yahoo!'s mobile site to get a summary of the movie.
In addition to the titles in the list, the script includes whether the movie is in theaters or on DVD, the critics' grade, and the average grade assigned by Yahoo! users.
Notice that at the end of the script, when it's printing out the WML, the content type is set as text/vnd.wap.wml. Setting this content type ensures that the device viewing the page will know how to render it. Web browsers won't be able to view the page, so you can either test it exclusively on your cell phone, or temporarily change the content type to text/xml in order to test it in a web browser.
Running the Hack
Upload m.cgi to a publicly available web server and bring it up in your cell phone's browser as you would any URL:
http://example.com/m.cgi
You'll have to key in the URL by hand on your phone. But most phone browsers can set bookmarks, so you can add this to your favorite mobile sites for one-click access in the future. On your phone, you should see your movie list, as shown in Figure.
A Yahoo! Movies list on a cell phone
Of course, you'll have to do the work of keeping your to-see list up-to-date. You'll need to revisit your list frequently to add movies you'd like to see or remove those you've seen. With an active list and your cell phone in your pocket, you'll never be faced with drawing a blank as you browse movies!
- Comment