#!/usr/bin/perl # $Id: ebayparse,v 1.2 2002/05/28 18:17:58 felicity Stab $ $|++; use strict; use Mail::Internet; use Getopt::Long; require 5.005; my $sep = ","; sub HandleMessage { my(@msg) = @_; my $mail = new Mail::Internet( \@msg ); my $head = $mail->head(); chomp(my $from = $head->get("From")); chomp(my $subject = $head->get("Subject")); return unless ( $from =~ /^endofauction\@ebay.com$/i ); my $body = $mail->body(); my(@parstarts) = ( 0 ); for(my $i=1, my $flag = 0; $i<@{$body}; $i++) { if ( $body->[$i] !~ /\S/ ) { $flag = 1; next; } if ( $flag ) { $flag = 0; push(@parstarts, $i); } } # first: Congratulations! This eBay item has successfully ended. # your item has ended without a winning bidder if ( $body->[0] =~ /item has successfully ended/ ) { # take second paragraph my $second = join("",@{$body}[ $parstarts[1] .. ($parstarts[2]-1) ]); my($date) = ( $second =~ /^End date:\s+(.+?)\s*$/mi ); my($name) = ( $second =~ /^Item name:\s+(.+?)\s*$/mi ); my($number) = ( $second =~ /^Item number:\s+(.+?)\s*$/mi ); my($price) = ( $second =~ /^Final price:\s+(.+?)\s*$/mi ); # if second doesn't have /High-bidder User ID:/, check third for dutch auction my($bidderu) = ( $second =~ /^High-bidder User Id:\s+(.+?)\s*$/mi ); my($biddere) = ( $second =~ /^High-bidder email:\s+(.+?)\s*$/mi ); if ( defined $bidderu ) { # single winner print join($sep,map { /,/ ? qq/"$_"/ : $_ } ($date,$name,$number,$price,1,"$biddere ($bidderu)","","","")),"\n"; } else { # dutch auction # take fourth my $fourth = join("",@{$body}[ ($parstarts[3]+1) .. ($parstarts[4]-1) ]); $fourth =~ s/[\s\n]+$/\n/; foreach ( split(/\n/, $fourth) ) { /^(\S+)\s+(\S+)\s+\d+\s+(\d+)/; print join($sep,map { /,/ ? qq/"$_"/ : $_ } ($date,$name,$number,$price,$3,"$2 ($1)"),"","",""),"\n" if ( $1 ); } } } elsif ( $body->[0] =~ /your item has ended without a winning bidder/ ) { # take second paragraph my $second = join("",@{$body}[ $parstarts[1] .. ($parstarts[2]-1) ]); my($date) = ( $second =~ /^End date:\s+(.+)/mi ); my($name) = ( $second =~ /^Item name:\s+(.+)/mi ); my($number) = ( $second =~ /^Item number:\s+(.+)/mi ); print join($sep,map { /,/ ? qq/"$_"/ : $_ } ($date,$name,$number,"","","","","","")),"\n"; } else { warn "Unknown body output!, $subject"; return; } } my $debug = 1; # don't actually do anything external ... my @message = (); my $help = 0; # Get the commandline options GetOptions( "help" => \$help, ) || Usage(1); # Print out the usage if neccessary. Usage() if ($help); print join($sep,map { qq/"$_"/ } ("End Date","Item Name","Item Number","Winning Price","Quantity","Winner","Zip Code","Amount Paid","Shipped?")),"\n"; # # Main loop # It reads from STDIN/file and takes messages in Berkeley format (messages # seperated by "^From ", then processes them through the HandleSpam routine. # my $line = undef; do { $line = <>; # New message! if ( !defined $line || $line =~ /^From / ) { HandleMessage( @message ) if (@message); @message = ($line); } else { push ( @message, $line ); } } while ( defined $line ); # Everything's good, exit out now. :) exit 0;