#!/usr/bin/perl
#
# Parse the Amazon Web Services XML data
# (downloaded separately using fetchbooks.pl)
#
# written by Jackie Hamilton (kira@cgi101.com)
# for the cgi101.com "books" pages.
#
# This script uses the XML::Simple module to parse the XML data.
# It also uses a 'truncate' module I wrote to truncate the text
# while preserving (and closing) HTML tags.
#
# see http://www.cgi101.com/books/howto/ for more on
# how this all works together.
#

use XML::Simple;
use lib '/home/www/books/bin';
use truncate;
use Date::Format;

my $basepath = '/home/www/books';

@keywords = qw(Perl PHP Java Javascript MySQL XML CSS);
push(@keywords, "Flash MX");

foreach my $keyword (@keywords) {
    my $file = lc($keyword);        # the xml file name is the lowercase keyword
    $file =~ s/\s+/_/g;             # and spaces are changed out for underscores

    my $xmlfile = $basepath . "/xml/" . $file . ".xml";
    my $xml = XMLin($xmlfile);
    
    my $output = qq(
<table cellspacing="0" class="book_table">\n);
    foreach my $item (@{$xml->{Items}->{Item}}) {

        my $desc;
        my $reviews = $item->{EditorialReviews}{EditorialReview};
        if (UNIVERSAL::isa($reviews, "ARRAY")) {
          foreach my $review (@{$item->{EditorialReviews}{EditorialReview}}) {
             if ($review->{Source} eq "Book Description") {
               $desc = $review->{Content};
             }
          }
        } else {
           $desc = $reviews->{Content};
        }
        
        # truncate the description at 500 characters
        
        my $desc2 = truncate($desc, 500);

        # add a "more" link if it was actually truncated.
        
        if ($desc2 ne $desc) {
            $desc2 .= qq( <a href="$item->{DetailPageURL}">[read more]</a>);
        }

        if ($desc2) { $desc2 = qq(<p>$desc2</p>\n); }

        # figure out the ratings and translate them to stars. First we
        # use sprintf to round the rating to 2 digits. Then we need to
        # round to the nearest 0.5, since we have whole and half stars
        # graphics from 0 to 5.

        my ($rating, $rimg);
        if ($item->{CustomerReviews}{AverageRating}) {
            $rating = sprintf("%.2f", $item->{CustomerReviews}{AverageRating});
            my($d, $f) = split(/\./, $rating);

            # round to the nearest .5
            if ($f > 0 and $f <= 25) { $f = 0; }
            elsif ($f > 25 and $f <= 74) { $f = 5; }
            elsif ($f > 74 and $f <= 99) { $f = 0; $d++; }  

            if ($d >= 0 and $d <= 5 and $f >= 0 and $f <= 5) {
                $rimg = qq(<img src="/books/stars/stars-$d-$f.gif">);
            }
        }

        # now write out the formatted data cell
        
        $output .= qq(
<tr class="book_row">
    <td valign="TOP" class="book_image"> <!-- image cell -->
<a href="$item->{DetailPageURL}" target="_blank"><img src="$item->{MediumImage}{URL}" border="0"></a> <br>
    </td>
    <td valign="TOP" class="book_desc">

<b><a href="$item->{DetailPageURL}" target="_blank">$item->{ItemAttributes}{Title}</a></b> 
$rimg<br>
List Price: $item->{ItemAttributes}{ListPrice}{FormattedPrice}<br>
<a href="$item->{DetailPageURL}" target="_blank">Amazon's Price:</a> <b>$item->{Offers}{Offer}{OfferListing}{Price}{FormattedPrice}</b><br>    

$desc2

    </td>
</tr>
<tr><td colspan="2" class="spacer"> </td></tr>
                );
    }
    $output .= qq(</table>\n);
    
    &outfile($keyword, $output);    # write out the file to "$keyword.html"
}


sub outfile {
    my($topic, $content) = @_;
    my $filename = lc($topic) . ".html";
    $filename =~ s/\s+/_/g;
    my $gentime = time2str("%C", time);
    
    open(OUT, ">$basepath/$filename") or die "Can't write to $basepath/$filename: $!";
    print OUT qq(
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
		<title>CGI101: $topic Books for Web Developers</title>
<link rel="stylesheet" href="/css/subpage.css">
<link rel="stylesheet" href="/css/booklist.css">
</head>
<body>
<a name="top"></a>
<table width="100%" cellpadding="0" cellspacing="0">

<tr class="bluebox"> <!-- topbar -->
    <td align="LEFT">
<a href="/"><img src="/img05/tophome.gif" width="100" height="70" alt="Home" border="0"></a>
    </td>
    <td align="RIGHT">
<img src="/img05/topbar.gif" width="647" height="70" alt="CGI101.COM: Tools for Building A Better Website">
    </td>
</tr>

<tr > <!-- content -->
    <td colspan="2" class="content">
    
    <h2>$topic Books for Web Developers</h2>
    
    $content

<p class="small" align="center">
This page was updated at $gentime. <a href="howto/">More info</a> on how this page was built...
</p>
    
    </td>
</tr>

<tr class="bluebox">
    <td colspan="2" align="CENTER">
<a href="#top"><img src="/img05/top.gif" alt="Top of Page" width="74" height="35" border="0"></a>
<a href="/"><img src="/img05/home.gif" alt="Home" width="73" height="35" border="0"></a>
    </td>
</tr>

</table>
</body>
</html>
        );
    close(OUT);
}