There's probably a better way to do this, but I developed a fairly simple set of tools a while ago to allow me to run google, askJeeves, dictionary, or thesaurus searches from the command line, using perl to parse the search words into a valid URL and then passing that off to lynx. E.g.:

% google cool uses for perl will start lynx in your terminal, at this address: http://www.google.com/search?q=cool+uses+for+perl

You'll need to have lynx in your path, you'll want to change the "use lib" statement in the four tools to the location that the browse.pm module is installed at, and you might need to change the netscape_path and mozilla_path vars at the bottom of browse.pm.

Enjoy!

browse.pm:

#!/usr/bin/perl package browse; use warnings; use strict; use Exporter (); @ISA = qw(Exporter); @EXPORT_OK = qw( jeeves google dictionary thesaurus ); use Getopt::Long; our %pars; sub browser_cmdline { DoGetOptions () if scalar @ARGV; my $site = shift () or die "no site in browse::browser_cmd +line\n"; my $bDontUseArgv = shift () || 0; my $sSearch = join '+', @ARGV unless $bDontUseArgv; # Replace quotes with http friendly %22-s, and replace whitespace +with # query-friendly '+'-s # $sSearch =~ s/"/%22/g; $sSearch =~ s/\s+/+/g; $site .= $sSearch; my $str = ( $pars{browser} eq '' ) ? "lynx -cookies A $site + " : ( $pars{browser} =~ /^m/ ) ? mozilla_path () . " $site +&" : netscape_path () . " $site &"; print "$str\n" and return if $pars{norun}; system $str; } </readmore> sub jeeves { browser_cmdline 'http://www.ask.com/main/askjeeves.asp?ask='; } sub google { browser_cmdline 'http://www.google.com/search?q='; } sub dictionary { browser_cmdline 'http://dictionary.reference.com/search?q='; } sub thesaurus { browser_cmdline 'http://thesaurus.reference.com/search?q='; } sub DoGetOptions { %pars = ( browser => "", norun => 0, help => 0, usage => 0, ); GetOptions ( \%pars, 'browser=s', 'norun', 'help', 'usage', ) or die "bad options $!"; my $help_txt = qq{\tgoogle/jeeves/dictionary/thesaurus \t[-browser] [-norun] [-help] [-usage] \tsearch words list google and jeeves perform a search (at the expected location) on the given words. dictionary looks up the first word on dictionary.com. Quoted strings must be enclosed in single quotes: google '\"one two three\"' -browser n[etscape] launches the search in netscape7, -browser m[ozilla] launches the search in mozilla (default is +lynx). -norun prints out the search URL, but doesn't load it.\n}; die $help_txt if $pars{help} || $pars{usage}; } sub netscape_path { "/usr/local/bin/netscape" } sub mozilla_path { "/usr/local/bin/mozilla" } 1;
dictionary:
#!/usr/bin/perl use warnings; use strict; use lib '/home/kester/bin/'; use browse qw( dictionary ); dictionary ();
google:
#!/usr/bin/perl use warnings; use strict; use lib '/home/kester/bin/'; use browse qw( google ); google ();
thesaurus:
#!/usr/bin/perl use warnings; use strict; use lib '/home/kester/bin/'; use browse qw( thesaurus ); thesaurus ();
jeeves:
#!/usr/bin/perl use warnings; use strict; use lib '/home/kester/bin/'; use browse qw( jeeves ); jeeves ();

Replies are listed 'Best First'.
Re: Command Line Google, AskJeeves, Dictionary, and Thesaurus
by hossman (Prior) on Sep 05, 2004 at 01:55 UTC

    A few quick suggestions...

    • Check out URI::Escape, your current escaping doesn't correctly handle a lot of common characters (in particular: single-quotes, plus signs, questions marks, etc...)
    • Instead of hardcoding a list of "sites" as method names, and then needing to write a seperate script for each one anyway, why not leave your module site agnostic -- and just make each script call "browser_cmdline" directly, passing the URL of the site (so you don't have to change the module just to add a new site).
    • Better still, why make yourself write a new script everytime you want to start searching a new site? why not just write a single script ("search" for example), that takes an optional "-site URL" argument (which defaults to something like "http://google.com/search?q=") and then just use your shell's command aliasing feature to create quick ways to search really common sites...
      alias google 'search -site "http://google.com/search?q="' alias jeeves 'search -site "http://www.ask.com/main/askjeeves.asp?ask= +"' ...
Re: Command Line Google, AskJeeves, Dictionary, and Thesaurus
by Aristotle (Chancellor) on Sep 05, 2004 at 02:08 UTC

    Instead of having a module and a dozen near-identical scripts, write a single script, and make a dozen symlinks to it. You can examine $0 to find out which name was used to call the script and act accordingly.

    Isn't Unix nice? :-)

    A lot of utilities use this idea in practice. zcat being symlinked to gzip is the first thing that came to mind, but a look at your /usr/bin will likely reveal dozens such symlinks. In fact, Busybox hides nearly an entire Unix toolkit inside a single binary for maximum space efficiency in embedded Linux systems.

    Makeshifts last the longest.

Re: Command Line Google, AskJeeves, Dictionary, and Thesaurus
by sintadil (Pilgrim) on Sep 06, 2004 at 11:21 UTC

    Nice work; here's some suggestions:

    • Use modules on CPAN instead of reinventing the wheel(s).
    • Don't hardcode things like browser paths and choices. This breaks portability, and is bound to tick off a user who absolutely despises Lynx because it's the ugliest program that they've ever seen. :) (like yours truly, who prefers ELinks)
    • Use existing protocols which are designed for certain tasks, instead of forcing their functionality to be hacked into working in a web environment. Specifically I'm speaking about the DICT protocol, but this applies elsewhere as well.

Re: Command Line Google, AskJeeves, Dictionary, and Thesaurus
by kesterkester (Hermit) on Sep 07, 2004 at 20:40 UTC
    Thanks much, all! I've used your suggestions to make the code to be shorter, more portable, and more robust:

    #!/usr/bin/perl use warnings; use strict; use Getopt::Long; use URI::Escape; { my %pars = DoGetOptions () if scalar @ARGV; my $str = "$pars{browser} $pars{site}" . uri_escape ( join '+', @AR +GV ); die "$str\n" if $pars{norun}; system $str; } sub DoGetOptions { my @sitelist = qw{ http://www.google.com/search?q= http://dictionary.reference.com/search?q= http://www.ask.com/main/askjeeves.asp?ask= http://thesaurus.reference.com/search?q= + }; my %pars = ( picksite => 0, site => '', browser => 'lynx -cookies A', #netcape #firefox norun => 0, ); GetOptions ( \%pars, qw{ picksite=s site=s browser=s norun } ) or die "bad options $!"; $pars{site} = $sitelist[$pars{picksite}] if '' eq $pars{site}; return %pars; }
Re: Command Line Google, AskJeeves, Dictionary, and Thesaurus
by Anonymous Monk on Sep 06, 2004 at 01:45 UTC
    http://surfraw.sourceforge.net/