File: getDiv

package info (click to toggle)
beancounter 0.8.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 332 kB
  • ctags: 136
  • sloc: perl: 2,308; sh: 743; makefile: 138
file content (135 lines) | stat: -rw-r--r-- 3,233 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl -w
#
# contributed to beancounter by 
# Joao Antunes Costa <joao.costa /AT/ fidelizarte /DOT/ pt
#
# copyright Joao Antunes Costa <joao.costa /AT/ fidelizarte /DOT/ pt

use strict;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::FormatText;
use Date::Calc qw (Delta_Days);


my $symbol = shift || die("Usage:\n\tgetDiv symbol\n");
my ($second, $minute, $hour, $day, $month, $year) = localtime(time);
$month++;
$month = "0$month" if ($month < 10);
$day = "0$day" if ($day < 10);
$year+=1900;

my $URL = "http://finance.yahoo.com/q/hp?s=$symbol&a=00&b=1&c=1980&d=$month&e=$day&f=$year&g=v";

my $content;
if (defined ($content = get $URL)) {
  my $tree = HTML::TreeBuilder->new;
  $tree->parse($content);
  my $txt = HTML::FormatText->new;
  my @text = reverse(split("\n", $txt->format($tree)));

  my $work = 0;
  my $IsSplit = 0;
  my $lastDate;
  my $firstDate = "1980-01-01";
  my $ratio = 0;
  my $lastRatio = 0;
  my $sql;
  my ($one, $two);


  foreach (@text) {
    chomp($_);
    $work = 0 if (/Adj Close/i);
    if ($work) {
      s/^ +| +$//;

      if (/^(\d[^:]+):([^s]+)/i) {
          $IsSplit = 1;
          $one = $1;
          $two = $2;
          $one =~ s/^\s+|\s+$//;
          $two =~ s/^\s+|\s+$//;
      }

      if (/^\d.+\d$/ && $IsSplit) {
          $IsSplit = 0;
          $lastDate = FormatDate($_);
          $ratio = $two / $one;
          if (DateOk($firstDate, $lastDate) || $lastRatio != $ratio) {
            $lastRatio = $ratio;
            #print "$ratio at $lastDate\n";
            $sql = "UPDATE stockprices SET day_open = day_open * $ratio, day_low = day_low * $ratio, day_high = day_high * $ratio, day_close = day_close * $ratio, volume = volume * $ratio WHERE symbol = '$symbol' AND date < '$lastDate';\n";
            print $sql;
            $firstDate = $lastDate;
          } else {
            warn "Ignoring $two/$one split at $lastDate for $symbol\n";
          }
      }

    }
    $work = 1 if (/Close price/i);
  }
} else {
  die "could not get $symbol\n";
}


sub DateOk {
my ($firstDate, $lastDate) = @_;

#print "checking $firstDate vs $lastDate\n";
my @d1 = split "-", $firstDate;
my @d2 = split "-", $lastDate;
my $days = abs(Delta_Days ($d1[0], $d1[1], $d1[2], $d2[0], $d2[1], $d2[2]));
return 1 if ($days > 40);
return 0;
}

sub FormatDate {
my $date = shift;
my @date = split("-", $date);
#print "Formatting $date\n";

if ($date[2] > 85) {$date[2] = "19".$date[2];} else {$date[2] = "20".$date[2];}

return $date[2] . "-" . GetMonth($date[1]) . "-" . stuff($date[0]);
}

sub stuff {
my $day = shift;
return "0$day" if (length($day) == 1);
return $day;
}

sub GetMonth {
my $month = shift;
if ( $month =~ /Jan/i) {
return "01";
} elsif ( $month =~ /Feb/i) {
return "02";
} elsif ( $month =~ /Mar/i) {
return "03";
} elsif ( $month =~ /Apr/i) {
return "04";
} elsif ( $month =~ /May/i) {
return "05";
} elsif ( $month =~ /Jun/i) {
return "06";
} elsif ( $month =~ /Jul/i) {
return "07";
} elsif ( $month =~ /Aug/i) {
return "08";
} elsif ( $month =~ /Sep/i) {
return "09";
} elsif ( $month =~ /Oct/i) {
return "10";
} elsif ( $month =~ /Nov/i) {
return "11";
} elsif ( $month =~ /Dec/i) {
return "12";
} else {
die("Unrecognized month string ($month)");
}
}