#!/usr/bin/perl ################################################################################ ### google.pl ### perform search on www.google.com ### ### Copyright (C) 2002 Ratatosk / Morten Wulff ### ### This program is free software; you can redistribute it and/or ### modify it under the terms of the GNU General Public License ### as published by the Free Software Foundation; either version 2 ### of the License, or (at your option) any later version. ### ### This program is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### GNU General Public License for more details. ### ### You should have received a copy of the GNU General Public License ### along with this program; if not, write to the Free Software ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ################################################################################ ### REVISIONS ### Begun : 04/07/2002 : Morten Wulff ### 1.0.0 : 08/09/2002 : Morten Wulff use strict; use vars '$result'; use Data::Dumper; use HTML::Template; use SOAP::Lite; ################################################################################ ### INIT ################################################################################ ### list of queries to perform my @queries = ('self injury', 'self mutilation','self-injury', 'self-mutilation'); ### google registration key my $key = 'guc7Rf4tGjCfrhMlg1k79oXAqr76yV4N'; ### save results to this file my $cache = 'cache.txt'; ### if 'online' is given on the command line, the script performs a new query ### otherwise, the local cache is used my $online = $ARGV[0] eq 'online'; ################################################################################ ### MAIN ################################################################################ if ( $online ) { # print "- online mode\n"; $result = google_query(); update_cache(); } else { # print "- offline mode\n"; unless ( my $return = do $cache ) { die "couldn't parse $cache: $@" if $@; die "couldn't do $cache: $!" unless defined $return; die "couldn't run $cache" unless $return; } } ### remove unnecessary HTML tags cleanup(); ### create output local $/; ### slurp mode ;-) my $html = ; my $t = HTML::Template->new( scalarref => \$html, die_on_bad_params => 0, global_vars => 1, loop_context_vars => 1 ); $t->param( { result => $result } ); print $t->output; ################################################################################ ### SUBS ################################################################################ ################################################################################ sub cleanup { foreach my $block ( @{ $result } ) { foreach my $item ( @{ $block->{resultElements} } ) { $item->{title} =~ s/<\/*b>//g; $item->{snippet} =~ s/<\/*b>//g; $item->{summary} =~ s/<\/*b>//g; $item->{directoryTitle} =~ s/<\/*b>//g; } } } ################################################################################ sub google_query { my $online = []; my $google = SOAP::Lite -> service("file:./GoogleSearch.wsdl"); foreach my $query ( @queries ) { print "- processing : $query\n"; push @{$online}, $google -> doGoogleSearch( $key, ### google key "\"$query\"", ### query 0, ### index of first desired result 10, ### number of results per query (max 10) "true", ### use results filter "", ### restrictions "false", ### filter adult content (SafeSearch) "", ### language restrict "latin1", ### input encoding "latin1" ### output encoding ); } return $online; } ################################################################################ sub update_cache { open (OUT, ">$cache") or die "Could not open file: $cache\n$1\n"; my $dumper = Data::Dumper->new( [$result], [ qw(result) ] ); $dumper->Purity(1); print OUT $dumper->Dump; close OUT; } ################################################################################ ### TEMPLATE ################################################################################ __DATA__

Estimated number of results:

">