| 1 |
# FirstGov.pm |
| 2 |
# by Dennis Sutch |
| 3 |
# |
| 4 |
|
| 5 |
package WWW::Search::FirstGov; |
| 6 |
|
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
WWW::Search::FirstGov - class for searching http://www.firstgov.gov |
| 11 |
|
| 12 |
=head1 SYNOPSIS |
| 13 |
|
| 14 |
use WWW::Search; |
| 15 |
my $search = new WWW::Search('FirstGov'); # cAsE matters |
| 16 |
my $query = WWW::Search::escape_query("uncle sam"); |
| 17 |
$search->native_query($query); |
| 18 |
while (my $result = $search->next_result()) { |
| 19 |
print $result->url, "\n"; |
| 20 |
} |
| 21 |
|
| 22 |
=head1 DESCRIPTION |
| 23 |
|
| 24 |
Class specialization of WWW::Search for searching F<http://www.firstgov.gov>. |
| 25 |
|
| 26 |
FirstGov.gov can return up to 100 hits per page. |
| 27 |
|
| 28 |
This class exports no public interface; all interaction should |
| 29 |
be done through WWW::Search objects. |
| 30 |
|
| 31 |
=head1 OPTIONS |
| 32 |
|
| 33 |
The following search options can be activated by sending a hash as the |
| 34 |
second argument to native_query(). |
| 35 |
|
| 36 |
=head2 Result Set Partitioning |
| 37 |
|
| 38 |
=over 4 |
| 39 |
|
| 40 |
=item { 'begin_at' => '100' } |
| 41 |
|
| 42 |
Retrieve results starting at 100th match. |
| 43 |
|
| 44 |
This option is not passed to FirstGov.gov. Instead, this option is used to |
| 45 |
set 'offset', 'act.next.x' and 'act.next.y' options to obtain results starting |
| 46 |
the requested starting point. |
| 47 |
|
| 48 |
=item { 'offset' => '100' } |
| 49 |
|
| 50 |
If 'act.next.x' and 'act.next.y' options are set, retrive results starting at |
| 51 |
the 100th plus 1 plus the value of the 'nr' option. If 'act.prev.x' and |
| 52 |
'act.prev.y' options are set, retrive results starting at the 100th plus 1 |
| 53 |
less the value of the 'nr' option. |
| 54 |
|
| 55 |
Note: Do not use this option. Use the 'begin_at' option instead. |
| 56 |
|
| 57 |
Note: This option was named 'fr' in a past version of FirstGov.gov's search |
| 58 |
engine. |
| 59 |
|
| 60 |
=item { 'act.next.x' => '1', 'act.next.y' => '1' } |
| 61 |
|
| 62 |
Retrieve next set of results, starting at the value of the 'offset' option plus |
| 63 |
1 plus the value of the 'nr' option. |
| 64 |
|
| 65 |
Note: Do not use this option. |
| 66 |
|
| 67 |
=item { 'act.prev.x' => '1', 'act.prev.y' => '1' } |
| 68 |
|
| 69 |
Retrieve previous set of results, starting at the value of the 'offset' |
| 70 |
option plus 1 less the value of the 'nr' option. |
| 71 |
|
| 72 |
Note: Do not use this option. |
| 73 |
|
| 74 |
=item { 'nr' => '40' } |
| 75 |
|
| 76 |
Retrieve 40 results. |
| 77 |
|
| 78 |
FirstGov.gov returns no more than 100 results at a time. |
| 79 |
|
| 80 |
From FirstGov.gov documentation: |
| 81 |
|
| 82 |
The Number of Results parameter (nr) allows you or the user to set how many |
| 83 |
search "hits" appear on each search results page. If this parameter is not |
| 84 |
used, nr is defaulted to 10. |
| 85 |
|
| 86 |
=back |
| 87 |
|
| 88 |
=head2 Query Terms |
| 89 |
|
| 90 |
From FirstGov.gov documentation: |
| 91 |
|
| 92 |
You may have noticed many parameters are suffixed by the number zero (0). |
| 93 |
This number essentially groups a set of search parameters sharing the same |
| 94 |
suffix number together to form a query statement. It is possible to link |
| 95 |
two or more such statements together. You might have guessed that this is |
| 96 |
accomplished by creating another set of search parameters, this time suffixed |
| 97 |
by the number one (1) or higher. Just be careful to keep track of parameters |
| 98 |
and follow the same guidelines as outline above. For example if you have an |
| 99 |
mw0, have corresponding ms0, mt0, etc. parameters. For each mw1, set its own |
| 100 |
corresponding ms1, mt1, etc. parameters. Be forewarned that the more |
| 101 |
complicated the query, the longer it may take to process. |
| 102 |
|
| 103 |
=over 4 |
| 104 |
|
| 105 |
=item { 'mw0' => 'uncle sam' } |
| 106 |
|
| 107 |
Return results that contain the words 'uncle' and 'sam'. |
| 108 |
|
| 109 |
The native_query() method sets this option. |
| 110 |
|
| 111 |
WWW::Search::FirstGov defaults the mw0 option to an empty string. |
| 112 |
|
| 113 |
Note: Do not use the 'mw0' option, instead use the native_query() method and |
| 114 |
the 'mw1', 'mw2', ... options. |
| 115 |
|
| 116 |
From FirstGov.gov documentation: |
| 117 |
|
| 118 |
The Main Words parameter is represented by the input field named mw0. This |
| 119 |
is a text input field that allows a user to enter the word or words they |
| 120 |
would like to search for. |
| 121 |
|
| 122 |
=item { 'mt0' => 'all' } |
| 123 |
|
| 124 |
=item { 'mt0' => 'any' } |
| 125 |
|
| 126 |
=item { 'mt0' => 'phrase' } |
| 127 |
|
| 128 |
WWW::Search::FirstGov defaults the mt0 option to 'all'. |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
From FirstGov.gov documentation: |
| 133 |
|
| 134 |
The Main Type field (mt0) is used to specify how you want to search for the |
| 135 |
words entered in the mw0 field. You can search for documents containing all |
| 136 |
the words provided, any of the words provided, or documents containing the |
| 137 |
exact phrase in the order the words are entered. This is done by setting the |
| 138 |
mt0 field to "all", "any", or "phrase". If this field is not provided, it is |
| 139 |
defaulted to "all". |
| 140 |
|
| 141 |
=item { 'ms0' => 'should' } |
| 142 |
|
| 143 |
=item { 'ms0' => 'mustnot' } |
| 144 |
|
| 145 |
=item { 'ms0' => 'must' } |
| 146 |
|
| 147 |
WWW::Search::FirstGov defaults the ms0 option to 'must'. |
| 148 |
|
| 149 |
From FirstGov.gov documentation: |
| 150 |
|
| 151 |
Main Sign field (ms0) further specifies your search. It can be used to specify |
| 152 |
whether words should or must not be present in the document. This is done by |
| 153 |
setting the ms0 field to "should" or "mustnot". If this field is not provided, |
| 154 |
it is defaulted to "should". |
| 155 |
|
| 156 |
=item { 'in0' => 'any' } |
| 157 |
|
| 158 |
=item { 'in0' => 'title' } |
| 159 |
|
| 160 |
=item { 'in0' => 'url' } |
| 161 |
|
| 162 |
Note: The value 'anywhere' was used in place of 'any' in a past version of |
| 163 |
FirstGov.gov's search engine. The value 'home' used in a past version of |
| 164 |
FirstGov.gov's search engine is obsolete. |
| 165 |
|
| 166 |
From FirstGov.gov documentation: |
| 167 |
|
| 168 |
The In parameter (in0) can be implemented to tell the search engine where |
| 169 |
specifically to search. Setting in0 to "anywhere" searches the complete web |
| 170 |
page of all web pages in a particular database. [...] Setting in0 to "title" |
| 171 |
searches only the Titles of web pages of a particular database. |
| 172 |
|
| 173 |
=item { 'in0' => 'domain', 'dom0' => 'doc.gov noaa.gov', 'domToggle' => ' +(' } |
| 174 |
|
| 175 |
=item { 'in0' => 'domain', 'dom0' => 'doc.gov noaa.gov', 'domToggle' => ' -(' } |
| 176 |
|
| 177 |
The query is limited to searching the doc.gov and noaa.gov domains when the |
| 178 |
doToggle option is set to ' +(' (note leading space). The query is limited to |
| 179 |
searching all but the doc.gov and noaa.gov domains when the doToggle option is |
| 180 |
set to ' -(' (note leading space). |
| 181 |
|
| 182 |
From the FirstGov.gov documentation: |
| 183 |
|
| 184 |
Setting in0 to "domain" allows you to search only a certain domain or domains, |
| 185 |
or domain/path combinations. Use of this attribute also requires an |
| 186 |
additional parameter, the Domain parameter (dom0). The Domain parameter (dom0), |
| 187 |
when used with in0="domain", allows searching of specific domains or |
| 188 |
domain/path combinations as described above. This is useful if you want |
| 189 |
a "site search" for your website. To do this, you could set in0 to domain |
| 190 |
and then dom0 to yourdomain.com. This would ensure that users are only |
| 191 |
searching web pages within your domain. In fact, you may specify as many |
| 192 |
domain or domain/path combinations up to 20 that you would like to limit your |
| 193 |
searches to. You can use any combination of domains or domain/path elements |
| 194 |
as long as they are separated by a comma or a space. |
| 195 |
|
| 196 |
=item { 'rs' => '1' } |
| 197 |
|
| 198 |
Results will include variations (example: vote, voting). |
| 199 |
|
| 200 |
=item { 'doc' => '' } |
| 201 |
|
| 202 |
=item { 'doc' => 'text/html' } |
| 203 |
|
| 204 |
=item { 'doc' => 'application/pdf' } |
| 205 |
|
| 206 |
=item { 'doc' => 'text/xml' } |
| 207 |
|
| 208 |
=item { 'doc' => 'application/msword' } |
| 209 |
|
| 210 |
=item { 'doc' => 'application/vnd.ms-excel' } |
| 211 |
|
| 212 |
=item { 'doc' => 'application/vnd.ms-powerpoint' } |
| 213 |
|
| 214 |
=item { 'doc' => 'text/plain' } |
| 215 |
|
| 216 |
Restrict results to a type of document. |
| 217 |
|
| 218 |
=item { 'age' => 'any' } |
| 219 |
|
| 220 |
=item { 'age' => '1m' } |
| 221 |
|
| 222 |
=item { 'age' => '3m' } |
| 223 |
|
| 224 |
=item { 'age' => '6m' } |
| 225 |
|
| 226 |
=item { 'age' => '9m' } |
| 227 |
|
| 228 |
=item { 'age' => '1y' } |
| 229 |
|
| 230 |
Restrict results to date document was updated. |
| 231 |
|
| 232 |
=item { 'sop' => '<', 'siz' => '512', 'byt' => 'b' } |
| 233 |
|
| 234 |
Restrict results to documents less than 512 bytes. |
| 235 |
|
| 236 |
The attribute sop may be set to '<' or '>'. The attribute 'byt' may be set to |
| 237 |
'b', 'kb', or 'mb'. |
| 238 |
|
| 239 |
=item { 'lang' => '' } |
| 240 |
|
| 241 |
The query is not limited by language. |
| 242 |
|
| 243 |
To limit a query to documents of a specific language, set this option to one of |
| 244 |
FirstGov's language abbreviations (an empty string denotes "Any language"): |
| 245 |
af - Afrikaans, |
| 246 |
sq - Albanian, |
| 247 |
ar - Arabic, |
| 248 |
eu - Basque, |
| 249 |
be - Byelorussian, |
| 250 |
bg - Bulgarian, |
| 251 |
ca - Catalan, |
| 252 |
tzh - Chinese (trad), |
| 253 |
szh - Chinese (simp), |
| 254 |
hr - Croatian, |
| 255 |
cs - Czech, |
| 256 |
da - Danish, |
| 257 |
nl - Dutch, |
| 258 |
en - English, |
| 259 |
et - Estonian, |
| 260 |
fo - Faeroese, |
| 261 |
fi - Finnish, |
| 262 |
fr - French, |
| 263 |
fy - Frisian, |
| 264 |
gl - Galician, |
| 265 |
de - German, |
| 266 |
el - Greek, |
| 267 |
he - Hebrew, |
| 268 |
hu - Hungarian, |
| 269 |
is - Icelandic, |
| 270 |
id - Indonesian, |
| 271 |
it - Italian, |
| 272 |
ja - Japanese, |
| 273 |
ko - Korean, |
| 274 |
la - Latin, |
| 275 |
lv - Latvian, |
| 276 |
lt - Lithuanian, |
| 277 |
ms - Malay, |
| 278 |
no - Norwegian, |
| 279 |
pl - Polish, |
| 280 |
pt - Portuguese, |
| 281 |
ro - Romanian, |
| 282 |
ru - Russian, |
| 283 |
sk - Slovak, |
| 284 |
sl - Slovenian, |
| 285 |
es - Spanish, |
| 286 |
sv - Swedish, |
| 287 |
th - Thai, |
| 288 |
tr - Turkish, |
| 289 |
uk - Ukrainian, |
| 290 |
vi - Vietnamese, |
| 291 |
cy - Welsh |
| 292 |
|
| 293 |
=back |
| 294 |
|
| 295 |
=head2 Specifying Federal and/or State Government Databases |
| 296 |
|
| 297 |
=over 4 |
| 298 |
|
| 299 |
=item { 'db' => 'www' } |
| 300 |
|
| 301 |
Note: The db and st options have been merged into the the db option in the |
| 302 |
current version of FirstGov.gov's search engine. The value 'states' now |
| 303 |
specifies searches against all states. |
| 304 |
|
| 305 |
From FirstGov.gov documentation: |
| 306 |
|
| 307 |
The Database field (db) allows you to specify if a search should query |
| 308 |
Federal Government websites, State Government websites, or both. This is done |
| 309 |
by setting db to "www" for a Federal Search, setting db to "states" for a |
| 310 |
State Search, or "www-fed-all" to search both. [...] If the db field is not |
| 311 |
provided, it is defaulted to Federal. |
| 312 |
|
| 313 |
List of State and Territory Abbreviations for FirstGov Searching: |
| 314 |
AS - All States, |
| 315 |
AL - Alabama, |
| 316 |
AK - Alaska, |
| 317 |
AZ - Arizona, |
| 318 |
AR - Arkansas, |
| 319 |
CA - California, |
| 320 |
CO - Colorado, |
| 321 |
CT - Connecticut, |
| 322 |
DC - D.C., |
| 323 |
DE - Delaware, |
| 324 |
FL - Florida, |
| 325 |
GA - Georgia, |
| 326 |
HI - Hawaii, |
| 327 |
ID - Idaho, |
| 328 |
IL - Illinois, |
| 329 |
IN - Indiana, |
| 330 |
IA - Iowa, |
| 331 |
KS - Kansas, |
| 332 |
KY - Kentucky, |
| 333 |
LA - Louisiana, |
| 334 |
ME - Maine, |
| 335 |
MD - Maryland, |
| 336 |
MA - Massachusetts, |
| 337 |
MI - Michigan, |
| 338 |
MN - Minnesota, |
| 339 |
MS - Mississippi, |
| 340 |
MO - Missouri, |
| 341 |
MT - Montana, |
| 342 |
NE - Nebraska, |
| 343 |
NV - Nevada, |
| 344 |
NH - New Hampshire, |
| 345 |
NJ - New Jersey, |
| 346 |
NM - New Mexico, |
| 347 |
NY - New York, |
| 348 |
NC - North Carolina, |
| 349 |
ND - North Dakota, |
| 350 |
OH - Ohio, |
| 351 |
OK - Oklahoma, |
| 352 |
OR - Oregon, |
| 353 |
PA - Pennsylvania, |
| 354 |
RI - Rhode Island, |
| 355 |
SC - South Carolina, |
| 356 |
SD - South Dakota, |
| 357 |
TN - Tennessee, |
| 358 |
TX - Texas, |
| 359 |
UT - Utah, |
| 360 |
VT - Vermont, |
| 361 |
VA - Virginia, |
| 362 |
WA - Washington, |
| 363 |
WV - West Virginia, |
| 364 |
WI - Wisconsin, |
| 365 |
WY - Wyoming, |
| 366 |
SA - American Samoa, |
| 367 |
GU - Guam, |
| 368 |
MP - Mariana Islands, |
| 369 |
MH - Marshall Islands, |
| 370 |
FM - Micronesia, |
| 371 |
PR - Puerto Rico, |
| 372 |
VI - Virgin Islands. |
| 373 |
|
| 374 |
=back |
| 375 |
|
| 376 |
=head2 Result Presentation |
| 377 |
|
| 378 |
=over 4 |
| 379 |
|
| 380 |
=item { 'rn' => '2' } |
| 381 |
|
| 382 |
Request FirstGov.gov to return pages using affiliate #2's page format. |
| 383 |
|
| 384 |
This option is used by FirstGov.gov to return result pages customized |
| 385 |
with headers and footers for the affiliate as identified by the 'rn' option. |
| 386 |
|
| 387 |
When not set, FirstGov.gov currently sets the 'rn' parameter to '2'. |
| 388 |
|
| 389 |
Note: It is suggested that this option not be used (since this class was |
| 390 |
developed using results returned with the 'rn' option not set). |
| 391 |
|
| 392 |
From FirstGov.gov documentation: |
| 393 |
|
| 394 |
The Referrer Name (rn) field is used to uniquely identify your affiliate. |
| 395 |
Each Affiliate, upon registration, is assigned a referrer ID that corresponds |
| 396 |
to it. |
| 397 |
|
| 398 |
=item { 'srt' => '' } |
| 399 |
|
| 400 |
Sort results by relevance (FirstGov.gov's default). |
| 401 |
|
| 402 |
Additional values are: 8 (Date - ascending), 7 (Date - descending), 6 |
| 403 |
(Size - ascending), and 5 (Size - descending). |
| 404 |
|
| 405 |
=item { 'dsc' => 'det' } |
| 406 |
|
| 407 |
=item { 'dsc' => 'sum' } |
| 408 |
|
| 409 |
Whether ('det') or not ('sum') to highlight search terms in result |
| 410 |
summaries. |
| 411 |
|
| 412 |
=back |
| 413 |
|
| 414 |
=head2 Other Options |
| 415 |
|
| 416 |
=over 4 |
| 417 |
|
| 418 |
=item { 'parsed' => 'true' } |
| 419 |
|
| 420 |
The default behavior for FirstGov.gov's search engine is to parse all search |
| 421 |
requests, and, if any options are missing or deprecated, rewrite the options |
| 422 |
and redirect the browser back to FirstGov.gov. When the parsed option is set |
| 423 |
to "true", FirstGov.gov does not perform this action. |
| 424 |
|
| 425 |
=item { 'sp' => '1' } |
| 426 |
|
| 427 |
Check spelling of the query terms. FirstGov.gov will return a message and |
| 428 |
a resubmit buttion if it finds misspelled query terms. |
| 429 |
|
| 430 |
Note: WWW::Search::FirstGov does not parse the results for this option. |
| 431 |
|
| 432 |
=item { 'submit' => 'Search' } |
| 433 |
|
| 434 |
Submit button. |
| 435 |
|
| 436 |
=item { 'md' => 'adv' } |
| 437 |
|
| 438 |
Function is unknown. |
| 439 |
|
| 440 |
=back |
| 441 |
|
| 442 |
=head1 SEE ALSO |
| 443 |
|
| 444 |
To make new back-ends, see L<WWW::Search>, |
| 445 |
or the specialized AltaVista searches described in options. |
| 446 |
|
| 447 |
An email list is for notifying users of updates to Perl's |
| 448 |
WWW::Search::FirstGov module is avilalable at |
| 449 |
http://two.pairlist.net/mailman/listinfo/www-search-firstgov . |
| 450 |
It is meant to be a very low volume list that only notifies users when there |
| 451 |
is a new version of the module available, and possibly when changes to the |
| 452 |
FirstGov search engine have broken the latest version of the module. |
| 453 |
|
| 454 |
=head1 HOW DOES IT WORK? |
| 455 |
|
| 456 |
C<native_setup_search> is called before we do anything. |
| 457 |
It initializes our private variables (which all begin with underscores) |
| 458 |
and sets up a URL to the first results page in C<{_next_url}>. |
| 459 |
|
| 460 |
C<native_retrieve_some> is called (from C<WWW::Search::retrieve_some>) |
| 461 |
whenever more hits are needed. It calls the LWP library |
| 462 |
to fetch the page specified by C<{_next_url}>. |
| 463 |
It parses this page, appending any search hits it finds to |
| 464 |
C<{cache}>. If it finds a ``next'' button in the text, |
| 465 |
it sets C<{_next_url}> to point to the page for the next |
| 466 |
set of results, otherwise it sets it to undef to indicate we're done. |
| 467 |
|
| 468 |
=head1 SUPPORT |
| 469 |
|
| 470 |
Send questions, comments, suggestions, and bug reports to <[email protected]>. |
| 471 |
|
| 472 |
To be notified of updates to WWW::Search::FirstGov, subscribe to the update |
| 473 |
notification list at http://two.pairlist.net/mailman/listinfo/www-search-firstgov |
| 474 |
|
| 475 |
=head1 AUTHOR |
| 476 |
|
| 477 |
C<WWW::Search::FirstGov> is written and maintained |
| 478 |
by Dennis Sutch - <[email protected]>. |
| 479 |
|
| 480 |
=head1 LEGALESE |
| 481 |
|
| 482 |
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 483 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 484 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 485 |
|
| 486 |
=head1 BUGS |
| 487 |
|
| 488 |
None reported. |
| 489 |
|
| 490 |
=head1 VERSION HISTORY |
| 491 |
|
| 492 |
1.14 2003-04-08 - Updated parsing. |
| 493 |
Documented new search parameters. |
| 494 |
1.13 2002-06-04 - Updated Makefile.PL to reflect requirement for WWW::Search version 2.33. |
| 495 |
Rewrote tests to use WWW::Search::Test. |
| 496 |
1.12 2002-06-03 - Updated to reflect changes to FirstGov (on 2002 Jun 03 they switched to a new search engine built by Fast Search & Transfer of Oslo, Norway). |
| 497 |
* The native_query options 'fr' and 'st' are obsolete. |
| 498 |
* the native_query option 'in0' now accepts the value 'any' instead of 'anywhere', and the value of 'home' is obsolete. |
| 499 |
Removed redefined WWW::Search functionality. |
| 500 |
|
| 501 |
1.11 2002-03-13 - Upated to reflect changed FirstGov search engine parameters. |
| 502 |
approximate_result_count() now returns 1 more than the result count when FirstGov's result count is "more than X relevant results". |
| 503 |
Changed test case 4 (in test.pl) to finish sooner. |
| 504 |
|
| 505 |
1.10 2002-03-05 - Updated to handle new FirstGov search engine format and to use HTML::TreeBuilder. |
| 506 |
Fixed problem that caused one too many searches against FirstGov.gov. |
| 507 |
Documented additional options, including adding notes from FirstGov.gov documentation. |
| 508 |
|
| 509 |
1.04 2001-07-16 - Fixed parsing problem. |
| 510 |
|
| 511 |
1.03 2001-03-01 - Removed 'require 5.005_62;'. |
| 512 |
|
| 513 |
1.02 2001-03-01 - Removed 'my' declarations for package variables. |
| 514 |
|
| 515 |
1.01 2001-02-26 - Fixed problem with quoted sring on MSWin. |
| 516 |
Removed 'our' declarations. |
| 517 |
|
| 518 |
1.00 2001-02-23 - First publicly-released version. |
| 519 |
|
| 520 |
=cut |
| 521 |
|
| 522 |
##################################################################### |
| 523 |
use strict; |
| 524 |
|
| 525 |
require Exporter; |
| 526 |
@WWW::Search::FirstGov::EXPORT = qw(); |
| 527 |
@WWW::Search::FirstGov::EXPORT_OK = qw(); |
| 528 |
@WWW::Search::FirstGov::ISA = qw( WWW::Search Exporter ); |
| 529 |
$WWW::Search::FirstGov::VERSION = '1.14'; |
| 530 |
|
| 531 |
$WWW::Search::FirstGov::MAINTAINER = 'Dennis Sutch <[email protected]>'; |
| 532 |
|
| 533 |
use Carp (); |
| 534 |
use WWW::Search( 'generic_option' ); |
| 535 |
require WWW::SearchResult; |
| 536 |
|
| 537 |
my $SEARCH_URL_PATH = 'http://www.firstgov.gov/fgsearch/'; |
| 538 |
|
| 539 |
my $default_option = { |
| 540 |
'search_url' => $SEARCH_URL_PATH . 'index.jsp', |
| 541 |
# 'mw0' => '', # search words |
| 542 |
'offset' => 0, # return results starting at match number 'offset' plus 1 and plus (or minus) 'nr', when act.next.x and .y (or act.prev.x and .y) are set |
| 543 |
'nr' => 20, # number of results returned per page (max = 100) |
| 544 |
'mt0' => 'all', # match: "all" = All of the words | "any" = Any of the words | "phrase" = The exact phrase |
| 545 |
'ms0' => 'must', # "should" = Should include | "mustnot" = Must not include | "must" = Must include |
| 546 |
'parsed' => 'true', # (added by FirstGov.gov's redirect) |
| 547 |
}; |
| 548 |
|
| 549 |
sub native_setup_search { |
| 550 |
my($self, $native_query, $native_options_ref) = @_; |
| 551 |
$self->{'_debug'} = $native_options_ref->{'search_debug'}; |
| 552 |
$self->{'_debug'} = 2 if ($native_options_ref->{'search_parse_debug'}); |
| 553 |
$self->{'_debug'} = 0 if (!defined($self->{'_debug'})); |
| 554 |
|
| 555 |
print STDERR " + WWW::Search::FirstGov::native_setup_search()\n" if ($self->{'_debug'}); |
| 556 |
|
| 557 |
$self->{'agent_name'} = ref($self) . '/' . $WWW::Search::FirstGov::VERSION; |
| 558 |
$self->user_agent('non-robot'); |
| 559 |
|
| 560 |
my $oTree = new HTML::TreeBuilder; |
| 561 |
$oTree->store_comments(1); # comments are required to parse results page |
| 562 |
$self->{'_treebuilder'} = $oTree; |
| 563 |
|
| 564 |
$self->{'_next_to_retrieve'} = 0; |
| 565 |
|
| 566 |
$self->{'_num_hits'} = 0; |
| 567 |
|
| 568 |
if (! defined($self->{'_options'})) { |
| 569 |
foreach (keys %$default_option) { |
| 570 |
$self->{'_options'}{$_} = $default_option->{$_}; |
| 571 |
} |
| 572 |
$self->{'_options'}{'mw0'} = $native_query; |
| 573 |
} |
| 574 |
if (defined($native_options_ref)) { |
| 575 |
foreach (keys %$native_options_ref) { |
| 576 |
$self->{'_options'}{$_} = $native_options_ref->{$_} if ($_ ne 'begin_at'); |
| 577 |
} |
| 578 |
} |
| 579 |
# if user has set 'begin_at' option, then handle other options to get desired result |
| 580 |
if (exists($native_options_ref->{'begin_at'}) && defined($native_options_ref->{'begin_at'})) { |
| 581 |
my $begin_at = $native_options_ref->{'begin_at'} || 1; |
| 582 |
$begin_at = 1 if ($begin_at < 1); |
| 583 |
$self->{'_options'}{'offset'} = $begin_at - 1 - $self->{'_options'}{'nr'}; |
| 584 |
$self->{'_options'}{'act.next.x'} = 1; |
| 585 |
$self->{'_options'}{'act.next.y'} = 1; |
| 586 |
} |
| 587 |
my $options = ''; |
| 588 |
foreach (sort keys %{$self->{'_options'}}) { |
| 589 |
next if (generic_option($_)); |
| 590 |
$options .= '&' if ($options); |
| 591 |
$options .= $_ . '=' . $self->{'_options'}{$_}; |
| 592 |
} |
| 593 |
$self->{'_next_url'} = $self->{'_options'}{'search_url'} . '?' . $options; |
| 594 |
} |
| 595 |
|
| 596 |
sub parse_tree { |
| 597 |
my($self, $tree) = @_; |
| 598 |
|
| 599 |
print STDERR " + WWW::Search::FirstGov::parse_tree()\n" if ($self->{'_debug'}); |
| 600 |
|
| 601 |
print STDERR " + (parse_tree) result HTML page tree:\n" if ($self->{'_debug'} > 1); |
| 602 |
$tree->dump( *STDERR ) if ($self->{'_debug'} > 1); |
| 603 |
|
| 604 |
return undef if (! defined($self->{'_prev_url'})); # fast exit if already done |
| 605 |
|
| 606 |
# approximate_result_count |
| 607 |
my $result_count = undef; |
| 608 |
my @td = $tree->look_down('_tag', 'td'); |
| 609 |
while (! defined($result_count) && (my $td = shift(@td))) { |
| 610 |
my $text = $td->as_text(); |
| 611 |
if ($text =~ m{Your\s+(.*\s)?search\s+(for.*\s.*\s)?returned\s+(\d+)\s+results\.}is) { |
| 612 |
$result_count = $3; |
| 613 |
} elsif ($text =~ m{Your\s+(.*\s)?search\s+(for.*\s)?returned\s+more\s+than\s+(\d+)\s+(relevant\s+)results\.}is) { |
| 614 |
$result_count = $3 + 1; |
| 615 |
} elsif ($text =~ m{Your\s+(.*\s)?search\s+(for.*\s.*\s)?did\s+not\s+return\s+any\s+documents\.}is) { |
| 616 |
$result_count = 0; |
| 617 |
} |
| 618 |
} |
| 619 |
if (defined($result_count)) { |
| 620 |
$self->approximate_result_count($result_count); |
| 621 |
} |
| 622 |
print STDERR " + (parse_tree) approximate_result_count is " . $result_count . "\n" if ($self->{'_debug'}); |
| 623 |
|
| 624 |
# SearchResults |
| 625 |
my $hits_found = 0; |
| 626 |
my $results_table_comment = $tree->look_down('_tag', '~comment', sub { $_[0]->attr('text') =~ m{Begin\s+display\s+of\s+search\s+results}si }); |
| 627 |
return undef if (! defined($results_table_comment)); # exit if no results table comment |
| 628 |
my $results_table = $results_table_comment->right(); # locate table containing results |
| 629 |
return undef if (! defined($results_table)); # exit if no results table |
| 630 |
my @results_tds = $results_table->look_down('_tag', 'td'); # get array of all TDs within the table of results |
| 631 |
my %result = (); # hash to contain one result |
| 632 |
foreach my $result_td (@results_tds) { |
| 633 |
next if ($result_td->as_text() =~ m{^(\s|\xA0)*$}s); # ignore any white space (or ) TDs |
| 634 |
print STDERR " + (parse_tree) result_td: " . $result_td->as_text() . "\n" if ($self->{'_debug'} > 1); |
| 635 |
if (! exists($result{'count'})) { # count TD occurs first |
| 636 |
if ($result_td->as_text() =~ m{^\s*(\d+)\.?\s*$}s) { # digit(s) with optional period |
| 637 |
$result{'count'} = $1; |
| 638 |
} # else ignore this TD |
| 639 |
} elsif (! exists($result{'url'})) { # url/title (anchor) TD occurs second |
| 640 |
if (my $result_a = $result_td->look_down('_tag', 'a')) { # if TD contains A |
| 641 |
$result{'url'} = $result_a->attr('href'); |
| 642 |
if ($result{'url'} =~ m{url=([^&]+)}i) { # strip URL out of FirstGov.gov's redirect URL |
| 643 |
$result{'url'} = $1; |
| 644 |
} |
| 645 |
$result{'title'} = $result_a->as_text(); |
| 646 |
} # else ignore this TD |
| 647 |
} else { # description TD occurs third |
| 648 |
my $hit = WWW::SearchResult->new(); |
| 649 |
$hit->add_url($result{'url'}); |
| 650 |
$hit->title($result{'title'}); |
| 651 |
$hit->description(&WWW::Search::strip_tags($result_td->as_text())); |
| 652 |
push(@{$self->{cache}}, $hit); |
| 653 |
$self->{'_num_hits'} += 1; |
| 654 |
$hits_found += 1; |
| 655 |
%result = (); |
| 656 |
} # the URL TD occurs fourth and is ignored when looking for count TD |
| 657 |
} |
| 658 |
|
| 659 |
# _next_url |
| 660 |
my $a_href = undef; |
| 661 |
if ($tree->look_down('_tag', 'table', sub { |
| 662 |
defined($_[0]->look_down('_tag', 'font', sub { |
| 663 |
defined($a_href = $_[0]->look_down('_tag', 'a', sub { |
| 664 |
defined($_[0]->look_down('_tag', 'img', sub { |
| 665 |
$_[0]->attr('alt') =~ /next/i }))}))}))})) { |
| 666 |
$self->{'_next_url'} = $SEARCH_URL_PATH . $a_href->attr('href'); |
| 667 |
print STDERR " + (parse_tree) _next_url is " . $self->{'_next_url'} . "\n" if ($self->{'_debug'}); |
| 668 |
} else { |
| 669 |
print STDERR " + (parse_tree) _next_url is undefined\n" if ($self->{'_debug'}); |
| 670 |
} |
| 671 |
|
| 672 |
print STDERR " + (parse_tree) hits_found: " . $hits_found . "\n" if ($self->{'_debug'}); |
| 673 |
return $hits_found; |
| 674 |
} |
| 675 |
|
| 676 |
1; |