add stock hledger updater

This commit is contained in:
Ingolf Wagner 2021-04-24 14:23:37 +02:00
parent 329d924674
commit 90cd3df0c8
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
2 changed files with 63 additions and 0 deletions

View file

@ -29,6 +29,7 @@
./jupyter.nix ./jupyter.nix
./mysql.nix ./mysql.nix
./property.nix ./property.nix
./finance.nix
]; ];
nixpkgs.config.permittedInsecurePackages = nixpkgs.config.permittedInsecurePackages =

View file

@ -0,0 +1,62 @@
{ lib, config, pkgs, ... }:
let
# find symbols with
# https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=<keywords>&apikey=<api_key>
# as described here : https://www.alphavantage.co/documentation/#symbolsearch
#
# example:
# --------
# stocks = [
# {
# friendly_name = "google";
# symbol = "GOOGL.DEX";
# name = "google";
# currency = "$";
# }
# ];
# results in
# P 2020-01-30 GOOGL $123
stocks = import <secrets/finance/stocks>;
stocksFile = toString /home/syncthing/finance/hledger/stocks.journal;
in {
systemd.services = let
pullService = { name, symbol, currency, friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true;
description = "pull stock_${friendly_name} for hledger";
serviceConfig = {
User = "syncthing";
Type = "oneshot";
};
script = ''
SYMBOL="${symbol}"
APIKEY=${lib.fileContents <secrets/finance/alphavantage/apikey>}
${pkgs.curl}/bin/curl --location --silent \
"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$SYMBOL&apikey=$APIKEY" \
| ${pkgs.jq}/bin/jq --raw-output '.["Global Quote"]
| "P \(.["07. latest trading day"]) ${name} ${currency}\(.["05. price"] | tonumber)"' \
>> ${stocksFile}
'';
};
};
in builtins.listToAttrs (map pullService stocks);
systemd.timers = let
pullTimer = { friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true;
wantedBy = [ "multi-user.target" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = "true";
};
};
};
in builtins.listToAttrs (map pullTimer stocks);
}