pricecharts

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

commit 35fa47ff1b2239b70b90e6baeaaa0452d8c649be
parent 066db85643c2aa5aaa7bebfd33d9a38542b9ca01
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sun, 12 Oct 2014 22:28:23 -0600

shared: add common function to get user agent

Diffstat:
Mprice_scraper.pl | 4+---
Mproduct_scraper.pl | 4+---
Mshared.pm | 11++++++++++-
3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/price_scraper.pl b/price_scraper.pl @@ -18,6 +18,7 @@ getopts('f:np:v', \%args); my $cfg = get_config($args{f}); my $dbh = get_dbh($cfg); +my $ua = get_ua($cfg); $| = 1 if ($args{v}); @@ -47,9 +48,6 @@ $dbh->do("create table if not exists prices(" . "duration int, " . "primary key(date, part_num, vendor, price))"); -my $ua = LWP::UserAgent->new(agent => $cfg->{general}{user_agent}); -$ua->default_header('Accept' => '*/*'); - print $log strftime "%b %e %Y %H:%M ", localtime; printf $log "%-15s [", $part_num; diff --git a/product_scraper.pl b/product_scraper.pl @@ -20,6 +20,7 @@ getopts("vf:", \%args); my $cfg = get_config($args{f}); my $dbh = get_dbh($cfg); +my $ua = get_ua($cfg); $| = 1 if ($args{v}); @@ -32,9 +33,6 @@ $dbh->do("create table if not exists products(" . "last_seen int, " . "last_scraped int)") or die $DBI::errstr; -my $ua = LWP::UserAgent->new(agent => $cfg->{general}{user_agent}); -$ua->default_header("Accept" => "*/*"); - my $email; # diff --git a/shared.pm b/shared.pm @@ -5,7 +5,7 @@ use Config::Grammar; use Exporter; @ISA = ("Exporter"); -@EXPORT = ("get_dom", "get_config", "get_dbh"); +@EXPORT = ("get_dom", "get_config", "get_dbh", "get_ua"); sub get_dom { @@ -67,4 +67,13 @@ sub get_dbh return $dbh; } +sub get_ua +{ + my $cfg = shift; + + my $ua = LWP::UserAgent->new(agent => $cfg->{general}{user_agent}); + $ua->default_header("Accept" => "*/*"); + return $ua; +} + 1;