Debian Codegraph @

ebay/lib/WWW/Search/Ebay/ByEndDate.pm

raw
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 use strict;
79 use warnings;
80
81 use Carp;
82 use Data::Dumper;
83 use Date::Manip;
84 use base 'WWW::Search::Ebay';
85
86 our
87 $VERSION = 2.341;
88 our $MAINTAINER = 'Martin Thurn <[email protected]>';
89
90 my $EBAY_TZ = 'America/Los_Angeles';
91
92 # private
93 sub _native_setup_search
94 {
95 my ($self, $native_query, $rhOptsArg) = @_;
96 $rhOptsArg ||= {};
97 unless (ref($rhOptsArg) eq 'HASH')
98 {
99 carp " --- second argument to _native_setup_search should be hashref, not arrayref";
100 return undef;
101 } # unless
102 # These will become CGI arguments when the HTTP request is made to
103 # ebay.com:
104 $rhOptsArg->{'SortProperty'} = 'MetaEndSort';
105 $rhOptsArg->{'_fosrp'} = 1;
106 $rhOptsArg->{'_sop'} = 1;
107 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 my $dateToday = ParseDate('today');
119 # Date_Init("SetDate=$EBAY_TZ");
120 # Date::Manip::DM6 says it will "default to the local timezone":
121 my $tz = undef;
122 # print STDERR " DDD convert today ==$dateToday== to tz ==$EBAY_TZ==\n";
123 $self->{_today_} = Date_ConvTZ($dateToday, $tz, $EBAY_TZ);
124 # print STDERR " DDD result today == ", $self->{_today_}, "\n";
125 # exit;
126 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 {
137 my ($self, @args) = @_;
138 my $hits = $self->SUPER::_parse_tree(@args);
139 $self->{cache} ||= [];
140 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 map { $_->[0] }
158 sort { $a->[1] cmp $b->[1] }
159 map { [ $_, $_->change_date || 0 ] }
160 @{$self->{cache}}
161 ];
162 return $hits;
163 } # _parse_tree
164
165 use constant DEBUG_MINUTES => 0;
166
167 sub _minutes
168 {
169 my $s = shift;
170 DEBUG_MINUTES && print STDERR " III _minutes($s)...\n";
171 my $min = 0;
172 $min += 60*24*$1 if $s =~ /(\d+)\s?[dT]/;
173 $min += 60*$1 if $s =~ /(\d+)\s?[hS]/;
174 $min += $1 if $s =~ /(\d+)\s?[mM]/;
175 DEBUG_MINUTES && print STDERR " min=$min=\n";
176 return $min;
177 } # _minutes
178
179 sub _date_to_rel
180 {
181 my $string = shift;
182 my $today = shift;
183 DEBUG_MINUTES && print STDERR " III _date_to_rel($string)...\n";
184 my $date = ParseDate($string) || '';
185 DEBUG_MINUTES && print STDERR " raw date =$date=...\n";
186 my $delta = DateCalc($today, $date) || 0;
187 DEBUG_MINUTES && print STDERR " delta =$delta=...\n";
188 # Convert to minutes:
189 my $iMin = int(&Delta_Format($delta, 0, '%mt'));
190 my $result = "$iMin min";
191 DEBUG_MINUTES && print STDERR " result =$result=...\n";
192 return $result;
193 } # _date_to_rel
194
195 1;
196
197 __END__