Hack 94 Generate a Custom Gallery

figs/expert.giffigs/hack94.gif

Insert a list of your running auctions, with photos, into your auction descriptions.

One of the ways to advertise your other auctions in any given auction description is to use a gallery ? a scrolling viewport with thumbnail photos of all your running auctions ? as described in [Hack #47]. Aside from the commercial versions, there's a way to create a custom gallery using the eBay API.

First, include this code in your description:

<iframe src="http://www.ebayhacks.com/cgi-bin/gallery.pl" 
                             style="width:100%;margin:0;border:0;">

The URL referenced in this iframe (see [Hack #51]) should point to your own server and cgi-bin directory. See the Using CGI Scripts sidebar if you're not sure how to do this.

Using CGI Scripts

Every web server has a dedicated folder (directory) in which scripts or programs are placed. Instead of displaying the contents of the scripts, as would happen with ordinary HTML files, the web server software runs the scripts and displays their output instead. This special script directory is typically called cgi-bin, wherein CGI stands for "common gateway interface." Refer to your web server software's documentation for details on setting up a cgi-bin directory, or contact your administrator (ISP) if you're renting space on someone else's web server.

Now, copying the Perl scripts discussed throughout this book into your cgi-bin directory is easy enough; the hard part can be making them work.

Provided that the scripts are in the correct location, all you need to do on Unix systems is to make them executable, like this:

chmod +x gallery.pl

where gallery.pl is the filename of the script.

On Windows systems, each Perl script must have a filename that ends with .pl. Then, you'll need to associate the .pl filename extension with your Perl interpreter. In most cases, this will be done for you when you install Perl.

The last step is to reference the script with the proper URL, like this:

http://www.ebayhacks.com/cgi-bin/gallery.pl

where www.ebayhacks.com is the address of your server, cgi-bin is the public name of your cgi-bin directory (not always the same as the private name), and gallery.pl is the filename of your script. If all goes well, you should see the output of the script in your web browser. (It's important to note that not all Perl scripts in this book are CGI scripts.)

If you get an error when you try to run the script from your browser, try running it from the command line instead to see more-detailed error messages.

Here's the script that generates a simple gallery:

#!/usr/bin/perl
require 'ebay.pl';

print "Content-type: text/html\n\n";     [1]
print "<html><script type=\"text/javascript\">\n";
print "function resizeIframe() {\n";
print "parent.document.getElementById('myIframe').height=";
print "document.getElementById('myContent').scrollHeight;\n";
print "}</script></head>\n";
print "<body style=\"margin:0;border:none\"\n";
print "onLoad=\"resizeIframe()\" onResize=\"resizeIframe()\">\n";
print "<div id=\"myContent\">\n";

$today = &formatdate(time);
$tendays = &formatdate(time + 864000);

my $page_number = 1;
PAGE:
while (1) {
    my $rsp = call_api({ Verb => 'GetSellerList',
                  DetailLevel => 32,
                       UserId => $user_id,
                  EndTimeFrom => $today,
                    EndTimeTo => $tendays,
                   PageNumber => $page_number
    });

    if ($rsp->{Errors}) { last PAGE; }
    foreach (@{$rsp->{SellerList}{Item}}) {
      my %i = %$_;
      ($id, $title, $gallery) = @i{qw/Id Title GalleryURL/};

      $gallery = undef if ref $gallery;     [2]
      if (defined $gallery) {
        print "<table width=96 align=left><tr><td align=center>";
        print "<a href=\"$itemurl$id\" target=\"_blank\">";
        print "<img src=\"$gallery\" width=96 height=96></a>";
        print "<br><font size=-2>$title</font></td></tr></table>\n";
      }
    }
    SKIP:
    last PAGE unless $rsp->{SellerList}{HasMoreItems};
    $page_number++;
}
print "</div></body></html>\n";     [3]

Note that only auctions for which you've added the Gallery listing upgrade and specified a gallery image (as described in [Hack #36]) will appear, as shown in Figure 8-1.

Figure 8-1. The gallery.pl script will display a scrolling list of all your current items
figs/ebh_0801.gif

If you're not using the Gallery upgrade, you can specify your own images. Simply replace line [2] with this:

      $gallery = "http://www.ebayhacks.com/images/$id.jpg";

where http://www.ebayhacks.com is the address of your server and /images is the directory containing your personal gallery images (in JPG format). Name the images after your auction numbers (e.g., 4500205199.jpg), and you're good to go!

This gallery script will be run and the GetSellerList API call will be used every time the auction page is loaded, which is probably much more often than necessary. This is an example of when it makes sense to [Hack #99].