pricecharts

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

commit 20ad9e24d89a1de065f95816c8c37f2b5501e20f
parent 43b45328b60f5cc8502db7a78340359651edd67e
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Tue, 11 Nov 2014 12:53:03 -0700

only parse config file in modules that need it

Diffstat:
Mprice_scraper.pl | 5++++-
Mproduct_scraper.pl | 7+++++--
Mshared.pm | 53++++++++++++++++++++++++++++-------------------------
Mupdate_vendors.pl | 4++++
4 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/price_scraper.pl b/price_scraper.pl @@ -3,7 +3,9 @@ use strict; use warnings; +use Config::Grammar; use Getopt::Std; +use LWP::Simple; use shared; @@ -13,7 +15,8 @@ getopts("nv", \%args); $| = 1 if ($args{v}); -my $ua = get_ua(); +my $cfg = get_config(); +my $ua = get_ua($cfg); my $dbh = get_dbh(); # pick the oldest product diff --git a/product_scraper.pl b/product_scraper.pl @@ -3,10 +3,12 @@ use strict; use warnings; -use Getopt::Std; +use Config::Grammar; use Email::Simple; use Email::Send; +use Getopt::Std; use HTML::Grabber; +use LWP::Simple; use shared; @@ -16,7 +18,8 @@ getopts("v", \%args); $| = 1 if ($args{v}); -my $ua = get_ua(); +my $cfg = get_config(); +my $ua = get_ua($cfg); my $dbh = get_dbh(); srand; diff --git a/shared.pm b/shared.pm @@ -1,42 +1,42 @@ #!/usr/bin/env perl package shared; -use Config::Grammar; use DBI; use Exporter; use HTML::Grabber; -use LWP::Simple; use POSIX; @ISA = ("Exporter"); -@EXPORT = qw(get_dom get_ua get_log get_dbh vprint vprintf %args $cfg); +@EXPORT = qw(get_config get_dom get_ua get_log get_dbh); -my $cfg_file = "/etc/pricechart.cfg"; -my $parser = Config::Grammar->new({ - _sections => ['vendors', 'general'], - vendors => { - # vendor regular expression - _sections => ['/[A-Za-z ]+/'], - '/[A-Za-z ]+/' => { +sub get_config +{ + my $parser = Config::Grammar->new({ + _sections => ['vendors', 'general'], + vendors => { + # vendor regular expression + _sections => ['/[A-Za-z ]+/'], + '/[A-Za-z ]+/' => { + _vars => [ + 'search_uri', + 'reg_price', + 'sale_price', + 'color' + ], + }, + }, + general => { _vars => [ - 'search_uri', - 'reg_price', - 'sale_price', - 'color' + 'user_agent', + 'email', + 'smtp', ], }, - }, - general => { - _vars => [ - 'user_agent', - 'email', - 'smtp', - ], - }, -}); - -our $cfg =$parser->parse($cfg_file) or die "error: $parser->{err}\n"; + }); + my $cfg_file = "/etc/pricechart.cfg"; + return $parser->parse($cfg_file) or die "error: $parser->{err}\n"; +} sub get_dbh { @@ -67,8 +67,11 @@ sub get_dom sub get_ua { + my $cfg = shift; + my $ua = LWP::UserAgent->new(agent => $cfg->{general}{user_agent}); $ua->default_header("Accept" => "*/*"); + return $ua; } diff --git a/update_vendors.pl b/update_vendors.pl @@ -3,10 +3,14 @@ use strict; use warnings; +use Config::Grammar; + use shared; +my $cfg = get_config(); my $dbh = get_dbh(); + $dbh->do("create table if not exists vendors(" . "name text not null primary key, " . "search_url not null, " .