optimize finanz

This commit is contained in:
Ingolf Wagner 2021-04-27 07:26:15 +02:00
parent 133ef070a4
commit e61ac41bee
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B

View file

@ -22,33 +22,29 @@ let
in { in {
systemd.services = let systemd.services.pull_stocks = {
pullService = { name, symbol, currency, friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true; enable = true;
description = "pull stock_${friendly_name} for hledger"; description = "pull stocks for hledger";
serviceConfig = { serviceConfig = {
User = "syncthing"; User = "syncthing";
Type = "oneshot"; Type = "oneshot";
}; };
script = ''
SYMBOL="${symbol}" script = let
command = { symbol, name, currency, ... }: ''
APIKEY=${lib.fileContents <secrets/finance/alphavantage/apikey>} APIKEY=${lib.fileContents <secrets/finance/alphavantage/apikey>}
SYMBOL="${symbol}"
${pkgs.curl}/bin/curl --location --silent \ ${pkgs.curl}/bin/curl --location --silent \
"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$SYMBOL&apikey=$APIKEY" \ "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$SYMBOL&apikey=$APIKEY" \
| ${pkgs.jq}/bin/jq --raw-output '.["Global Quote"] | ${pkgs.jq}/bin/jq --raw-output '.["Global Quote"]
| "P \(.["07. latest trading day"]) ${name} ${currency}\(.["05. price"] | tonumber)"' \ | "P \(.["07. latest trading day"]) ${name} ${currency}\(.["05. price"] | tonumber)"' \
>> ${stocksFile} >> ${stocksFile}
sleep 1
''; '';
in lib.concatStringsSep "\n" (map command stocks);
}; };
};
in builtins.listToAttrs (map pullService stocks);
systemd.timers = let systemd.timers.pull_stocks = {
pullTimer = { friendly_name, ... }: {
name = "pull_stock_${friendly_name}";
value = {
enable = true; enable = true;
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
timerConfig = { timerConfig = {
@ -56,7 +52,4 @@ in {
Persistent = "true"; Persistent = "true";
}; };
}; };
};
in builtins.listToAttrs (map pullTimer stocks);
} }