diff --git a/default.nix b/default.nix index cf9e3cb1fa..608cd1f314 100644 --- a/default.nix +++ b/default.nix @@ -20,6 +20,7 @@ in fix (self: nix = import ./nix ch; web = import ./web ch; go = import ./go ch; + py = import ./py ch; version = import ./version.nix ch; diff --git a/py/default.nix b/py/default.nix new file mode 100644 index 0000000000..6dc76cb549 --- /dev/null +++ b/py/default.nix @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2020 Luke Granger-Brown +# +# SPDX-License-Identifier: Apache-2.0 + +args: { + valveindexinstock = import ./valveindexinstock args; +} diff --git a/py/valveindexinstock/__main__.py b/py/valveindexinstock/__main__.py new file mode 100644 index 0000000000..ac6725bdf9 --- /dev/null +++ b/py/valveindexinstock/__main__.py @@ -0,0 +1,40 @@ +import time + +import requests +import bs4 +from prometheus_client import start_http_server, Gauge + +IN_STOCK = Gauge("valve_index_in_stock", "Valve index in stock?") + + +def fetch_in_stock(): + resp = requests.get("https://store.steampowered.com/valveindex") + resp.raise_for_status() + + soup = bs4.BeautifulSoup(resp.text, 'html.parser') + form = soup.find('form', attrs=dict(name='add_to_cart_354231')) + if not form: + raise Exception("couldn't find add to cart form") + price = form.parent.find('div', class_='game_purchase_price') + if not price: + raise Exception("couldn't find price") + green_btn = price.parent.find('span', class_='btn_disabled') + if green_btn: + return 0 + return 1 + + +def main(argv): + if argv: + raise Exception("too many arguments") + + IN_STOCK.set_function(fetch_in_stock) + start_http_server(9998) + while True: + time.sleep(1000) + + +if __name__ == "__main__": + import sys + + main(sys.argv[1:]) diff --git a/py/valveindexinstock/default.nix b/py/valveindexinstock/default.nix new file mode 100644 index 0000000000..f44bc3371e --- /dev/null +++ b/py/valveindexinstock/default.nix @@ -0,0 +1,43 @@ +# SPDX-FileCopyrightText: 2020 Luke Granger-Brown +# +# SPDX-License-Identifier: Apache-2.0 + +{ depot, pkgs, ... }: + +let + python = pkgs.python3.withPackages (ps: with ps; [ + requests + beautifulsoup4 + prometheus_client + ]); + + filterSourcePred = (path: type: type != "directory" || ( + baseNameOf path != "__pycache__" && + baseNameOf path != "node_modules" && + true)); +in +pkgs.stdenvNoCC.mkDerivation rec { + name = "valveindexinstock"; + + src = builtins.filterSource filterSourcePred ./.; + + buildInputs = with pkgs; [ makeWrapper ]; + propagatedBuildInputs = [ python ]; + + installPhase = '' + sitepkgdir="$out/lib/${python.libPrefix}/site-packages" + pkgdir="$sitepkgdir/valveindexinstock" + mkdir -p $pkgdir + cp -R \ + *.py \ + $pkgdir + + mkdir "$out/bin" + makeWrapper "${python}/bin/python" "$out/bin/valveindexinstock" \ + --add-flags "-m" \ + --add-flags "valveindexinstock" \ + --suffix PYTHONPATH : "$sitepkgdir" + ''; + + passthru.pythonEnv = python; +} diff --git a/py/valveindexinstock/shell.nix b/py/valveindexinstock/shell.nix new file mode 100644 index 0000000000..d5c5bbe055 --- /dev/null +++ b/py/valveindexinstock/shell.nix @@ -0,0 +1,12 @@ +{ depot ? import {} }: + +let + inherit (depot.py) valveindexinstock; + inherit (depot) pkgs; +in pkgs.mkShell { + buildInputs = with pkgs; [ + valveindexinstock.pythonEnv + + black + ]; +}