about summary refs log tree commit diff
path: root/pkgs/games/itch/fetch-itch
diff options
context:
space:
mode:
authoraszlig <aszlig@redmoonstudios.org>2017-08-25 22:35:28 +0200
committeraszlig <aszlig@redmoonstudios.org>2017-08-25 22:35:28 +0200
commit490b4b47733a25dd953fc4d3326294c65a35b1e4 (patch)
treef31d71a7055c927b81e196551d2ff7d803957575 /pkgs/games/itch/fetch-itch
parentcab045f4f50a3e80eedbdf8953e10cd53f029b43 (diff)
pkgs/games: Add Invisigun Heroes
This introduces support for fetching games from itch.io, because the
Humble Bundle version unfortunately only provides a Steam key and we
obviously don't want that.

I only played two levels, but so far it works as intended.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
Diffstat (limited to 'pkgs/games/itch/fetch-itch')
-rw-r--r--pkgs/games/itch/fetch-itch/default.nix54
1 files changed, 54 insertions, 0 deletions
diff --git a/pkgs/games/itch/fetch-itch/default.nix b/pkgs/games/itch/fetch-itch/default.nix
new file mode 100644
index 00000000..ed1ac7f6
--- /dev/null
+++ b/pkgs/games/itch/fetch-itch/default.nix
@@ -0,0 +1,54 @@
+{ stdenv, curl, cacert, writeText, python3Packages
+
+, apiKey
+}:
+
+{ name, gameId, uploadId, sha256 }:
+
+let
+  cafile = "${cacert}/etc/ssl/certs/ca-bundle.crt";
+
+  getDownloadURL = writeText "getitch.py" ''
+    import os, sys, json
+
+    from urllib.parse import urljoin
+    from urllib.request import urlopen
+
+    API_KEY = os.getenv('apiKey')
+    API_URL = 'https://itch.io/api/1/'
+    API_BASE = urljoin(API_URL, API_KEY) + '/'
+
+    NAME = os.getenv('name')
+    GAME_ID = int(os.getenv('gameId'))
+    UPLOAD_ID = int(os.getenv('uploadId'))
+
+    def request(path):
+      with urlopen(urljoin(API_BASE, path)) as u:
+        return json.loads(u.read())
+
+    for key in request('my-owned-keys')['owned_keys']:
+      if key['game']['id'] == GAME_ID:
+        url = 'download-key/{}/download/{}'.format(key['id'], UPLOAD_ID)
+        sys.stdout.write(request(url)['url'] + '\n')
+        raise SystemExit(0)
+
+    sys.stderr.write('Unable to find download for game {}!'.format(NAME))
+    raise SystemExit(1)
+  '';
+
+in stdenv.mkDerivation {
+  inherit name apiKey gameId uploadId;
+  outputHashAlgo = "sha256";
+  outputHash = sha256;
+
+  SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
+
+  nativeBuildInputs = [ python3Packages.python ];
+
+  buildCommand = ''
+    url="$(python "${getDownloadURL}")"
+    header "downloading $name from $url"
+    "${curl.bin or curl}/bin/curl" --fail --output "$out" "$url"
+    stopNest
+  '';
+}