File: opensearch-genquery

package info (click to toggle)
surfraw 2.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,652 kB
  • ctags: 377
  • sloc: sh: 8,571; perl: 824; makefile: 322
file content (328 lines) | stat: -rwxr-xr-x 7,624 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/perl -w
# $Id$
# Ian Beckwith <ianb@erislabs.net>
# 20060830

use Getopt::Long;

use strict;
use vars qw($me);
$me=($0=~/(?:.*\/)?(.*)/)[0];

use vars qw(@wantedtypes $htmlmime $atommime $rssmime);
@wantedtypes=();
$htmlmime="text/html";
$atommime="application/atom+xml";
$rssmime ="application/rss+xml";

my $quiet=0;
my $count=undef;
my $startindex=undef;
my $startpage=undef;
my $language=undef;
my $inputencoding=undef;
my $outputencoding=undef;
my $help=0;

Getopt::Long::Configure("no_ignore_case");

GetOptions("quiet"              => \$quiet,
		   "H|html"             => sub { push(@wantedtypes,$htmlmime); },
		   "A|atom"             => sub { push(@wantedtypes,$atommime); },
		   "R|rss"              => sub { push(@wantedtypes,$rssmime);  },
		   "count=i"            => \$count,
		   "language=s"         => \$language,
		   "i|startindex=i"     => \$startindex,
		   "p|startpage=i"      => \$startpage,
		   "O|outputencoding=s" => \$outputencoding,
		   "I|inputencoding=s"  => \$inputencoding,
		   "h|help"             => \$help);

if ($help || ($#ARGV<1)) { usage(); }

my $url=shift;
my $searchterms=join(' ',@ARGV);

unless($url=~/^http\:\/\//i)
{
	$url="http://".$url;
}

unless (eval { require WWW::OpenSearch; })
{
	unless($quiet)
	{
		print STDERR "$me: Cannot find WWW::OpenSearch, is it installed?\n";
	}
	exit 4;
}

my $engine;
eval { $engine=WWW::OpenSearch->new($url); };
if($@)
{
	my $errstr="";
	my $exitcode=0;
	if($@ =~/fetching/i)
	{
		$errstr="Error fetching $url";
		$exitcode=1;
	}
	elsif(($@ =~/parsing/i) ||
		  ($@ =~/node\s+should/i))
	{
		$errstr="Error parsing OpenSearch Description at $url"; 
		$exitcode=2;
	}
	else
	{
		# Some other error, probably because WWW::OpenSearch has changed
		$errstr=$@;
		$exitcode=5;
	}
	unless($quiet)
	{
		print STDERR "$me: $errstr\n";
	}
	exit $exitcode;
}

my $desc=$engine->description();

# copy options to hash, translating from option name to OpenSearch param
my %options=();
$options{searchTerms} = $searchterms;
if(defined($count))          { $options{count}          = $count;          }
if(defined($startindex))     { $options{startIndex}     = $startindex;     }
if(defined($startpage))      { $options{startPage}      = $startpage;      }
if(defined($language))       { $options{language}       = $language;       }
if(defined($inputencoding))  { $options{inputEncoding}  = $inputencoding;  }
if(defined($outputencoding)) { $options{outputEncoding} = $outputencoding; }


my @urls=();
for (@{$desc->url()})
{
	if($_->method() =~ /^get$/i)
	{
		push(@urls,$_);
	}
}

my $besturl=undef;

# if no preferred type specified use first Url.
if($#wantedtypes<0)
{
	$besturl=$urls[0];
}
else # find best match against @wantedtypes
{
	my $bestindex=undef;
	for my $urlindex (0..$#urls)
	{
		for(my $typeindex=0;$typeindex<=$#wantedtypes;$typeindex++)
		{
			if($urls[$urlindex]->type() eq $wantedtypes[$typeindex])
			{
				if((!defined($bestindex)) || ($typeindex < $bestindex))
				{
					$bestindex=$typeindex;
					$besturl=$urls[$urlindex];
				}
			}
		}
	}
}

if(!defined($besturl))
{
	unless($quiet)
	{
		print STDERR "$me: No matching search type found\n";
	}
	exit 3;
}
	
print $besturl->prepare_query(\%options),"\n";

exit 0;


sub usage
{
	die("Usage: $me [OPTIONS] URL SEARCH TERMS...\n".
		" Outputs the url of a query generated from an OpenSearch Description\n".
		" at URL with SEARCH TERMS filled in.\n".
		"Options:\n".
		" -q\t\tQuiet: Don't display errors.\n".
		"   -H, -A, -R can be ordered to express response type priority.\n".
		" -H\t\tRequest HTML response.\n".
		" -A\t\tRequest Atom response.\n".
		" -R\t\tRequest RSS response.\n".
		"   Not all sites implement all (or any) of the following options.\n".
		"   Unimplemented options will be silently ignored.\n".
		" -c COUNT\tRequest COUNT results (OpenSearch param: count).\n".
		" -i INDEX\tRequest results start at NUM (OpenSearch param: startIndex).\n".
		" -p PAGENUM\tRequest results page PAGENUM (OpenSearch param: pageIndex).\n".
		" -l LANG\tRequest results in LANG (2-letter ISO country code) (OpenSearch param: language).\n".
		" -I ENC\t\tSpecify search terms are encoded in ENC (OpenSearch param: inputEncoding).\n".
		" -O ENC\t\tRequest results encoded in ENC (OpenSearch param: outputEncoding).\n".
		"Return codes:\n".
		" 0 - Success.\n".
		" 1 - Error fetching OpenSearch Description URL.\n".
		" 2 - Error parsing OpenSearch Description.\n".
		" 3 - No matching search type found.\n".
		" 4 - Cannot find perl module WWW::OpenSearch.\n".
		" 5 - Unhandled error from WWW::OpenSearch.\n");
}

__END__


=head1 NAME

opensearch-genquery - Output the URL of a query generated from an OpenSearch Description

=head1 SYNOPSIS

B<opensearch-genquery> [B<-q>] [B<-H>] [B<-A>] [B<-R>]
S<[B<-c> I<COUNT>]> S<[B<-i> I<INDEX>]>
S<[B<-p> I<PAGENUM>]> S<[B<-l> I<LANG>]>
S<[B<-O> I<ENC>]> S<[B<-I> I<ENC>]>
I<URL> S<I<SEARCH TERMS...>>

=head1 DESCRIPTION

Output the URL of a query generated from an OpenSearch Description
file at I<URL>, with the specified S<I<SEARCH TERMS>> and other
options filled in. 

If multiple search types are available, B<-H>,
B<-A> and B<-R> can be used to specify which type is wanted. For
example, S<B<-A -R>> can be used to request an Atom response, or
failing that fall back to RSS. By default the first URL found in the
description file is used.

Not all web sites implement all (or any) of the optional OpenSearch
parameters. When used with those sites, the corresponding options will
be silently ignored.

=head1 OPTIONS

=over 4

=item B<-q>

Give no output on errors. In this case, consult the exit code to
determine errors (See L</DIAGNOSTICS>).

=item B<-H>

Request HTML response.

=item B<-A>

Request Atom response.

=item B<-R>

Request RSS response.

B<-H>, B<-A>, and B<-R> can be combined to express response type
priority and specify fallback types.

=item B<-c COUNT>, B<--count=COUNT>

Request COUNT results.

OpenSearch parameter: I<count>.

=item B<-i INDEX>, B<--startindex=INDEX>

Request results start at offset NUM.

OpenSearch parameter: I<startIndex>.

=item B<-p PAGENUM>, B<--startpage=PAGENUM>

Request results page PAGENUM.

OpenSearch parameter: I<pageIndex>.

=item B<-l LANG>, B<--language=LANG>

Request results in LANG. LANG should be a 2-letter ISO country code
(e.g. B<en> or B<de>).

OpenSearch parameter: I<language>.

=item B<-O ENC>, B<--outputencoding=ENC>

Request results encoded in ENC (default: UTF-8).

OpenSearch parameter: I<outputEncoding>.

=item B<-I ENC>, B<--inputencoding=ENC>

Specify search terms are encoded in ENC (default: UTF-8).

OpenSearch parameter: I<inputEncoding>.

=item B<-h>, B<--help>

Show a short help message.

=item B<-->

End of options.

=back

=head1 DIAGNOSTICS

The exit code can be consulted to determine errors, as follows:

=over 4

=item B<0> - Success.

=item B<1> - Error fetching OpenSearch Description URL.

=item B<2> - Error parsing OpenSearch Description.

=item B<3> - No matching search type found.

=item B<4> - Cannot find perl module WWW::OpenSearch.

=item B<5> - Unhandled error from WWW::OpenSearch.

=back

=head1 DEPENDENCIES

Requires the module WWW::OpenSearch (Debian package
libwww-opensearch-perl).

=head1 BUGS

Only supports GET queries.

Please report any bugs found to ianb@erislabs.net

=head1 SEE ALSO

L<opensearch(1)>, L<opensearch-discover(1)>, L<surfraw(1)>,
L<WWW::OpenSearch(3pm)>, L<http://www.opensearch.org/>

=head1 AUTHOR

Ian Beckwith <ianb@erislabs.net>

=head1 COPYRIGHT AND LICENSE

Copyright 2006 Ian Beckwith <ianb@erislabs.net>

Licensed under the same terms as surfraw.

=cut