File Coverage

blib/lib/WWW/Search/MSN.pm
Criterion Covered Total %
statement 20 79 25.3
branch 0 26 0.0
condition 0 4 0.0
subroutine 7 9 77.7
pod 2 2 100.0
total 29 120 24.1


line stmt bran cond sub pod time code
1             package WWW::Search::MSN;
2              
3 2     2   29095 use warnings;
  2         4  
  2         63  
4 2     2   11 use strict;
  2         5  
  2         39  
5              
6 2     2   41 use 5.008;
  2         10  
7              
8             require WWW::Search;
9              
10 2     2   1549 use WWW::SearchResult;
  2         87579  
  2         72  
11 2     2   1942 use Encode;
  2         23928  
  2         171  
12              
13 2     2   15 use Scalar::Util ();
  2         4  
  2         67  
14              
15             =head1 NAME
16              
17             WWW::Search::MSN - backend for searching search.msn.com
18              
19             =head1 VERSION
20              
21             Version 0.0202
22              
23             =cut
24              
25             our $VERSION = '0.0202';
26              
27 2     2   11 use vars qw(@ISA);
  2         4  
  2         1578  
28              
29             @ISA=(qw(WWW::Search));
30              
31             =head1 WARNING! THIS MODULE IS DEPRECATED
32              
33             You should be using L instead which uses the API.
34              
35             =head1 SYNOPSIS
36              
37             This module provides a backend of L to search using
38             L.
39              
40             use WWW::Search;
41              
42             my $oSearch = WWW::Search->new("MSN");
43              
44             =head1 FUNCTIONS
45              
46             All of these functions are internal to the module and are of no concern
47             of the user.
48              
49             =head2 native_setup_search()
50              
51             This function sets up the search.
52              
53             =cut
54              
55             sub native_setup_search
56             {
57 0     0 1   my ($self, $native_query, $opts) = @_;
58              
59 0           $self->{'_hits_per_page'} = 10;
60              
61 0           $self->user_agent('non-robot');
62              
63 0           $self->user_agent()->default_header('Accept-Language' => "en");
64              
65 0           $self->{'_next_to_retrieve'} = 1;
66              
67 0   0       $self->{'search_base_url'} ||= 'http://search.msn.com';
68 0   0       $self->{'search_base_path'} ||= '/results.aspx';
69              
70 0 0         if (!defined($self->{'_options'}))
71             {
72 0           $self->{'_options'} = +{
73             'q' => $native_query,
74             'FORM' => "PORE",
75             };
76             }
77 0           my $self_options = $self->{'_options'};
78              
79 0 0         if (defined($opts))
80             {
81 0           foreach my $k (keys %$opts)
82             {
83 0 0         if (WWW::Search::generic_option($k))
84             {
85 0 0         if (defined($opts->{$k}))
86             {
87 0           $self->{$k} = $opts->{$k};
88             }
89             }
90             else
91             {
92 0 0         if (defined($opts->{$k}))
93             {
94 0           $self_options->{$k} = $opts->{$k};
95             }
96             }
97             }
98             }
99              
100 0           $self->{'_next_url'} = $self->{'search_base_url'} . $self->{'search_base_path'} . '?' . $self->hash_to_cgi_string($self_options);
101 0           $self->{'_MSN_first_retrieve_call'} = 1;
102             }
103              
104             =head2 parse_tree()
105              
106             This function parses the tree and fetches the results.
107              
108             =cut
109              
110             sub parse_tree
111             {
112 0     0 1   my ($self, $tree) = @_;
113              
114 0 0         if ($self->{'_MSN_first_retrieve_call'})
115             {
116 0           $self->{'_MSN_first_retrieve_call'} = undef;
117              
118 0           my $header_div = $tree->look_down("_tag", "div", "id", "results_area");
119              
120 0 0         if (!defined($header_div))
121             {
122 0           return 0;
123             }
124 0           my $h5 = $header_div->look_down("_tag", "span", "id", "count");
125              
126 0 0         if (!defined($h5))
127             {
128 0           return 0;
129             }
130              
131 0 0         if ($h5->as_text() =~ m{^\d+-\d+\s+of\s+([\d,]+)\s+results})
132             {
133 0           my $n = $1;
134 0           $n =~ tr/,//d;
135 0           $self->approximate_result_count($n);
136             }
137             }
138              
139 0           my $results_div = $tree->look_down("_tag", "div", "id", "results");
140 0           my $results_ul = $results_div->look_down("_tag", "ul");
141 0           my @items;
142 0 0         @items = (grep { Scalar::Util::blessed($_) && ($_->tag() eq "li") } $results_ul->content_list());
  0            
143              
144 0           my $hits_found = 0;
145 0           foreach my $item (@items)
146             {
147 0           my $h3 = $item->look_down("_tag", "h3");
148 0           my ($a_tag) = (grep { $_->tag() eq "a" } $h3->content_list());
  0            
149 0           my ($p_tag) = (grep { $_->tag() eq "p" } $item->content_list());
  0            
150 0           my $url = $a_tag->attr("href");
151              
152 0           my $hit = WWW::SearchResult->new();
153 0           $hit->add_url($url);
154 0           $hit->title($a_tag->as_text());
155 0 0         $hit->description(defined($p_tag) ? $p_tag->as_text() : "");
156 0           push @{$self->{'cache'}}, $hit;
  0            
157 0           $hits_found++;
158             }
159              
160             # Get the next URL
161             {
162 0           my $pagination_div =
  0            
163             $tree->look_down("_tag", "div", "class", "sb_pag");
164 0 0         if ($pagination_div)
165             {
166 0           my ($a_tag) = $pagination_div->look_down(
167             "_tag", "a", "class", "sb_pagN"
168             );
169              
170 0 0         if ($a_tag)
171             {
172             $self->{'_next_url'} =
173             $self->absurl(
174 0           $self->{'_prev_url'},
175             $a_tag->attr('href')
176             );
177             }
178             }
179             }
180 0           return $hits_found;
181             }
182              
183             =head1 AUTHOR
184              
185             Shlomi Fish, L .
186              
187             Funded by L and
188             L.
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests to
193             C, or through the web interface at
194             L.
195             I will be notified, and then you'll automatically be notified of progress on
196             your bug as I make changes.
197              
198             =head1 ACKNOWLEDGEMENTS
199              
200             Funded by L and
201             L.
202              
203             =head1 DEVELOPMENT
204              
205             Source code is version-controlled in a Subversion repository in Berlios:
206              
207             L
208              
209             One can find the most up-to-date version there.
210              
211             =head1 COPYRIGHT & LICENSE
212              
213             Copyright 2006 Shlomi Fish, all rights reserved.
214              
215             This program is released under the following license: MIT X11 (a BSD-style
216             license).
217              
218             =cut
219              
220             1; # End of WWW::Search::MSN