5 |
use DBI; |
use DBI; |
6 |
use HTML::TreeBuilder; |
use HTML::TreeBuilder; |
7 |
|
|
8 |
my $debug=1; |
my $debug=0; |
9 |
|
|
10 |
my $url = 'http://custom.marketwatch.com/custom/alliance/ftmw/invrel.asp?siteid=!plivadd-0773-4F6D-DHID-QN1112NFTD0X&symb=PLVD'; |
my $url = 'http://custom.marketwatch.com/custom/alliance/ftmw/invrel.asp?siteid=!plivadd-0773-4F6D-DHID-QN1112NFTD0X&symb=PLVD'; |
11 |
|
|
22 |
} |
} |
23 |
|
|
24 |
sub print_val { |
sub print_val { |
|
return if (! $debug); |
|
25 |
foreach (keys %val) { |
foreach (keys %val) { |
26 |
print "$_: $val{$_}\n"; |
print "$_: $val{$_}\n"; |
27 |
} |
} |
41 |
my $close_time='21:21:21 CET'; |
my $close_time='21:21:21 CET'; |
42 |
|
|
43 |
sub insert { |
sub insert { |
44 |
|
# fix UNCH to 0 |
45 |
|
foreach (keys %val) { |
46 |
|
$val{$_} =~ s/^\s+//; |
47 |
|
$val{$_} =~ s/\s+$//; |
48 |
|
$val{$_} = 0 if ($val{$_} eq "UNCH"); |
49 |
|
} |
50 |
|
|
51 |
my $sth = $dbh->prepare("select count(date) from stocks where date='".$val{date}."'"); |
my $sth = $dbh->prepare("select count(date) from stocks where date='".$val{date}."'"); |
52 |
$sth->execute(); |
$sth->execute(); |
53 |
my ($nr) = $sth->fetchrow_array; |
my ($nr) = $sth->fetchrow_array; |
55 |
my $sql="insert into stocks values ('$val{date}','LSE',$val{curr},$val{change},$val{high},$val{low},$val{open})"; |
my $sql="insert into stocks values ('$val{date}','LSE',$val{curr},$val{change},$val{high},$val{low},$val{open})"; |
56 |
$sql=~s/,\+(\d)/,$1/g; # nuke + which pgsql doesn't like |
$sql=~s/,\+(\d)/,$1/g; # nuke + which pgsql doesn't like |
57 |
if ($sql =~ m,n/a,i) { |
if ($sql =~ m,n/a,i) { |
58 |
|
print("undefined values found. not inserting in db [$sql]"); |
59 |
print_val(); |
print_val(); |
|
print_debug("undefined values found. not inserting in db"); |
|
60 |
} else { |
} else { |
61 |
$dbh->do("$sql") || die "$sql\n".$DBI::errstr; |
$dbh->do("$sql") || die "$sql\n".$DBI::errstr; |
62 |
} |
} |
69 |
my $req = HTTP::Request->new(GET => $url); |
my $req = HTTP::Request->new(GET => $url); |
70 |
my $tree = HTML::TreeBuilder->new; |
my $tree = HTML::TreeBuilder->new; |
71 |
|
|
72 |
#my $res = $ua->request($req); |
my $res = $ua->request($req); |
73 |
#if ($res->is_success) { |
if ($res->is_success) { |
74 |
# print_debug("html: ".$res->content."\n"); |
print_debug("html: ".$res->content."\n"); |
75 |
# $tree->parse($res->content) || die "can't parse html file!"; |
$tree->parse($res->content) || die "can't parse html file!"; |
76 |
|
|
77 |
if (1) { |
# $tree->parse_file("out-without_proxy") || die "can't parse html file!"; |
|
$tree->parse_file("out-without_proxy") || die "can't parse html file!"; |
|
78 |
|
|
79 |
# find date |
# find date |
80 |
my $t = $tree; |
my $t = $tree; |
126 |
warn "can't fetch stock data"; |
warn "can't fetch stock data"; |
127 |
} |
} |
128 |
|
|
129 |
print_val(); |
print_val() if ($debug); |
130 |
|
|
131 |
$dbh->disconnect; |
$dbh->disconnect; |
132 |
|
|