#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;
print extract_quotes(get_quotes_page());
sub get_quotes_page {
my $links;
my $mech = new WWW::Mechanize;
$mech->get( "http://bash.org/?random1" );
$mech->success or die "Can't get the bash quote page, are you connected to the internet? Or maybe bash is down.";
my @links = $mech->find_all_links( url_regex => qr(\?[0-9]+) )
or die "Weird error, for some reason it got the quote page, but didnt find any quotes on the page";
# Use the first link
my ( $url, $title ) = @{$links[0]};
$mech->get( $links->[0] );
return $mech->content;
}
sub extract_quotes {
my $page = shift;
for my $quote ( $page ) {
my @lines = split( /<br \/>/, $quote );
for ( @lines ) {
s/<[^>]+>//g; # Strip HTML tags
s/\s+/ /g; # Squash whitespace
s/^ //; # Strip leading space
s/ $//; # Strip trailing space
s/"/"/g; # Replace HTML entity quotes
}
$quote = join( "\n", @lines );
}
return $page;
}