Programming with the eBay API and Perl

I wrote a web application to search and filter daily eBay computer listings using the eBay API, with Perl, XML, CSS, JavaScript and HTML.


eBay API Data Flow

eBay has done a good job with Getting Started info and creating and managing credentials on their eBay Developer site, which only takes 5 minutes to create an account and generate your appid and credentials. An eBay account is not needed for search-only applications.

Although the eBay API works well and has good email support from employees, the documentation is not friendly to new users. For example, finding items by keywords does not by default return all selling info, like seller id.

API programming is made more complicated by legacy eBay data schema limitations, including ambiguity between BuyItNow and FixedPrice listings.

So it took me a few extra minutes of experimentation to get exactly what I wanted, but the result was worth it.


eBay API
Screenshot of final navigation UI, implemented using a button bar with the HTML table and CSS multiple classes technique (see references)

Here is a minimal but non-trivial sample search program using the CPAN eBay::API::Simple module, which is supported by eBay staff.

#!/usr/bin/perl

# Program: ebay_find_test.cgi
# Author: James Briggs (james.briggs -AT- yahoo.com)
# Purpose: demo of the eBay finding API
# Date: 2013 04 16
# Env: Perl5
# Note:

use strict;
use diagnostics;

use eBay::API::Simple::Finding;
use CGI qw / :standard *table /;
use DateTime::Format::ISO8601;

   binmode STDOUT, ':utf8';

   my @keywords = ( 'Dell R710 Quad 4TB' );
   my $app_id = '';
   my $tz = 'America/Los_Angeles';

   print header(), start_html( -title => 'eBay API', );

   my $call = eBay::API::Simple::Finding->new(
    { appid => $app_id }
  );

  for my $keyword (@keywords) { 
     $call->execute( 'findItemsByKeywords', {
         keywords => $keyword,
         paginationInput => { entriesPerPage => 200 },
         outputSelector => [ 'SellerInfo' ],
         sortOrder => 'EndTimeSoonest',
      } );

     if ( $call->has_error() ) {
        die "error: find call failed (check your appid and Internet connectivity):" . $call->errors_as_string();
     }
   
     # getters for the response DOM or Hash
     my $dom  = $call->response_dom();
     my $hash = $call->response_hash();

     print "Keywords: '$keyword', Time: " . $call->nodeContent( 'timestamp' ) . ' # ' .  $call->nodeContent( 'totalEntries' ) . " items\n";
   
     my @nodes = $dom->findnodes(
       '//item'
     );

     print start_table(), "\n";

     for my $n (@nodes) {
       next if $n->findvalue('sellingStatus/sellingState/text()') ne 'Active';
  
       my $dt_start = date_from_utc($n->findvalue('listingInfo/startTime/text()'), $tz);
       my $dt_end   = date_from_utc($n->findvalue('listingInfo/endTime/text()'), $tz);

       print Tr(td([
          $dt_start,
          $dt_end,
          $n->findvalue('itemId/text()'),
          $n->findvalue('sellerInfo/sellerUserName/text()'),
          $n->findvalue('sellerInfo/feedbackScore/text()'),
          $n->findvalue('title/text()'),
          $n->findvalue('sellingStatus/currentPrice/text()'),
          $n->findvalue('listingInfo/buyItNowPrice/text()'),
          $n->findvalue('sellingStatus/bidCount/text()'),
          $n->findvalue('country/text()'),
          $n->findvalue('sellingStatus/timeLeft/text()'),
       ])), "\n";
     }
   }

   print end_table(), "\n", end_html(), "\n";

sub date_from_utc {
   my ($dt, $tz) = @_;

   return '' if not defined $dt or $dt eq '';

   if ($dt ne 'UTC') {
      $dt = DateTime::Format::ISO8601->parse_datetime($dt);
      $dt->set_time_zone($tz);
      $dt = $dt->strftime("%F_%T");
   }

   return $dt;
}

References

developer.ebay.com: eBay Finding API
SO: Show/hide rows based on class in HTML
SO:Changing the class of an element with onclick javascript
wikipedia: eBay API

One thought on “Programming with the eBay API and Perl

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>