6 |
|
|
7 |
my $debug=0; |
my $debug=0; |
8 |
|
|
9 |
|
$debug++ if (lc($ARGV[0]) eq "-d"); |
10 |
|
|
11 |
|
sub print_debug { |
12 |
|
return if (! $debug); |
13 |
|
open(DEBUG,">> debug") || warn "can't open debug file!"; |
14 |
|
print DEBUG "###",@_; |
15 |
|
print @_; |
16 |
|
close(DEBUG); |
17 |
|
} |
18 |
|
|
19 |
|
print_debug("debug level $debug"); |
20 |
|
|
21 |
my $dbh = DBI->connect("DBI:Pg:dbname=corp","","") || die $DBI::errstr; |
my $dbh = DBI->connect("DBI:Pg:dbname=corp","","") || die $DBI::errstr; |
22 |
|
|
23 |
my $ua = new LWP::UserAgent; |
my $ua = new LWP::UserAgent; |
24 |
$ua->agent("pliva_harvester 0.0"); |
$ua->agent("pliva_harvester 0.0"); |
25 |
$ua->timeout(60); |
$ua->timeout(60); |
26 |
$ua->env_proxy(); |
$ua->env_proxy(); |
27 |
|
$ua->proxy(['http', 'ftp'], 'http://proxy.pliva.hr:8080/'); |
28 |
|
|
29 |
my $market=0; |
my $market=0; |
30 |
my @markets = ('LSE','FTSE','bug1','bug2','bug3','bug4'); |
my @markets = ('LSE','FTSE','bug1','bug2','bug3','bug4'); |
36 |
my $sql="insert into stocks values ('".shift(@val)."','".$markets[$market]."',".join(",",@val).")"; |
my $sql="insert into stocks values ('".shift(@val)."','".$markets[$market]."',".join(",",@val).")"; |
37 |
$sql=~s/,\+(\d)/,$1/g; # nuke + which pgsql doesn't like |
$sql=~s/,\+(\d)/,$1/g; # nuke + which pgsql doesn't like |
38 |
$dbh->do("$sql") || die "$sql\n".$DBI::errstr; |
$dbh->do("$sql") || die "$sql\n".$DBI::errstr; |
39 |
print "$sql\n" if ($debug); |
print_debug("sql: $sql\n"); |
40 |
$market++ |
$market++; |
|
|
|
41 |
} |
} |
42 |
|
|
43 |
my $req = HTTP::Request->new(GET =>"http://analysis.esi.co.uk/display.cgi?PLVD"); |
my $req = HTTP::Request->new(GET =>"http://analysis.esi.co.uk/display.cgi?PLVD"); |
44 |
my $res = $ua->request($req); |
my $res = $ua->request($req); |
45 |
if ($res->is_success) { |
if ($res->is_success) { |
46 |
foreach (split(/\n/, $res->content)) { |
print_debug("html: ".$res->content."\n"); |
47 |
|
foreach (split(/[\n\r]+/, $res->content)) { |
48 |
chomp; |
chomp; |
49 |
|
print_debug("line: $_\n"); |
50 |
if (m, at (\d+:\d\d) on (\d+\s\w+\s\d+),i) { |
if (m, at (\d+:\d\d) on (\d+\s\w+\s\d+),i) { |
51 |
my ($time,$date) = ($1,$2); |
my ($time,$date) = ($1,$2); |
52 |
|
print_debug("time: $time date: $date"); |
53 |
insert() if (@val); |
insert() if (@val); |
54 |
undef @val; |
undef @val; |
55 |
push @val,"$date $time"; |
push @val,"$date $time"; |
63 |
} |
} |
64 |
if (m,<td VALIGN="MIDDLE" ALIGN="CENTER" HEIGHT="30"><FONT SIZE="-1" FACE="Verdana"><b>([-\+]*\d+\.\d+)</b>,i) { |
if (m,<td VALIGN="MIDDLE" ALIGN="CENTER" HEIGHT="30"><FONT SIZE="-1" FACE="Verdana"><b>([-\+]*\d+\.\d+)</b>,i) { |
65 |
push @val,$1; |
push @val,$1; |
66 |
|
print_debug("val: $1"); |
67 |
} |
} |
68 |
|
|
69 |
} |
} |
70 |
insert(); |
insert(); |
71 |
|
} else { |
72 |
|
warn "can't fetch stock data"; |
73 |
} |
} |
74 |
|
|
75 |
$dbh->disconnect; |
$dbh->disconnect; |