our
$VERSION
=
do
{
my
@r
= (
q$Revision: 2.42 $
=~ /\d+/g );
sprintf
"%d."
.
"%03d"
x
$#r
,
@r
};
sub
native_setup_search {
my
(
$self
,
$native_query
,
$native_options_ref
) =
@_
;
$self
->user_agent(
'user'
);
$self
->{_next_to_retrieve} = 0;
if
(!
defined
(
$self
->{_options})) {
$self
->{_options} = {
'cat'
=>
'63'
,
};
};
my
(
$options_ref
) =
$self
->{_options};
if
(
defined
(
$native_options_ref
)) {
foreach
(
keys
%$native_options_ref
) {
$options_ref
->{
$_
} =
$native_options_ref
->{
$_
};
};
};
my
(
$options
) =
''
;
foreach
(
keys
%$options_ref
) {
next
if
(generic_option(
$_
));
$options
.=
$_
.
'='
.
$options_ref
->{
$_
} .
'&'
;
};
$self
->{_debug} =
$options_ref
->{
'search_debug'
};
$self
->{_debug} = 2
if
(
$options_ref
->{
'search_parse_debug'
});
$self
->{_debug} = 0
if
(!
defined
(
$self
->{_debug}));
$self
->{_base_url} =
$self
->{_next_url} =
$self
->{_options}{
'search_url'
} .
"?"
.
$options
.
"q="
.
$native_query
;
print
$self
->{_base_url} .
"\n"
if
(
$self
->{_debug});
}
sub
native_retrieve_some {
my
(
$self
) =
@_
;
return
undef
if
(!
defined
(
$self
->{_next_url}));
print
STDERR
"WWW::Search::Metapedia::native_retrieve_some: fetching "
.
$self
->{_next_url} .
"\n"
if
(
$self
->{_debug});
my
(
$response
) =
$self
->http_request(
'GET'
,
$self
->{_next_url});
$self
->{response} =
$response
;
if
(!
$response
->is_success) {
return
undef
;
};
my
(
$HEADER
,
$HITS
,
$TRAILER
,
$POST_NEXT
) = (1..10);
my
(
$hits_found
) = 0;
my
(
$state
) = (
$HEADER
);
my
(
$hit
,
$title
,
$url
,
$sDesc
) = ();
foreach
(
$self
->split_lines(
$response
->content())) {
next
if
m@^$@;
if
(
$state
==
$HEADER
&& m
@metasearch
results.*?of\s(\d+)
@i
) {
$self
->approximate_result_count($1);
print
STDERR
"PARSE(HEADER->HITS-1): $_\n"
if
(
$self
->{_debug} >= 2);
$state
=
$HITS
;
}
elsif
(
$state
==
$HITS
&& m@<dt><a href=.*?,http%3A%2F%2F(.*?)\">(.*)</font></dt><dd><.*?>(.*)<br>
@i
) {
my
(
$url
,
$title
,
$sDesc
) = ($1,$2,$3);
print
STDERR
"PARSE(HITS->HITS): $_\n"
if
(
$self
->{_debug} >= 2);
my
(
$hit
) = new WWW::SearchResult;
$title
=~ s/<b>//g;
$hit
->add_url(
&decodeURL
(
$url
));
$hit
->title(
$title
);
$hit
->description(
$sDesc
);
$hits_found
++;
push
(@{
$self
->{cache}},
$hit
);
$state
=
$HITS
;
}
elsif
(
$state
==
$HITS
&& m@<dt><a href=.*?,http%3A%2F%2F(.*?)\">(.*)</font></dd><br>
@i
) {
my
(
$url
,
$title
,
$sDesc
) = ($1,$2,$3);
$sDesc
.= $3;
print
STDERR
"PARSE(HITS->HITS): $_\n"
if
(
$self
->{_debug} >= 2);
my
(
$hit
) = new WWW::SearchResult;
$title
=~ s/<b>//g;
$hit
->add_url(
&decodeURL
(
$url
));
$hit
->title(
$title
);
$hit
->description(
$sDesc
);
$hits_found
++;
push
(@{
$self
->{cache}},
$hit
);
$state
=
$HITS
;
}
elsif
(
$state
==
$HITS
&& (m@</font></dl>
@i
)) {
print
STDERR
"PARSE(HITS->TRAILER): $_\n\n"
if
(
$self
->{_debug} >= 2);
$state
=
$TRAILER
;
}
elsif
(
$state
==
$TRAILER
&& m@<a href=
"([^"
]+)">
next
\<\/a\>(.*)
@i
) {
my
(
$relative_url
) = $1;
$self
->{_next_url} = new URI::URL(
$relative_url
,
$self
->{_base_url});
print
STDERR
"PARSE(TRAILER->POST_NEXT): $_\n\n"
if
(
$self
->{_debug} >= 2);
$state
=
$POST_NEXT
;
}
else
{
print
STDERR
"PARSE: read:\"$_\"\n"
if
(
$self
->{_debug} >= 2);
};
};
if
(
$state
!=
$POST_NEXT
) {
if
(
defined
(
$hit
)) {
push
(@{
$self
->{cache}},
$hit
);
};
$self
->{_next_url} =
undef
;
};
$self
->user_agent_delay
if
(
defined
(
$self
->{_next_url}));
return
$hits_found
;
}
sub
decodeURL {
my
$url
=
shift
;
$url
=~ s/%2E/./g;
$url
=~ s/%2F/\//g;
$url
=~ s/%3A/:/g;
$url
=~ s/%2D/-/g;
$url
=~ s/%5F/_/g;
$url
=~ s/%7E/~/g;
return
$url
;
}
1;