Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # Author: Dawid Mocek <[email protected]>
- # Exract data from earnings HTML table @ EarningsWhispers.com/calendar.asp and save to DB
- use strict;
- use warnings;
- use DBI;
- use HTML::TableExtract;
- use LWP::UserAgent;
- use DateTime;
- use DateTime::Format::Strptime;
- my $url = 'http://www.earningswhispers.com/calendar.asp';
- my $dt = DateTime->now;
- my $strp = DateTime::Format::Strptime->new(pattern => '%F %I:%M %p', time_zone => 'EST');
- my $ua = LWP::UserAgent->new(
- timeout => 180,
- );
- my $resp= $ua->get($url);
- my $html = $resp->content;
- my @innerHTML = ($html =~ m/document\.getElementById\(\'epsdates\'\)\.innerHTML\s+=\s+\"(.*)\";/g);
- my $dbh = DBI->connect('DBI:mysql:db_name;host=localhost', 'user', 'password');
- $dbh->{mysql_auto_reconnect} = 1;
- my $stmt_earn = $dbh->prepare('INSERT INTO `earnings` (earn_time, ticker, companyname, conf_time, stage) VALUES(?,?,?,?,?)');
- parse($innerHTML[0], 'BMO');
- parse($innerHTML[1], 'DMH');
- parse($innerHTML[2], 'AMC');
- sub parse {
- my $table = $_[0];
- my $when = $_[1];
- my $te = HTML::TableExtract->new();
- $te->parse($table);
- $te->first_table_found();
- my @rows = $te->rows();
- if(scalar(@rows) == 0) { return; }
- foreach my $row (@rows) {
- @$row[1] =~ /^(.*)\s+\((.*)\)$/;
- my $issue_name = $1;
- my $issue_symbol = $2;
- my $earn_time;
- if(@$row[3] =~ /^([a-zA-Z\s0-9:]*)$/) {
- $earn_time = $strp->parse_datetime($dt->ymd . " " . $1);
- $earn_time = $earn_time->strftime("%Y-%m-%d %H:%M:%S");
- }
- else {
- $earn_time = $dt->ymd . ' ' .'00:00:00';
- }
- my $conf_time;
- if(@$row[4] =~ /^([a-zA-Z\s0-9:]*)$/) {
- $conf_time = $strp->parse_datetime($dt->ymd . " " . $1);
- $conf_time = $conf_time->strftime("%Y-%m-%d %H:%M:%S");
- }
- else {
- $conf_time = $dt->ymd . ' '. '00:00:00';
- }
- $stmt_earn->execute( $earn_time, $issue_symbol, $issue_name, $conf_time, $when);
- }
- }
- $dbh->disconnect();
Add Comment
Please, Sign In to add comment