commit 7dd886c5c1e506d3dfee5d4155cd236f3ce212a1
parent 7b17ed980bd09375bb3aa5133818ec31d7382ee6
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Thu, 19 Mar 2015 03:06:27 -0600
gen_static: add a function and simplify
Diffstat:
| M | gen_static |  |  | 137 | ++++++++++++++++++++++++++++++++++--------------------------------------------- | 
1 file changed, 59 insertions(+), 78 deletions(-)
diff --git a/gen_static b/gen_static
@@ -17,11 +17,12 @@ getopts("v", \%args);
 $| = 1 if ($args{v});
 
 my $cfg = get_config();
-my $dbh = get_dbh($cfg->{"http"}, undef, $args{v});
+my $dbh = get_dbh($cfg->{http}, undef, $args{v});
 
 my $work_dir =  $cfg->{http}{chroot} . $cfg->{http}{htdocs};
 print "info: working dir: $work_dir\n" if ($args{v});
 
+# rock a new template
 my $config = {
 	INTERPOLATE => 1,
 	POST_CHOMP => 1,
@@ -32,94 +33,30 @@ my $config = {
 my $template = Template->new($config)
 	|| die Template->error() . "\n";
 
-# xmkdir $cfg->{http} . "/manufacturers";
-my $sql = "select distinct manufacturer from products";
-# my $manuf_sth = $dbh->prepare($sql);
-# $manuf_sth->execute();
-# while (my ($manufacturer) = $manuf_sth->fetchrow_array()) {
-my $manufacturers = $dbh->selectall_arrayref($sql);
-my @manufacturer_list;
-for my $manufacturer (@$manufacturers) {
-	$manufacturer = $manufacturer->[0];
-	my $manuf_lc = lc($manufacturer);
-
-	$sql = "select part_num, manufacturer, description from products where manufacturer = ?";
-	my $part_nums = $dbh->selectall_arrayref($sql, undef, $manufacturer);
-	printf("info: manufacturers/$manuf_lc.html: %i products\n",
-		scalar @$part_nums) if ($args{v});
+# manufacturers.html, manufacturers/*
+my $manufacturers_sql = "select distinct manufacturer from products";
+my $parts_sql = "select part_num, manufacturer, description from products where manufacturer = ?";
+my $m = gen_link_page($manufacturers_sql, $parts_sql, "manufacturers", "Manufacturers");
 
-	my $vars = {
-		name => $manufacturer,
-		products => $part_nums,
-	};
-	$template->process("chart_list.tt", $vars,
-		"manufacturers/$manuf_lc.html"); # error checking!
-
-	push @manufacturer_list, [$manufacturer, $manuf_lc];
-}
-
-# manufacturers.html
-my $vars = {
-	name => "Manufacturers",
-	name_lc => "manufacturers",
-	num_manufacturers => scalar @manufacturer_list,
-	manufacturers => \@manufacturer_list,
-};
-print "info: manufacturers.html\n";
-$template->process("link_list.tt", $vars, "manufacturers.html")
-	|| die "template: " . $template->error() . "\n";
-
-
-# vendors/*.html
-$sql = "select distinct vendor from prices";
-my $vendors = $dbh->selectall_arrayref($sql);
-my @vendor_list;
-for my $vendor (@$vendors) {
-	$vendor = $vendor->[0];
-	my $vendor_lc = lc($vendor);
-
-	$sql = "select distinct part_num from prices where vendor = ?";
-	my $part_nums = $dbh->selectall_arrayref($sql, undef, $vendor);
-	printf("info: vendors/$vendor_lc.html: %i products\n",
-		scalar @$part_nums) if ($args{v});
-
-	my $vars = {
-		name => $vendor,
-		products => $part_nums,
-	};
-	$template->process("chart_list.tt", $vars,
-		"vendors/$vendor_lc.html"); # error checking!
-
-	push @vendor_list, [$vendor, uri_escape($vendor_lc)];
-}
-
-# vendors.html
-$vars = {
-	name => "Vendors",
-	name_lc => "vendors",
-	num_manufacturers => scalar @$vendors,
-	manufacturers => \@vendor_list,
-};
-print "info: vendors.html\n";
-$template->process("link_list.tt", $vars, "vendors.html")
-	|| die "template: " . $template->error() . "\n";
+# vendors.html, vendors/*
+my $vendor_sql = "select distinct vendor from prices";
+$parts_sql = "select distinct part_num from prices where vendor = ?";
+my $v = gen_link_page($vendor_sql, $parts_sql, "vendors", "Retailers");
 
 # index.html
-$sql = "select count(part_num) from products";
+my $sql = "select count(part_num) from products";
 my ($p) = $dbh->selectrow_array($sql);
 
-$sql = "select count(distinct vendor) from prices";
-my ($v) = $dbh->selectrow_array($sql);
-
+# get a list of products added within the last week
 my $time = time - (7 * 24 * 60 * 60);
 $sql = "select description from products where first_seen > $time";
 my $new_products = $dbh->selectall_arrayref($sql);
 
 my $n = @$new_products;
-# print "info: manufacturers, $p products ($n new), $v vendors\n" if ($args{v});
-$vars = {
+print "info: $m manufacturers, $p products ($n new), $v vendors\n" if ($args{v});
+my $vars = {
 	num_vendors => $v,
-	num_manufacturers => scalar @$manufacturers,
+	num_manufacturers => $m,
 	num_products => $p,
 	num_new => $n,
 	new_products => $new_products,
@@ -129,3 +66,47 @@ $template->process("index.tt", $vars, "index.html")
 	|| die "template: " . $template->error() . "\n";
 
 $dbh->disconnect();
+
+# generate a page of links to pages of products
+sub gen_link_page
+{
+	my $type_sql = shift;
+	my $parts_sql = shift;
+	my $dir_prefix = shift;
+	my $case_normal = shift;
+
+	xmkdir "$work_dir/$dir_prefix";
+
+	my $results = $dbh->selectall_arrayref($type_sql);
+	my @manicured_list;
+	for my $result (@$results) {
+		$result = $result->[0];
+		my $result_lc = lc($result);
+
+		my $part_nums = $dbh->selectall_arrayref($parts_sql, undef, $result);
+		printf("info: $dir_prefix/$result_lc.html:\t%i products\n",
+			scalar @$part_nums) if ($args{v});
+
+		my $vars = {
+			name => $dir_prefix,
+			products => $part_nums,
+		};
+		$template->process("chart_list.tt", $vars,
+			"$dir_prefix/$result_lc.html"); # error checking!
+
+		push @manicured_list, [$result, $result_lc];
+	}
+
+	# manufacturers.html
+	my $vars = {
+		name => $case_normal,
+		name_lc => $dir_prefix,
+		num_manufacturers => scalar @manicured_list,
+		manufacturers => \@manicured_list,
+	};
+	print "info: $dir_prefix.html\n" if ($args{v});
+	$template->process("link_list.tt", $vars, "$dir_prefix.html")
+		|| die "template: " . $template->error() . "\n";
+
+	return scalar @manicured_list;
+}