Browse Source

Added beancount price fetching scripts

Samuel W. Flint 6 years ago
parent
commit
a19247520c
2 changed files with 52 additions and 0 deletions
  1. 6 0
      bean-prices
  2. 46 0
      beancount-price-fetch.pl

+ 6 - 0
bean-prices

@@ -0,0 +1,6 @@
+#!/bin/sh
+
+beancount-price-fetch.pl
+
+cd ~/.ledger/
+git add prices.beancount ; git commit -m "Updated beancount price database."

+ 46 - 0
beancount-price-fetch.pl

@@ -0,0 +1,46 @@
+#!/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);