use strict; use warnings;
use utf8;
use XML::LibXML;

my $p = XML::LibXML->new;
$p->recover(2);
$p->load_ext_dtd(0);
$p->expand_entities(0);
$p->complete_attributes(0);

my @c;
for my $f (<grab/*.html>) {
	my $d = $p->parse_html_file($f);

	my ($prod_table) = $d->findnodes('//*[@id="priceBlock"]/table[@class="product"]');
	next unless $prod_table;

	my $savings = $prod_table->findvalue('//tr[contains(td[@class="productLabel"],"sparen")]/td[@class="price"]');
	next unless $savings;

	my $title = $d->findvalue('//div[@class="buying"]/b[@class="sans"]');

	my $listprice = $prod_table->findvalue('//tr[td[@class="productLabel"]]/td[@class="listprice"]');

	my $newprice = $prod_table->findvalue('//tr[td[@class="productLabel"]]/td/b[@class="price"]');

	push @c, [ mangle($f), $title, $newprice, $listprice, $savings ];
	push @{$c[-1]}, mangle($c[-1][-1]);
}
display();

sub mangle {
	my ($e) = @_;
	for ($e) {
		chomp;
		$_ = $1 if m!^grab/(.*)\.html$!;
		$_ = $1 if m!(\d+(?:[.,]\d+)?)%!;
		y!,!.!;
		$_ += 0 if m!^\d+(?:[.]\d+)?$!;
	}
	$e
}

sub display {
	for my $e (sort { $b->[-1] <=> $a->[-1] } @c) {
		print $e->[0],":\n\tTitel:\t", $e->[1],
		"\n\tPreis:\t", $e->[2],
		"\n\tAlt:\t", $e->[3],
		"\n\tSpar:\t", $e->[4], "\n";
	}
	print '-'x20,"\n";
}

