pricecharts

track prices of consumer electronics
Log | Files | Refs | README

commit 0fc800b6dd2de7e042e3d0c8a8c6a1d4edba9c30
parent 7ecdaf6fcc09f025e48095870217cb94418538f1
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sat, 25 Apr 2015 01:07:00 -0600

pc_html: first steps at special character handling

Diffstat:
Mpc_html | 92+++++++++++++++++++++++++++++++++----------------------------------------------
Mtt/coarse_list.tt | 59++++++++++++++++++++---------------------------------------
Mtt/fine_list.tt | 47++++++++++++++++++++++-------------------------
Mtt/index.tt | 41+++++++++++++++++++----------------------
Mtt/product.tt | 44++++++++++++++++++++++++--------------------
Mtt/summary.tt | 5-----
6 files changed, 123 insertions(+), 165 deletions(-)

diff --git a/pc_html b/pc_html @@ -24,14 +24,6 @@ $| = 1 if ($args{v}); my $cfg = get_config(); my $dbh = get_dbh($cfg->{http}, undef, $args{v}); -my $clause = $args{a} ? "" : "where svg_stale = 1"; -my ($n) = $dbh->selectrow_array("select count(*) from products $clause"); -if ($n < 1) { - print "info: nothing stale\n" if ($args{v}); - $dbh->disconnect(); - exit; -} - my $work_dir = $cfg->{http}{chroot} . $cfg->{http}{htdocs}; my $svg_dir = $work_dir . "/svg"; print "info: work, svg dirs $work_dir\{,svg\}\n" if ($args{v}); @@ -131,27 +123,27 @@ generate_folder($stale_list, $types, $products_fine, "Types", $coarse, $summary) # # products # +print "info: products: " if ($args{v}); + $stale_clause = $args{a} ? "" : "where svg_stale = 1"; my $sql = "select * from products $stale_clause"; my $products = $dbh->selectall_hashref($sql, "part_num"); -$n = scalar keys %$products; -print "info: products: " if ($args{v}); +my $n = scalar keys %$products; while (my ($part_num, $row) = each %$products) { - my $part_num_lc = lc($part_num); - my $manufacturer_lc = lc($row->{manufacturer}); + my $part_link = linkify($part_num); + my $manuf_link = linkify($row->{manufacturer}); spin() if ($args{v}); $row->{description} = get_description($row->{manufacturer}, $row->{part_num}); $template->process("product.tt", $row, - "products/$manufacturer_lc/$part_num_lc.html") + "products/$manuf_link/$part_link.html") || die "template: " . $template->error() . "\n"; } print "\b$n processed\n" if ($args{v}); - # # index # @@ -168,78 +160,73 @@ my ($p, $m) = $dbh->selectrow_array($sql); $sql = "select count(*) from retailers"; my ($r) = $dbh->selectrow_array($sql); -my $vars = { - nret => $r, nmanuf => $m, nprod => $p, nnew => scalar @$new, - news => $new, nupd => scalar @$upd, upds => $upd -}; +my $vars = { nret => $r, nmanuf => $m, nprod => $p, news => $new, upds => $upd }; print "info: index: $p products, $m manufacturers, $r retailers\n" if ($args{v}); $template->process("index.tt", $vars, "index.html") or die "template: " . $template->error() . "\n"; - # # svg # +print "info: svg: " if ($args{v}); + my ($left, $center, $right, $top, $middle, $bottom) = (3, 957, 40, 15, 150, 20); my $width = $right + $center + $left; my $height = $top + $middle + $bottom; -my $where_clause = $args{a} ? "" : "where svg_stale = 1"; -$sql = "select manufacturer, part_num from products $where_clause"; -my $parts_sth = $dbh->prepare($sql); - $sql = "select distinct retailer from prices where part_num = ? and manufacturer = ?"; my $retailer_sth = $dbh->prepare($sql); -$sql = "select date, price from prices where " . - "part_num = ? and retailer = ? order by date"; +$sql = qq{select date, price from prices where part_num = ? and retailer = ? + order by date}; my $point_sth = $dbh->prepare($sql); -$parts_sth->execute(); -print "info: svg: " if ($args{v}); +$sql = qq{select min(date), max(date), min(price), max(price) from prices + where manufacturer = ? and part_num = ?}; +my $extremes_sth = $dbh->prepare($sql); + +$sql = "select url, color from retailers where name = ?"; +my $ret_info_sth = $dbh->prepare($sql); + my ($raw_total, $rendered_total, $points, $series) = (0, 0, 0, 0); -while (my ($manufacturer, $part_num) = $parts_sth->fetchrow_array()) { + +my $where_clause = $args{a} ? "" : "where svg_stale = 1"; +my $parts_sql = "select manufacturer, part_num from products $where_clause"; +for (@{$dbh->selectall_arrayref($parts_sql)}) { + my ($manufacturer, $part_num) = @$_; + $raw_total++; spin() if ($args{v}); - my %series; - $sql = qq{select min(date), max(date), min(price), max(price) - from prices where manufacturer = ? and part_num = ?}; my ($x_min, $x_max, $y_min, $y_max) = - $dbh->selectrow_array($sql, undef, $manufacturer, $part_num); + $dbh->selectrow_array($extremes_sth, undef, $manufacturer, $part_num); next unless (defined $x_min); - # each series on the chart represents a retailers prices + my %series; $retailer_sth->execute($part_num, $manufacturer); while (my ($retailer) = $retailer_sth->fetchrow_array()) { - $sql = "select url, color from retailers where name = ?"; - my ($url, $color) = $dbh->selectrow_array($sql, undef, $retailer); + my ($url, $color) = + $dbh->selectrow_array($ret_info_sth, undef, $retailer); # xlink:href's don't like raw ampersands $url =~ s/&/&amp;/g; $series{$retailer}{url} = $url; $series{$retailer}{color} = $color; - - $point_sth->execute($part_num, $retailer); - $series{$retailer}{data} = $point_sth->fetchall_arrayref(); + $series{$retailer}{data} = + $dbh->selectall_arrayref($point_sth, undef, $part_num, $retailer); $points += scalar @{$series{$retailer}{data}}; $series++; } my $svg = make_svg(\%series, $x_min, $x_max, $y_min, $y_max, $part_num); - # all links lower case - my $part_num_cased = $part_num; - $part_num = lc($part_num); + my $manufacturer_dir = linkify($manufacturer); + my $part_link = linkify($part_num); + make_path("$svg_dir/$manufacturer_dir", { verbose => $args{v} }); - # giant hack, if the part number has / in it, make some directories - if ($part_num =~ /\//) { - my $needed_dirs = substr($part_num, 0, rindex($part_num, '/')); - make_path("$svg_dir/$needed_dirs", { verbose => $args{v} }); - } - open my $svg_fh, ">", "$svg_dir/$part_num.svg" or - die "couldn't open $svg_dir/$part_num: $!"; + my $svg_path = "$svg_dir/$manufacturer_dir/$part_link.svg"; + open my $svg_fh, ">", "$svg_path" or die "couldn't open $svg_path: $!"; # XXX: not sure how to add this programatically, hack around for now my @buf = split("\n", $svg->xmlify); @@ -259,7 +246,6 @@ $dbh->do("update products set svg_stale = 0"); $dbh->disconnect(); - # # generate an entire tree of html structure # @@ -314,7 +300,7 @@ sub linkify my $type = shift; my $type_link = lc($type); - $type_link =~ s/ /_/; + $type_link =~ s/[ #\/]/_/g; return $type_link; } @@ -341,7 +327,6 @@ sub get_description return $best; } - # # make a new svg with provided coordinate and label data # @@ -414,9 +399,6 @@ sub make_svg my $retailer_id = lc($retailer); $retailer_id =~ s/ /_/; - my $url = $values->{url}; - my $color = $values->{color}; - my (@xs, @ys, @pts); for (@{$values->{data}}) { my ($x, $y) = @$_; @@ -436,6 +418,8 @@ sub make_svg $defs->tag("path", "d" => $d, id => "path_$retailer_id"); } + my ($url, $color) = ($values->{url}, $values->{color}); + # the line, points, and label can be grouped under one anchor my $anchor = $svg->anchor(-href => $url . $part_num, target => "new_window"); diff --git a/tt/coarse_list.tt b/tt/coarse_list.tt @@ -1,59 +1,40 @@ [% WRAPPER wrapper.tt %] -[% PERL %] - use Lingua::EN::Inflect qw(PL); - # all url references are lower case - my $dir_prefix = lc($stash->get("name")); - $stash->set("dir_prefix", $dir_prefix); - - my $total = scalar(keys %{$stash->get("list")}); - $stash->set("num", $total); - $stash->set("i", -1); - $stash->set("boundary", POSIX::ceil($total / 3)); -[% END %] - <h1>[% name %] ([% num %])</h1> + <h1>[% name %] ([% list.keys.size %])</h1> + <div class="thirds_column"> + <ul> + [% i = 0 %] [% FOREACH item IN list.keys.sort %] + [% item_link = item.lower.replace('[ #\/]', '_') %] [% PERL %] - # all url references are lower case - my $link = lc $stash->get("item"); - $link =~ s/ /_/; - $stash->set("link", $link); - - my $i = $stash->get("i"); - $stash->set("i", ++$i); - + my $link = $stash->get("item_link"); my ($logo) = glob("logo/$link.*"); $stash->set("logo", $logo); [% END %] - [% IF (i % boundary) == 0 %] - [% IF i != 0 %] - </ul> - </div> - [% END %] + + [% i = i + 1 %] + [% IF (i % (list.keys.size / 3)) == 0 %] + </ul> + </div> <div class="thirds_column"> <ul> [% END %] <li><div class="list_item"> - <a href="/[% dir_prefix %]/[% link %].html"> - <img alt="[% item %]" class="[% dir_prefix %]" + [% name_link = name.lower.replace('[ #\/]', '_') %] + <a href="/[% name_link %]/[% item_link %].html"> + <img alt="[% item %]" class="[% name_link %]" src="/[% logo %]" /> - </a> + </a><br> - <br><div class="breakdown"> + <div class="breakdown"> [% FOREACH type IN list.$item.keys %] - [% PERL %] - my $type = $stash->get("type"); - my $count = $stash->get("list.\$item.\$type.count"); - my $type_link = lc $type; - $type_link =~ s/ /_/; - $stash->set("type_link", $type_link); - $stash->set("type_pl", PL($type, $count)); - [% END %] [% list.$item.$type.count %] - <a href="/[% dir_prefix %]/[% link %]/[% type_link %].html"> - [% type_pl %]</a>, + + [% type_link = type.lower.replace('[ #\/]', '_') %] + <a href="/[% name_link %]/[% item_link %]/[% type_link %].html"> + [% type %]</a>, [% END %] </div></div> [% END %] diff --git a/tt/fine_list.tt b/tt/fine_list.tt @@ -1,40 +1,37 @@ [% WRAPPER wrapper.tt %] +[% name_link = name.lower.replace('[ #\/]', '_') %] [% PERL %] - my $name_lc = lc($stash->get("name")); - $stash->set("name_lc", $name_lc); - - my $num = scalar @{$stash->get("products")}; - $stash->set("num", $num); - - $name_lc =~ s/ /_/; - my ($logo) = glob("logo/$name_lc*"); + my $name_html = $stash->get("name_link"); + my ($logo) = glob("logo/$name_html*"); $stash->set("logo", $logo); [% END %] <h1><img alt="[% name %]" class="logo_small" src="/[% logo %]"/> - [% num %] [% type %]</h1> - [% FOREACH product IN products %] - [% PERL %] - my $manufacturer = lc $stash->get("product.0"); - $stash->set("manufacturer_lc", $manufacturer); - my $part_num = $stash->get("product.1"); - $stash->set("part_lc", lc($part_num)); + [% products.size %] [% type %]</h1> - my ($logo) = glob("logo/$manufacturer*"); - $stash->set("logo", $logo); - [% END %] + [% FOREACH product IN products %] <div class="product"> - <!-- make the manufacturer logo into a link --> - <a href="/manufacturers/[% manufacturer_lc %].html"> + [% manuf_html = product.0.lower.replace('[ #\/]', '_') %] + [% part_link = product.1.lower.replace('[ #\/]', '_') %] + + [% PERL %] + my $manuf_html = $stash->get("manuf_html"); + my ($logo) = glob("logo/$manuf_html*"); + $stash->set("logo", $logo); + [% END %] + + [%# make the manufacturer logo into a link %] + <a href="/manufacturers/[% manuf_html %].html"> <img alt="[% product.0 %]" class="logo_small" src="/[% logo %]"/></a> - <!-- display the description --> + [%# display the description %] [% product.2 %] - <!-- inline link to the products page, in parenthesis --> - (<a href="/products/[% manufacturer_lc %]/[% part_lc %].html">[% product.1 %]</a>)<br> - <!-- show the chart --> - <object data="/svg/[% part_lc %].svg" type="image/svg+xml"> + [%# inline link to the products page, in parenthesis %] + (<a href="/products/[% manuf_html %]/[% part_link %].html">[% product.1 %]</a>)<br> + + [%# show the chart %] + <object data="/svg/[% manuf_html %]/[% part_link %].svg" type="image/svg+xml"> </object> </div> [% END %] diff --git a/tt/index.tt b/tt/index.tt @@ -35,36 +35,33 @@ </div> <div class="column"> - <h1>New Products ([% nnew %])</h1> + <h1>New Products ([% news.size %])</h1> <ul> - [% FOREACH new IN news %] - [% PERL %] - my $part_num = $stash->get("new.1"); - $stash->set("part_lc", lc($part_num)); - my $manufacturer = $stash->get("new.0"); - $stash->set("manuf_lc", lc($manufacturer)); - [% END %] + [% FOREACH new IN news %] + [% manuf_link = new.0.lower.replace('[ #\/]', '_') %] + [% part_link = new.1.lower.replace('[ #\/]', '_') %] + <li><img class="logo_small" alt="[% new.0 %]" - src="/logo/[% manuf_lc %].svg"></img> - <a href="/products/[% manuf_lc %]/[% part_lc %].html">[% new.1 %]</a> - [% END %] + src="/logo/[% manuf_link %].svg"></img> + + <a href="/products/[% manuf_link %]/[% part_link %].html"> + [% new.1 %]</a> + [% END %] </ul> </div> <div class="column"> - <h1>Recently Updated ([% nupd %])</h1> + <h1>Recently Updated ([% upds.size %])</h1> <ul> - [% FOREACH upd IN upds %] - [% PERL %] - my $part_num = $stash->get("upd.1"); - $stash->set("part_lc", lc($part_num)); - my $manufacturer = $stash->get("upd.0"); - $stash->set("manuf_lc", lc($manufacturer)); - [% END %] + [% FOREACH upd IN upds %] + [% manuf_link = upd.0.lower.replace('[ #\/]', '_') %] + [% part_link = upd.1.lower.replace('[ #\/]', '_') %] + <li><img class="logo_small" alt="[% upd.0 %]" - src="/logo/[% manuf_lc %].svg"></img> - <a href="/products/[% manuf_lc %]/[% part_lc %].html">[% upd.1 %]</a> - [% END %] + src="/logo/[% manuf_link %].svg"></img> + <a href="/products/[% manuf_link %]/[% part_link %].html"> + [% upd.1 %]</a> + [% END %] </ul> </div> [% END %] diff --git a/tt/product.tt b/tt/product.tt @@ -1,29 +1,33 @@ [% WRAPPER wrapper.tt %] [% PERL %] - my $manuf = $stash->get("manufacturer"); - $stash->set("manufacturer_lc", lc($manuf)); - my $part_num = $stash->get("part_num"); - $stash->set("part_num_lc", lc($part_num)); - + # drop the exact time on this somehow my $time_str = scalar localtime($stash->get('first_seen')); $stash->set("first_seen_proc", $time_str); - # drop the exact time on this somehow $time_str = scalar localtime($stash->get('last_seen')); $stash->set("last_seen_proc", $time_str); [% END %] - <h1><a href="/manufacturers/[% manufacturer_lc %].html"> + [% manuf_link = manufacturer.lower.replace('[ #\/]', '_') %] + <h1><a href="/manufacturers/[% manuf_link %].html"> <img class="logo_small" alt="[% manufacturer %]" - src="/logo/[% manufacturer_lc %].svg"/></a> - [% part_num %]</h1> - <div class="product"> - <table> - <tr><td>Manufacturer:</td><td>[% manufacturer %]</td></tr> - <tr><td>Description:</td><td>[% description %]</td></tr> - <tr><td>Type:</td><td>[% type %]</td></tr> - <tr><td>First Seen:</td><td>[% first_seen_proc %]</td></tr> - <tr><td>Last Seen:</td><td>[% last_seen_proc %]</td></tr> - </table> - <object data="/svg/[% part_num_lc %].svg" type="image/svg+xml"> - </object> - </div> + src="/logo/[% manuf_link %].svg"/></a> + [% part_num %]</h1> + + [% BLOCK row %] + <tr> + <td>[% first %]:</td> + <td>[% second %]</td> + </tr> + [% END %] + + <table> + [% PROCESS row first='Manufacturer' second=manufacturer %] + [% PROCESS row first='Description' second=description %] + [% PROCESS row first='Type' second=type %] + [% PROCESS row first='First Seen' second=first_seen_proc %] + [% PROCESS row first='Last Seen' second=last_seen_proc %] + </table> + + [% part_link = part_num.lower.replace('[ #\/]', '_') %] + <object data="/svg/[% manuf_link %]/[% part_link %].svg" type="image/svg+xml"> + </object> [% END %] diff --git a/tt/summary.tt b/tt/summary.tt @@ -1,9 +1,4 @@ [% WRAPPER wrapper.tt %] -[% PERL %] - # all url references are lower case - my $dir_prefix = lc($stash->get("name")); - $stash->set("name_lc", $dir_prefix); -[% END %] <h1><img alt="[% name %]" class="logo_small" src="/logo/[% logo %]"/></h1> <ul>