pricecharts

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

commit b3b92805d98adbdf83ed3c06162f8effd46fd226
parent 82c9894c54890e944601ec056f880dd7a6ecc1ce
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sat, 28 Feb 2015 18:04:08 -0700

PriceChart: add trunc_line function

Diffstat:
MPriceChart.pm | 32+++++++++++++++++++++++++-------
Mprice_scraper.pl | 4+++-
Mproduct_scraper.pl | 8+++-----
3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/PriceChart.pm b/PriceChart.pm @@ -4,7 +4,7 @@ use DBI; use Exporter; @ISA = ("Exporter"); -@EXPORT = qw(get_config get_dom new_ua get_log get_dbh); +@EXPORT = qw(get_config get_dom get_log get_dbh trunc_line new_ua); sub get_config @@ -74,10 +74,8 @@ sub get_dom my $resp = $ua->get($url); if ($resp->is_success) { - if (length($url) > 55) { - $url = "..." . substr($url, length($url) - 55); - } - print "info: GET $url " . $resp->status_line . "\n" if ($verbose); + my $short_url = trunc_line($url, length($resp->status_line) + 11, 1); + print "info: GET " . $resp->status_line . " $short_url\n" if ($verbose); return HTML::Grabber->new(html => $resp->decoded_content); } @@ -99,10 +97,11 @@ sub new_ua $ua->default_header("User-Agent" => $cfg->{"user_agent"}); if ($verbose) { - print "info: new_ua: user agent http headers are:\n"; + print "info: new_ua: http headers are:\n"; my $headers = $ua->default_headers; for (sort keys %$headers) { - print " $_: " . $headers->{$_}. "\n"; + my $header = trunc_line($headers->{$_}, length($_) + 16); + print " $_: $header\n"; } } @@ -125,4 +124,23 @@ sub get_log return $log; } +sub trunc_line +{ + my $line = shift; + my $prefix = shift || 0; + my $front = shift || 0; + + my ($wchar) = Term::ReadKey::GetTerminalSize(); + if (length($line) < ($wchar - $prefix - 3)) { + return $line; + } + + if ($front) { + my $chopped = substr($line, length($line) - ($wchar - $prefix - 3)); + return "..." . $chopped; + } + my $chopped = substr($line, 0, ($wchar - $prefix - 3)); + return $chopped . "..."; +} + 1; diff --git a/price_scraper.pl b/price_scraper.pl @@ -10,6 +10,7 @@ use List::Util qw(min); use LWP::Simple; use PriceChart; use POSIX; +use Term::ReadKey; use URI::Escape; @@ -109,7 +110,8 @@ for my $vendor (sort keys %{$cfg->{"vendors"}}) { if (length($desc) > 50) { $desc = substr($desc, 0, 50) . "..."; } - print "info: $vendor: $desc\n"; + my $desc_s = trunc_line($desc, length($vendor) + 9); + print "info: $vendor: $desc_s\n"; } } diff --git a/product_scraper.pl b/product_scraper.pl @@ -11,6 +11,7 @@ use Getopt::Std; use HTML::Grabber; use LWP::Simple; use PriceChart; +use Term::ReadKey; my %args; @@ -217,12 +218,9 @@ sub mem_exp_scrape_thumbnail return undef; } - my $tmp_desc = $desc; - if (length($tmp_desc) > 50) { - $tmp_desc = substr($tmp_desc, 0, 50) . "..."; - } + my $shortened_desc = trunc_line($desc, length($info_hdr) + 2); print "$info_hdr: $brand $part_num\n" if ($args{v}); - print "$info_hdr: $tmp_desc\n" if ($args{v}); + print "$info_hdr: $shortened_desc\n" if ($args{v}); return ($brand, $part_num, $desc); }