add stock hledger updater
This commit is contained in:
parent
329d924674
commit
90cd3df0c8
2 changed files with 63 additions and 0 deletions
|
@ -29,6 +29,7 @@
|
|||
./jupyter.nix
|
||||
./mysql.nix
|
||||
./property.nix
|
||||
./finance.nix
|
||||
];
|
||||
|
||||
nixpkgs.config.permittedInsecurePackages =
|
||||
|
|
62
configs/workhorse/finance.nix
Normal file
62
configs/workhorse/finance.nix
Normal 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);
|
||||
|
||||
}
|
Loading…
Reference in a new issue