pricecharts

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

commit 8646d7990eef48a1081ecf89ed63bdd34210057e
parent 419ceaa1cfbfaf2ce3fb8951fe24aedc59e6eb48
Author: Kyle R W Milz <kyle@getaddrinfo.net>
Date:   Wed, 13 Aug 2014 01:24:47 -0600

price_scraper: move grammar parser into shared pm

Diffstat:
MShared.pm | 23++++++++++++++++++++++-
Mprice_scraper.pl | 31++++---------------------------
2 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/Shared.pm b/Shared.pm @@ -1,10 +1,11 @@ #!/usr/bin/env perl package Shared; +use Config::Grammar; use Exporter; @ISA = ("Exporter"); -@EXPORT = ("get_dom"); +@EXPORT = ("get_dom", "get_config"); sub get_dom { @@ -19,4 +20,24 @@ sub get_dom return HTML::Grabber->new(html => $resp->decoded_content); } +sub get_config +{ + my $cfg_file = shift; + my $parser = Config::Grammar->new({ + _sections => ['vendors', 'paths'], + vendors => { + # vendor regular expression + _sections => ['/[A-Za-z ]+/'], + '/[A-Za-z ]+/' => { + _vars => ['search_uri', 'reg_price', 'sale_price', 'color'], + }, + }, + paths => { + _vars => ['http', 'log'], + }, + }); + + return $parser->parse($cfg_file) or die "ERROR: $parser->{err}\n"; +} + 1; diff --git a/price_scraper.pl b/price_scraper.pl @@ -3,7 +3,6 @@ use strict; use warnings; -use Config::Grammar; use Data::Dumper; use DBI; use Getopt::Std; @@ -16,32 +15,10 @@ use POSIX; my %args; getopts('f:np:v', \%args); -my $parser = Config::Grammar->new({ - _sections => ['vendors', 'paths'], - vendors => { - # vendor regular expression - _sections => ['/[A-Za-z ]+/'], - '/[A-Za-z ]+/' => { - _vars => ['search_uri', 'reg_price', 'sale_price', 'color'], - }, - }, - paths => { - _vars => ['http', 'log'], - }, -}); - -my $cfg_file; -if ($args{f}) { - $cfg_file = $args{f}; -} -elsif (-e "/etc/price_scraper.cfg") { - $cfg_file = "/etc/price_scraper.cfg"; -} -elsif (-e "price_scraper.cfg") { - $cfg_file = "price_scraper.cfg"; -} - -my $cfg = $parser->parse($cfg_file) or die "ERROR: $parser->{err}\n"; +my $cfg; +$cfg = get_config("/etc/price_scraper.cfg") if (-e "/etc/price_scraper.cfg"); +$cfg = get_config("price_scraper.cfg") if (-e "price_scraper.cfg"); +$cfg = get_config($args{f}) if ($args{f}); my $dbh = DBI->connect( "dbi:SQLite:dbname=pricechart.db",