summaryrefslogtreecommitdiffstats
path: root/scripts/google.pl
blob: 84de8486afa115b884ab2fa2975c7302c96555d8 (plain)
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
# - Google.pl
use Irssi;
use Getopt::Long qw/GetOptionsFromString/;
use IPC::Open3;
use JSON::PP;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '2.01';
%IRSSI = (
    authors     => 'bw1',
    contact     => 'bw1@aol.at',
    name        => 'google',
    description => 'This script queries google.com with googler and returns the results.',
    license     => 'Public Domain',
    url		=> 'https://scripts.irssi.org/',
    modules => '',
    commands=> 'google',
    selfcheckcmd=> 'google -check',
);

my $help = << "END";
%9Name%9
  $IRSSI{name}
%9Version%9
  $VERSION
%9Usage%9
  /google [-N|-news] [-x|-exact] [-c|-tld TLD] [-l|-lang LANG]
            [-n|-count N] [-s|-start] <KEYWORD>
  /google {-h|-help}
  /google {-p|-say N}
  /google -check
%9Description%9
  $IRSSI{description}
  first author: Oddbjrn Kvalsund
%9Arguments%9
  -N|-news      show results from news section
  -x|-exact     disable automatic spelling correction
  -c|-tld       country-specific search with top-level domain
  -l|-lang      display in language LANG
  -n|-count     show N results (default 10)
  -s|-start     start at the Nth result
  -h|-help      show this help message
  -p|-say       say the N url in channel
  -check        self check
%9See also%9
  https://github.com/jarun/googler
END

my ($copt, $tld, $lang, $count, $start, $chelp, $say, $check);
my %options = (
	'N'=> sub {$copt .= '--news '},
	'news'=> sub {$copt .= '--news '},
	'x'=> sub {$copt .= '--exact '},
	'exact'=> sub {$copt .= '--exact '},
	'c=s'=> \$tld,
	'tld=s'=> \$tld,
	'l=s'=> \$lang,
	'lang=s'=> \$lang,
	'n=o' => \$count,
	'count=o' => \$count,
	's=o' => \$start,
	'start=o' => \$start,
	'h' => \$chelp,
	'help' => \$chelp,
	'p=o' => \$say,
	'say=o' => \$say,
	'check' => \$check,
);

## Usage:
## /google [-p, prints to current window] [-<number>, number of searchresults returned] search-criteria1 search-criteria2 ...
##
## History:
## - Sun May 19 2002
##   Version 0.1 - Initial release
## - 2019-08-04
##   Version 2.0 - Change to googler
## - 2021-01-26
##   Version 2.01 - self check
## -------------------------------

my (%readex, $instr, $errstr, @res);

sub read_exec {
	my ($cmd, $rfunc) = @_;

	my ($in, $out, $err);
	use Symbol 'gensym'; $err = gensym;
	my $pid = open3($in, $out, $err, $cmd);
	$readex{$pid}->{pid}=$pid;
	$readex{$pid}->{cmd}=$cmd;
	$readex{$pid}->{in}=$in;
	$readex{$pid}->{out}=$out;
	$readex{$pid}->{err}=$err;
	$readex{$pid}->{rfunc}=$rfunc;

	Irssi::pidwait_add($pid);
}

sub sig_read_exec {
	my ($pid, $status) = @_;

	if (defined $readex{$pid} ) {
		my $out =$readex{$pid}->{out};
		my $err =$readex{$pid}->{err};
		my $rfunc =$readex{$pid}->{rfunc};

		delete $readex{$pid};

		my $old = select $out;
		local $/;
		$instr = <$out>;
		select $old;

		my $old = select $err;
		local $/;
		$errstr = <$err>;
		$errstr =~ s/[\n\r]//g;
		select $old;

		&$rfunc() if (defined $rfunc);
		if ( scalar(keys(%readex)) == 1 &&
				exists $readex{job}) {
			foreach my $j ( @{$readex{job}} ) {
				if ( ref( $j) eq 'CODE' ) {
					&$j();
				} else {
					eval( $j );
				}
			}
			delete $readex{job};
		}
		Irssi::signal_stop();
	}
}

sub cmd {
	my ($args, $server, $witem)=@_;
	Getopt::Long::Configure('no_ignore_case');
	my ($ret, $arg) = GetOptionsFromString($args, %options);
	if ($ret) {
		if (defined $chelp) {
			cmd_help($IRSSI{name}, $server, $witem);
		} elsif (defined $say) {
			if ($say >0 && $say <= scalar(@res)) {
				Irssi::active_win()->command("say $res[$say-1]->{url}");
			}
		} else {
			my $cmd="googler --json ";
			$cmd .="--tld $tld " if (defined $tld);
			$cmd .="--lang $lang " if (defined $lang);
			$cmd .="--count $count " if (defined $count);
			$cmd .="--start $start " if (defined $start);
			$cmd .="irssi " if (defined $check);
			$cmd .="$copt " if (defined $copt);
			$cmd .=join(" ",@{$arg});
			Irssi::print(">$cmd<", MSGLEVEL_CLIENTCRAP);
			read_exec($cmd ,\&print_all);
		}
	}
	$copt=undef;
	$tld=undef;
	$lang=undef;
	$count=undef;
	$start=undef;
	$chelp=undef;
	$say=undef;
}

sub self_check {
	my @r =@_;
	my $s="ok";
	$check=undef;
	Irssi::print("Selfcheck: results: ".scalar @r);
	Irssi::print("Selfcheck: url: ".$r[0]->{url});
	Irssi::print("Selfcheck: title: ".$r[0]->{title});
	if ( scalar(@r) < 6 ) {
		$s="Error: results (".scalar @r.")";
	} elsif ( $r[0]->{url} !~ m/^http/ ) {
		$s="Error: url (".$r[0]->{url}.")";
	} elsif ( length($r[0]->{title}) < 4) {
		$s="Error: title (".$r[0]->{title}.")";
	}
	Irssi::print("Selfcheck: $s");
	my $schs_version = $Irssi::Script::selfcheckhelperscript::VERSION;
	Irssi::command("selfcheckhelperscript $s") if ( defined $schs_version );
}

sub print_all {
	if( length($errstr) <1 ) {
		@res= @{decode_json($instr)};
		self_check(@res) if (defined $check);
		Irssi::print("/---- google ----", MSGLEVEL_CLIENTCRAP);
		my $c=1;
		foreach my $r (@res) {
			my $s= sprintf("| %3d ",$c) . $r->{title};
			Irssi::print($s, MSGLEVEL_CLIENTCRAP);
			$s="|     ". $r->{url};
			Irssi::print($s, MSGLEVEL_CLIENTCRAP);
			$c++;
		}
		Irssi::print('\---- google ----', MSGLEVEL_CLIENTCRAP);
	} else {
		Irssi::print($errstr, MSGLEVEL_CLIENTCRAP);
	}
}

sub cmd_help {
	my ($args, $server, $witem)=@_;
	$args=~ s/\s+//g;
	if ($IRSSI{name} eq $args) {
		Irssi::print($help, MSGLEVEL_CLIENTCRAP);
		Irssi::signal_stop();
	}
}

$ENV{PYTHONIOENCODING}='utf8';
Irssi::signal_add('pidwait', 'sig_read_exec');

Irssi::command_bind('google', 'cmd');
my @opt=map {$_ =~ s/=.*$//, $_ } keys %options;
Irssi::command_set_options($IRSSI{name}, join(" ", @opt));
Irssi::command_bind('help', \&cmd_help);