Hack 85 Automatically Keep Track of Auctions You've Won


Maintain a permanent record of everything you've ever purchased.

Since eBay keeps auctions on site only for about 90 days and lists them in My eBay for only 30 days, all bidders should maintain permanent, off-site records of the items they've purchased.

As long as you keep all email you've received, as described in the Preface of this book, you'll always have records of the item numbers, titles, seller IDs and email addresses, and closing prices of the items you've won. But this data is stored in a less-than-convenient format, and the descriptions aren't stored at all.

Here's a script that will automatically retrieve and store details for every auction you've won:

require 'ebay.pl';

$today = &formatdate(time);     [1]
$yesterday = &formatdate(time - 86400);     [2]

my $rsp = call_api({ Verb => 'GetBidderList',     [3]
              DetailLevel => 32,
                   UserId => $user_id,
                   SiteId => $site_id,
              EndTimeFrom => $yesterday,
                EndTimeTo => $today,

if ($rsp->{Errors}) {
} else {
  foreach (@{$rsp->{BidderList}{Item}}) {
    my %i = %$_;
    ($highbidder, $title, $id) = @i{qw/HighBidderUserId Title Id/};     [4]

    if ((! -e "$localdir/$id") && ($highbidder eq $user_id)) {     [5]
      my $rsp = call_api({ Verb => 'GetItem',     [6]
                    DetailLevel => 2,
                             Id => $id

      if ($rsp->{Errors}) {
      } else {
        my %i = %{$rsp->{Item}[0]};
        my ($price, $currency, $seller, $title, $description) =     [7]
                 @i{qw/CurrentPrice CurrencyId Seller Title Description/};
        open (OUTFILE,">$localdir/$id");     [8]
          print OUTFILE "[$id]\n";
          print OUTFILE "title=$title\n";
          print OUTFILE "seller=".$seller->{User}{UserId}."\n";
          print OUTFILE "price=$currency$price\n";
          print OUTFILE "description=$description\n";
        close (OUTFILE);

The GetBidderList API verb, called on line [3], returns a maximum of 200 items, but doesn't support paging like GetSearchResults (see [Hack #83]). This means that we need to be a little creative. So instead of trying to grab as many listings as possible, we grab only the auctions that have ended in a certain interval, say, a day. (If you bid infrequently, you can change this to a week or even a month, and help keep down your API calls). So, we set the EndTimeTo and EndTimeFrom fields to today's and yesterday's dates, respectively. The formatdate subroutine is used on lines [1] and [2] to convert the Perl dates to something eBay understands.

Next, the script runs through the list of auctions you've bid on and retrieves [4] the high bidder, title, and item number for each. (We don't actually use the title here, but it's nice to have.)

On line [5], a check is performed to see if the auction has been recorded previously and if you're indeed the high bidder. Note that since we're using a DetailLevel of 32 (specified on line [3]), we get back an "abbreviated results set" from which, as the documentation explains, the user ID of the high bidder is retrieved with the HighBidderUserId tag. We could also use a DetailLevel of 0, but we'd have to deal with the HighBidder hash (see [Hack #84] for more information on container nodes). Refer to the API documentation for the somewhat confusing circumstances surrounding the DetailLevel tag with regards to GetBidderList.

Finally, the auction details for each item are retrieved with GetItem (line [6] and then line [7]). Note that the DetailLevel for this call is set to 2 to retrieve the descriptions as well, as described in [Hack #84].

The $description variable contains the raw description, including any HTML code. See [Hack #90] for details on extracting plain text from HTML code.

Line [8] then saves the retrieved data into a file, named for the item number of the listing. Naturally, you can retrieve and store as many or as few fields as you like and store them in whatever format is convenient, such as a database, comma-delimited (CSV) text file, or the generic format used here.

See [Hack #17] for information on scheduling the script to run at regular intervals. Since the script retrieves only auctions that have ended in the last 24 hours, this script should be run once a day, every day. If you want to run it less (or more) often, just change the EndTimeTo and EndTimeFrom fields accordingly.