pricecharts

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

commit 65978eb00a350268dc6e016d80eb71e6825c6de0
parent 1d9830ea94075d991ad8fd4410679aaf07970970
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Thu, 13 Nov 2014 02:10:11 -0700

use PriceChart::Shared instead of shared

Diffstat:
AShared.pm | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgen_index.pl | 3+--
Mgen_svg.pl | 3+--
Mprice_scraper.pl | 3+--
Mproduct_scraper.pl | 3+--
Msearch.pl | 3+--
Dshared.pm | 74--------------------------------------------------------------------------
Mupdate_vendors.pl | 3+--
8 files changed, 79 insertions(+), 86 deletions(-)

diff --git a/Shared.pm b/Shared.pm @@ -0,0 +1,73 @@ +package PriceChart::Shared; + +use DBI; +use Exporter; +use POSIX; + +@ISA = ("Exporter"); +@EXPORT = qw(get_config get_dom get_ua get_log get_dbh); + + +sub get_config +{ + my $parser = Config::Grammar->new({ + _vars => [ + 'user_agent', + 'email', + 'smtp', + ], + }); + my $cfg_file = "/etc/pricechart.cfg"; + return $parser->parse($cfg_file) or die "error: $parser->{err}\n"; +} + +sub get_dbh +{ + my $db_dir = "/var/www/db"; + mkdir $db_dir; + + my $dbh = DBI->connect( + "dbi:SQLite:dbname=$db_dir/pricechart.db", + "", + "", + { RaiseError => 1 } + ) or die $DBI::errstr; + return $dbh; +} + +sub get_dom +{ + my $url = shift; + my $ua = shift; + + my $resp = $ua->get($url); + if (! $resp->is_success) { + print "getting $url failed: " . $resp->status_line . "\n"; + return undef; + } + return HTML::Grabber->new(html => $resp->decoded_content); +} + +sub get_ua +{ + my $cfg = shift; + + my $ua = LWP::UserAgent->new(agent => $cfg->{user_agent}); + $ua->default_header("Accept" => "*/*"); + + return $ua; +} + +sub get_log +{ + my $file = shift; + my $log_dir = "/var/www/logs/pricechart"; + + mkdir $log_dir; + open my $log, ">>", "$log_dir/$file.log" || die "$!"; + + print $log strftime "%b %e %Y %H:%M ", localtime; + return $log; +} + +1; diff --git a/gen_index.pl b/gen_index.pl @@ -4,10 +4,9 @@ use strict; use warnings; use File::Copy; +use PriceChart::Shared; use Template; -use shared; - my $dbh = get_dbh(); diff --git a/gen_svg.pl b/gen_svg.pl @@ -6,8 +6,7 @@ use warnings; use Getopt::Std; use SVG; use POSIX; - -use shared; +use PriceChart::Shared; my %args; diff --git a/price_scraper.pl b/price_scraper.pl @@ -7,8 +7,7 @@ use Config::Grammar; use Getopt::Std; use HTML::Grabber; use LWP::Simple; - -use shared; +use PriceChart::Shared; my %args; diff --git a/product_scraper.pl b/product_scraper.pl @@ -9,8 +9,7 @@ use Email::Send; use Getopt::Std; use HTML::Grabber; use LWP::Simple; - -use shared; +use PriceChart::Shared; my %args; diff --git a/search.pl b/search.pl @@ -6,12 +6,11 @@ use warnings; use FCGI; use Getopt::Std; use Template; +use PriceChart::Shared; use Proc::Daemon; use Unix::Syslog qw(:macros :subs); use URI::Escape; -use shared; - my %args; getopts("d", \%args); diff --git a/shared.pm b/shared.pm @@ -1,74 +0,0 @@ -#!/usr/bin/env perl - -package shared; -use DBI; -use Exporter; -use POSIX; - -@ISA = ("Exporter"); -@EXPORT = qw(get_config get_dom get_ua get_log get_dbh); - - -sub get_config -{ - my $parser = Config::Grammar->new({ - _vars => [ - 'user_agent', - 'email', - 'smtp', - ], - }); - my $cfg_file = "/etc/pricechart.cfg"; - return $parser->parse($cfg_file) or die "error: $parser->{err}\n"; -} - -sub get_dbh -{ - my $db_dir = "/var/www/db"; - mkdir $db_dir; - - my $dbh = DBI->connect( - "dbi:SQLite:dbname=$db_dir/pricechart.db", - "", - "", - { RaiseError => 1 } - ) or die $DBI::errstr; - return $dbh; -} - -sub get_dom -{ - my $url = shift; - my $ua = shift; - - my $resp = $ua->get($url); - if (! $resp->is_success) { - print "getting $url failed: " . $resp->status_line . "\n"; - return undef; - } - return HTML::Grabber->new(html => $resp->decoded_content); -} - -sub get_ua -{ - my $cfg = shift; - - my $ua = LWP::UserAgent->new(agent => $cfg->{user_agent}); - $ua->default_header("Accept" => "*/*"); - - return $ua; -} - -sub get_log -{ - my $file = shift; - my $log_dir = "/var/www/logs/pricechart"; - - mkdir $log_dir; - open my $log, ">>", "$log_dir/$file.log" || die "$!"; - - print $log strftime "%b %e %Y %H:%M ", localtime; - return $log; -} - -1; diff --git a/update_vendors.pl b/update_vendors.pl @@ -4,8 +4,7 @@ use strict; use warnings; use Getopt::Std; - -use shared; +use PriceChart::Shared; my %args;