All pastes #198545 Raw Edit

Mine

public text v1 · immutable
#198545 ·published 2006-10-12 01:32 UTC
rendered paste body
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use HTML::LinkExtor;

my @linklist;
my @otherlist;

my $url = shift || die "Usage: $0 <url>\n";

if ($url !~ m|^http(s)?://[a-z0-9\-\./]+$|i) {
  die "Invalid URL: must be formatted properly.  IE: http://www.example.com or https://ftp.example.ca\n";
}


my $content = get($url);

die "Unable to GET $url" if (!defined $content);


my $p = HTML::LinkExtor->new(sub {
                  my($tag, %links) = splice @_;

                  if ($tag eq "a") {
                    push(@linklist, values %links);
                  } else {
                    push(@otherlist, values %links);
                  }
              },$url);
$p->parse($content);


Section1($url,@linklist,@otherlist);
Section2($url,@linklist,@otherlist);
Section3(@otherlist);



### Section 1 is a sorted list of all links that point to a static web page with the same base url as the one originally input.

sub Section1 {
                my ($url,@links) = @_;

  myheader("Section 1");

  foreach (sort(@links)) {
    if ($_ =~ /$url.*\.html?$/) {
      print "$_\n";
    }else{
      push(@otherlist, $_)
    }
  }

  print "\n";
}

### Section 2 is a sorted list of all links that point to a static web page with a different base url as the one originally input.

sub Section2 {
  my ($url,@links) = @_;

  myheader("Section 2");

  foreach (sort(@links)) {
    if ($_ !~ /$url/) {
      if ($_ =~ /.*\.html?$/){
        print "$_\n";
      }
    }
  }
  print "\n";
}


### Section 3 is a sorted list of all the other links on the page (imgaes, scripts, etc.).

sub Section3 {
  my (@links) = @_;

  myheader("Section 3");

  foreach (sort(@links)) {
    print "$_\n";
      }

sub myheader {
  my ($text) = @_;
  print "x" x 20;
  print "  $text  ";
  print "x" x 30;
  print "\n\n";
}