#!/sw/bin/perl

use warnings;
use strict;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
#my $url = 'http://vrp.blogspot.com';
#my $url='http://partyfive.blogspot.com/';
my $url = 'http://pinkgoldenretrievers.blogspot.com/';

$mech->agent_alias( 'Mac Safari' );
$mech->get( $url );

my $page = $mech->content;
my $title = $page;
#my $description = $page;

# Get The Title
$title =~ s/.*<title>(.*)<\/title>.*/$1/s;
print "Blog Title: $title\n\n";

# Get the Description
#$description =~ s/.*<p id="description">(.*)<(\S)p>.*/$1/gs;
#print "Blog Description: $description\n\n";

# Get the "Main" part of the page, and make it one line in an array
$page =~ s/<!-- Begin #main - Contains main-column blog content -->(.*)<!-- End #main -->.*/$1/gs;
my @pages = split("\n", $page);

# Get the Date
#if ($page =~ /.*<h2 class="date-header">(.*)<\/h2>.*/s) { print "###### Date: $1 ######\n\n"; }

for (@pages) {
    if (/.*<h2 class="date-header">(.*)<\/h2>.*/s) { print"|: Date: $1 :|\n"; }
    if (/.*<div style="clear:both;"><\/div>(.*)<div style="clear:both; padding-bottom: 0.25em;"><\/div>.*/s) {
        my $message = $1; chomp($message);
        $message =~ s/<br(\s)(\S)>/\n/g;    # get rid of new-lines
        $message =~ s/<!--(.*)-->//s;   # get rid of comments
        $message =~ s/<a href='(.*)'>/$1/g; # where link is image
        $message =~ s/<a href="(.*)" target="(.*)">/$1/g; # where link is image
        $message =~ s/<a href='(.*)' target='(.*)'>/$1/g; # where link is image
        $message =~ s/<img(\s)(.*)><(\S)a>//g;
        $message =~ s/<img border='(.*)'>//g;
        $message =~ s/<a onblur="(.*)" href="(.*)">/$2\n/g; 
        $message =~ s/<span(.*)>//g; 
        $message =~ s/<(\S)span>//gs; 
        
        print "#" x 50;
        print "\n$message\n";
        print "#" x 50;
        print "\n\n\n";
    }
}

#print "$_\n" for (@page);
#my @links = $mech->links;
#for (@links) {
#    my $text = $_->text;
#    my $link = $_->url;
#
#    print "$text\n";
#    print "\t$link\n\n";
#}
