12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/usr/bin/env perl
- use strict;
- use warnings;
- use POSIX qw/strftime/;
- use Finance::Quote;
- my $queryRunner = Finance::Quote->new();
- my $time = strftime("%F", localtime());
- my @currencies;
- my @stocks;
- open(my $LINES, "$ENV{HOME}/.ledger/commodities.txt") || die "Cannot open definition file: $!.\n";
- while (my $line = <$LINES>) {
- chomp($line);
- (my $type, my $name, my @rest) = split / /, $line;
- if ($type eq 's') {
- push @stocks, $name;
- } elsif ($type eq 'c') {
- push @currencies, $name;
- } else {
- next;
- }
- }
- close($LINES);
- open(my $output, ">>$ENV{HOME}/.ledger/prices.beancount") || die "Cannot open price data file: $!.\n";
- print "\n* $time\n\n";
- print $output "\n* $time\n\n";
- foreach my $stock (@stocks) {
- my %data = $queryRunner->fetch('nyse',$stock);
- printf "$time price $stock %.2f USD\n", $data{$stock, "price"};
- printf $output "$time price $stock %.2f USD\n", $data{$stock, "price"};
- }
- foreach my $currency (@currencies) {
- my $exchangeRate = $queryRunner->currency($currency ,"USD");
- printf "$time price $currency %.2f USD\n", $exchangeRate;
- printf $output "$time price $currency %.2f USD\n", $exchangeRate;
- }
- close($output);
|