package GetLinks;

=head1 NAME

GetLinks - fetch a webpage and return a list of its links.

=head1 SYNOPSIS

  use GetLinks;
  my @links = getLinks("http://www.perl.com/");

=head1 DESCRIPTION

The GetLinks.pm package defines a single subroutine, getLinks, 
which will retrieve the webpage with a given URL, search its
content for links to other pages, and return those links
as a list of absolute URLS.  

Only tags of the form <A HREF="url"> are return.  IMG tags, 
LINK tags, or other external references are ignored.

Eventually this package may include more options; however,
at this point it's just a simple interface to the LWP
and HTML::LinkExtor packages.

=head2 EXPORT

  getLinks()

=head1 SEE ALSO

  HTML::LinkExtor, LWP, HTML::Parser, HTML::Tagset, URI::URL

=head1 COPYRIGHT AND LICENSE

Copyright 2003 by Jim Mahoney, E<lt>mahoney@marlboro.edu<gt>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut
# ====================================================================
use 5.008;
our $VERSION = 0.1;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(getLinks);

use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

my $userAgent;
my $parser;
my @links;

sub init {
  unless ($userAgent) {
    $userAgent = LWP::UserAgent->new;
    $parser    = HTML::LinkExtor->new( \&addLink );
  }
  @links = ();
}

sub addLink {
  my ($tag, %attr) = @_;
  return if $tag ne 'a';			# only keep <a ...> tags
  push @links,$attr{href} if $attr{href};	# with href= attributes.
}

sub getLinks {
  my ($url) = @_;
  init();
  # Request the document, parsing it as it arrives
  my $response = $userAgent->request(HTTP::Request->new(GET => $url),
				     sub {$parser->parse($_[0])});

  # Expand the links to absolute urls and return it.
  my $base = $response->base;
  return map { $_ = url($_, $base)->abs; } @links;
}

1;







