nixos-config/nixos/configs/workhorse/finance.nix

56 lines
1.5 KiB
Nix
Raw Normal View History

2021-04-24 14:23:37 +02:00
{ lib, config, pkgs, ... }:
let
# find symbols with
2021-04-27 07:03:55 +02:00
# https://www.alphavantage.co/query?function=SYMBOL_SEARCH&apikey=<api_key>&keywords=<keywords>
2021-04-24 14:23:37 +02:00
# 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
2021-10-31 17:49:44 +01:00
stocks = import ../../private_assets/finance/stocks;
2021-04-24 14:23:37 +02:00
stocksFile = toString /home/syncthing/finance/hledger/stocks.journal;
in {
2021-04-27 07:26:15 +02:00
systemd.services.pull_stocks = {
enable = true;
description = "pull stocks for hledger";
serviceConfig = {
User = "syncthing";
Type = "oneshot";
2021-04-24 14:23:37 +02:00
};
2021-04-27 07:26:15 +02:00
script = let
command = { symbol, name, currency, ... }: ''
2021-10-31 17:49:44 +01:00
APIKEY=${lib.fileContents ../../private_assets/finance/alphavantage/apiKey}
2021-04-27 07:26:15 +02:00
SYMBOL="${symbol}"
${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}
sleep 1
'';
in lib.concatStringsSep "\n" (map command stocks);
};
2021-04-24 14:23:37 +02:00
2021-04-27 07:26:15 +02:00
systemd.timers.pull_stocks = {
enable = true;
wantedBy = [ "multi-user.target" ];
timerConfig = {
OnCalendar = "weekly";
Persistent = "true";
};
};
2021-04-24 14:23:37 +02:00
}