File Coverage

blib/lib/WWW/Search/Ebay/ByEndDate.pm
Criterion Covered Total %
statement 21 61 34.4
branch 0 8 0.0
condition 0 8 0.0
subroutine 7 11 63.6
pod n/a
total 28 88 31.8


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