thedoedoeblog

Musings of a small game development team

Scraping iTunes: Touch of Generalization

Written by Bill Soistmann on March 23, 2009 at 7:06 pm

At this point our script (if you haven’t been following along) gives us the xhtml for five images which we can include in our webpage. This works nicely but requires that we name and store our images a certain way. What if we want to use different image names?

In a “real world” situation it might be just as easy to rename the script, modify the images and path names; but we can generalize this a bit in just a few minutes. Follow along below and you should be able to take either approach.

First, initialize a few more variables

my $star;
my $empty_star;
my $half_star;
my $image_path;
my $ext;

then add a few more lines to our splitVars function to see if options are passed in query string

$star=($data{'star'})?$data{'star'}:'star_on';
$empty_star=$data{'empty_star'}?$data{'empty_star'}:'star';
$half_star=$data{'half_star'}?$data{'half_star'}:'star_half';
$image_path=$data{'image_path'}?$data{'image_path'}:'images';
$ext=$data{'ext'}?$data{'ext'}:'gif';

Finally, edit the printHTML function. Find these four lines

    $thisimg='star_on';
    $thisimg='star_half';
    $thisimg='star';
	print "<img src=\"images/$thisimg.gif\" alt=\"$t Star\" />\n";

and change them to this

    $thisimg=$star;
    $thisimg=$half_star;
    $thisimg=$empty_star;
	print "<img src=\"$image_path/$thisimg.$ext\" alt=\"$t Star\" />\n";

Now you can pass in the following options via the query string

  • image_path - directory containing your images
  • ext - extension for images
  • star - basename for full star
  • half_star - basename for half star
  • empty_star - basename for empty star (no star)

Grab the whole script here or give it a try at http://bsoist.freeshell.org/itunes/getstars3.cgi?id=304570595&html=true&ext=png&image_path=my_images&star=full_star&half_star=half_star&empty_star=empty_star

This is part of a series of posts which start here. The next post is here.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.