py/valveindexinstock: init

This commit is contained in:
Luke Granger-Brown 2021-02-22 21:31:23 +00:00
parent 0a38f01047
commit 66e9fb38f8
5 changed files with 103 additions and 0 deletions

View file

@ -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;

7
py/default.nix Normal file
View file

@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0
args: {
valveindexinstock = import ./valveindexinstock args;
}

View file

@ -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:])

View file

@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# 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;
}

View file

@ -0,0 +1,12 @@
{ depot ? import <depot> {} }:
let
inherit (depot.py) valveindexinstock;
inherit (depot) pkgs;
in pkgs.mkShell {
buildInputs = with pkgs; [
valveindexinstock.pythonEnv
black
];
}