| 1 | # Ebay/ByEndDate.pm | ||||||||||||
| 2 | # by Martin Thurn | ||||||||||||
| 3 | |||||||||||||
| 4 | =head1 NAME | ||||||||||||
| 5 | |||||||||||||
| 6 | WWW::Search::Ebay::ByEndDate - backend for searching www.ebay.com, with results sorted with "items ending first" | ||||||||||||
| 7 | |||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||
| 9 | |||||||||||||
| 10 | use WWW::Search; | ||||||||||||
| 11 | my $oSearch = new WWW::Search('Ebay::ByEndDate'); | ||||||||||||
| 12 | my $sQuery = WWW::Search::escape_query("C-10 carded Yakface"); | ||||||||||||
| 13 | $oSearch->native_query($sQuery); | ||||||||||||
| 14 | while (my $oResult = $oSearch->next_result()) | ||||||||||||
| 15 | { print $oResult->url, "\n"; } | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||
| 18 | |||||||||||||
| 19 | This class is a Ebay specialization of WWW::Search. | ||||||||||||
| 20 | It handles making and interpreting Ebay searches | ||||||||||||
| 21 | F<http://www.ebay.com>. | ||||||||||||
| 22 | |||||||||||||
| 23 | This class exports no public interface; all interaction should | ||||||||||||
| 24 | be done through L<WWW::Search> objects. | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 NOTES | ||||||||||||
| 27 | |||||||||||||
| 28 | The calling program must ensure that the Date::Manip module is able to | ||||||||||||
| 29 | determine the local timezone. When Date::Manip changed from version 5 | ||||||||||||
| 30 | to version 6, the timezone handling was completely overhauled. See | ||||||||||||
| 31 | the documentation of Date::Manip but good luck because it is VERY | ||||||||||||
| 32 | confusing and does not contain useful examples. | ||||||||||||
| 33 | |||||||||||||
| 34 | The search is done against CURRENT running auctions only. | ||||||||||||
| 35 | |||||||||||||
| 36 | The query is applied to TITLES only. | ||||||||||||
| 37 | |||||||||||||
| 38 | The results are ordered auctions ending soon first (order of | ||||||||||||
| 39 | increasing auction ending date). | ||||||||||||
| 40 | |||||||||||||
| 41 | In the resulting WWW::Search::Result objects, the description field | ||||||||||||
| 42 | consists of a human-readable combination (joined with semicolon-space) | ||||||||||||
| 43 | of the Item Number; number of bids; and high bid amount (or starting | ||||||||||||
| 44 | bid amount). | ||||||||||||
| 45 | |||||||||||||
| 46 | In the WWW::Search::Result objects, the change_date field contains the | ||||||||||||
| 47 | auction ending date & time in ISO 8601 format; | ||||||||||||
| 48 | i.e. YYYY-MM-DDThh:mm:ss. | ||||||||||||
| 49 | |||||||||||||
| 50 | =head1 SEE ALSO | ||||||||||||
| 51 | |||||||||||||
| 52 | To make new back-ends, see L<WWW::Search>. | ||||||||||||
| 53 | |||||||||||||
| 54 | =head1 CAVEATS | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 BUGS | ||||||||||||
| 57 | |||||||||||||
| 58 | Please tell the author if you find any! | ||||||||||||
| 59 | |||||||||||||
| 60 | =head1 AUTHOR | ||||||||||||
| 61 | |||||||||||||
| 62 | Martin 'Kingpin' Thurn, C<mthurn at cpan.org>, L<http://tinyurl.com/nn67z>. | ||||||||||||
| 63 | |||||||||||||
| 64 | =head1 LEGALESE | ||||||||||||
| 65 | |||||||||||||
| 66 | Copyright (C) 1998-2015 Martin 'Kingpin' Thurn | ||||||||||||
| 67 | |||||||||||||
| 68 | THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||||||||||||
| 69 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||||||||||||
| 70 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||
| 71 | |||||||||||||
| 72 | =cut | ||||||||||||
| 73 | |||||||||||||
| 74 | ##################################################################### | ||||||||||||
| 75 | |||||||||||||
| 76 | package WWW::Search::Ebay::ByEndDate; | ||||||||||||
| 77 | |||||||||||||
| 78 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 1 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 1 | ▶ | use Date::Manip; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 84 | 1 | ▶ | use base 'WWW::Search::Ebay'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | |||||||||||||
| 86 | our | ||||||||||||
| 87 | 1 | $VERSION = 2.341; | |||||||||||
| 88 | 1 | our $MAINTAINER = 'Martin Thurn <mthurn@cpan.org>'; | |||||||||||
| 89 | |||||||||||||
| 90 | 1 | my $EBAY_TZ = 'America/Los_Angeles'; | |||||||||||
| 91 | |||||||||||||
| 92 | # private | ||||||||||||
| 93 | sub _native_setup_search | ||||||||||||
| 94 | 1 | ▶ | { | ||||||||||
|
Subroutine
_native_setup_search: called CC=3 SCAR=11.4
| |||||||||||||
| 95 | 1 | my ($self, $native_query, $rhOptsArg) = @_; | |||||||||||
| 96 | 1 | ▶ | $rhOptsArg ||= {}; | ||||||||||
|
Condition50%1/2
$rhOptsArg ||= {}
MC/DC0%0/1
$rhOptsArg ||= {}
$rhOptsArg
Truth table50%1/2
A
A= $rhOptsArg
| |||||||||||||
| 97 | 1 | ▶ | unless (ref($rhOptsArg) eq 'HASH') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | { | ||||||||||||
| 99 | 0 | carp " --- second argument to _native_setup_search should be hashref, not arrayref"; | |||||||||||
| 100 | 0 | return undef; | |||||||||||
| 101 | } # unless | ||||||||||||
| 102 | # These will become CGI arguments when the HTTP request is made to | ||||||||||||
| 103 | # ebay.com: | ||||||||||||
| 104 | 1 | $rhOptsArg->{'SortProperty'} = 'MetaEndSort'; | |||||||||||
| 105 | 1 | $rhOptsArg->{'_fosrp'} = 1; | |||||||||||
| 106 | 1 | $rhOptsArg->{'_sop'} = 1; | |||||||||||
| 107 | 1 | if (0) | |||||||||||
| 108 | { | ||||||||||||
| 109 | # my @a = sort Date_Init(); | ||||||||||||
| 110 | # print STDERR " III BEFORE: ", Dumper(\@a); | ||||||||||||
| 111 | Date_Init("ConvTZ=$EBAY_TZ"); | ||||||||||||
| 112 | # @a = sort Date_Init(); | ||||||||||||
| 113 | # print STDERR " III AFTER: ", Dumper(\@a); | ||||||||||||
| 114 | # We need to know the time in eBayLand right now: | ||||||||||||
| 115 | my $tz = $Date::Manip::Cnf{TZ}; # UnixDate('today', '%Z'); | ||||||||||||
| 116 | } # if 0 | ||||||||||||
| 117 | # Get the date-time right now, in the local timezone: | ||||||||||||
| 118 | 1 | my $dateToday = ParseDate('today'); | |||||||||||
| 119 | # Date_Init("SetDate=$EBAY_TZ"); | ||||||||||||
| 120 | # Date::Manip::DM6 says it will "default to the local timezone": | ||||||||||||
| 121 | 1 | my $tz = undef; | |||||||||||
| 122 | # print STDERR " DDD convert today ==$dateToday== to tz ==$EBAY_TZ==\n"; | ||||||||||||
| 123 | 1 | $self->{_today_} = Date_ConvTZ($dateToday, $tz, $EBAY_TZ); | |||||||||||
| 124 | # print STDERR " DDD result today == ", $self->{_today_}, "\n"; | ||||||||||||
| 125 | # exit; | ||||||||||||
| 126 | 1 | return $self->SUPER::_native_setup_search($native_query, $rhOptsArg); | |||||||||||
| 127 | } # _native_setup_search | ||||||||||||
| 128 | |||||||||||||
| 129 | # Enforce sorting by end date, even if Ebay is returning it in a | ||||||||||||
| 130 | # different order. (They will be out of order if there are "Featured | ||||||||||||
| 131 | # Items" at the top of the page.) Calls _parse_tree() of the base | ||||||||||||
| 132 | # class, and then reshuffles its 'cache' results. Code contributed by | ||||||||||||
| 133 | # Mike Schilli. | ||||||||||||
| 134 | |||||||||||||
| 135 | sub _parse_tree | ||||||||||||
| 136 | 0 | ▶ | { | ||||||||||
|
Subroutine
_parse_tree: not called CC=3 SCAR=24.8
| |||||||||||||
| 137 | 0 | my ($self, @args) = @_; | |||||||||||
| 138 | 0 | my $hits = $self->SUPER::_parse_tree(@args); | |||||||||||
| 139 | 0 | ▶ | $self->{cache} ||= []; | ||||||||||
|
Condition0%0/2
$self->{"cache"} ||= []
MC/DC0%0/1
$self->{"cache"} ||= []
$self->{"cache"}
Truth table0%0/2
A
A= $self->{"cache"}
| |||||||||||||
| 140 | 0 | if (0) | |||||||||||
| 141 | { | ||||||||||||
| 142 | # Convert all eBay relative times to absolute times: | ||||||||||||
| 143 | $self->{cache} = [ | ||||||||||||
| 144 | map { | ||||||||||||
| 145 | my $iMin = _minutes($_->change_date) || _minutes(_date_to_rel($_->change_date, | ||||||||||||
| 146 | $self->{_today_})); | ||||||||||||
| 147 | $_->change_date(UnixDate(DateCalc($self->{_today_}, " + $iMin minutes"), | ||||||||||||
| 148 | '%Y-%m-%dT%H:%M:%S')); | ||||||||||||
| 149 | $_ | ||||||||||||
| 150 | } | ||||||||||||
| 151 | grep { ref } | ||||||||||||
| 152 | @{$self->{cache}} | ||||||||||||
| 153 | ]; | ||||||||||||
| 154 | } # if | ||||||||||||
| 155 | # Sort by date using a Schwartzian transform to save memory: | ||||||||||||
| 156 | $self->{cache} = [ | ||||||||||||
| 157 | 0 | map { $_->[0] } | |||||||||||
| 158 | 0 | sort { $a->[1] cmp $b->[1] } | |||||||||||
| 159 | 0 | ▶ | map { [ $_, $_->change_date || 0 ] } | ||||||||||
|
Condition0%0/2
$_->change_date || 0
MC/DC0%0/1
$_->change_date || 0
$_->change_date
Truth table0%0/2
A
A= $_->change_date
| |||||||||||||
| 160 | 0 | @{$self->{cache}} | |||||||||||
| 161 | ]; | ||||||||||||
| 162 | 0 | return $hits; | |||||||||||
| 163 | } # _parse_tree | ||||||||||||
| 164 | |||||||||||||
| 165 | 1 | ▶ | use constant DEBUG_MINUTES => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 166 | |||||||||||||
| 167 | sub _minutes | ||||||||||||
| 168 | 0 | ▶ | { | ||||||||||
|
Subroutine
_minutes: not called CC=4 SCAR=30.0
| |||||||||||||
| 169 | 0 | my $s = shift; | |||||||||||
| 170 | 0 | DEBUG_MINUTES && print STDERR " III _minutes($s)...\n"; | |||||||||||
| 171 | 0 | my $min = 0; | |||||||||||
| 172 | 0 | ▶ | $min += 60*24*$1 if $s =~ /(\d+)\s?[dT]/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 173 | 0 | ▶ | $min += 60*$1 if $s =~ /(\d+)\s?[hS]/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 0 | ▶ | $min += $1 if $s =~ /(\d+)\s?[mM]/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 175 | 0 | DEBUG_MINUTES && print STDERR " min=$min=\n"; | |||||||||||
| 176 | 0 | return $min; | |||||||||||
| 177 | } # _minutes | ||||||||||||
| 178 | |||||||||||||
| 179 | sub _date_to_rel | ||||||||||||
| 180 | 0 | ▶ | { | ||||||||||
|
Subroutine
_date_to_rel: not called CC=3 SCAR=24.8
| |||||||||||||
| 181 | 0 | my $string = shift; | |||||||||||
| 182 | 0 | my $today = shift; | |||||||||||
| 183 | 0 | DEBUG_MINUTES && print STDERR " III _date_to_rel($string)...\n"; | |||||||||||
| 184 | 0 | ▶ | my $date = ParseDate($string) || ''; | ||||||||||
|
Condition0%0/2
ParseDate($string) || ''
MC/DC0%0/1
ParseDate($string) || ''
ParseDate($string)
Truth table0%0/2
A
A= ParseDate($string)
| |||||||||||||
| 185 | 0 | DEBUG_MINUTES && print STDERR " raw date =$date=...\n"; | |||||||||||
| 186 | 0 | ▶ | my $delta = DateCalc($today, $date) || 0; | ||||||||||
|
Condition0%0/2
DateCalc($today, $date) || 0
MC/DC0%0/1
DateCalc($today, $date) || 0
DateCalc($today, $date)
Truth table0%0/2
A
A= DateCalc($today, $date)
| |||||||||||||
| 187 | 0 | DEBUG_MINUTES && print STDERR " delta =$delta=...\n"; | |||||||||||
| 188 | # Convert to minutes: | ||||||||||||
| 189 | 0 | my $iMin = int(&Delta_Format($delta, 0, '%mt')); | |||||||||||
| 190 | 0 | my $result = "$iMin min"; | |||||||||||
| 191 | 0 | DEBUG_MINUTES && print STDERR " result =$result=...\n"; | |||||||||||
| 192 | 0 | return $result; | |||||||||||
| 193 | } # _date_to_rel | ||||||||||||
| 194 | |||||||||||||
| 195 | 1 | 1; | |||||||||||
| 196 | |||||||||||||
| 197 | __END__ | ||||||||||||