about summary refs log tree commit diff
path: root/pkgs/games/humblebundle/fetch-humble-bundle
diff options
context:
space:
mode:
authoraszlig <aszlig@redmoonstudios.org>2016-02-08 22:15:55 +0100
committeraszlig <aszlig@redmoonstudios.org>2016-02-08 22:15:55 +0100
commit3c6c1392de4ee4a6c0bb13cb7908f9304d6dd053 (patch)
tree8eb7abf04eb250e5337c90acb69103adaeeef422 /pkgs/games/humblebundle/fetch-humble-bundle
parent42c3024877bbdd127e7002e35f41cd87aaf792f7 (diff)
parent5570ec94ffe086f1ff47ad99606d39eb7b815dd1 (diff)
Merge aszlig/nixgames into vuizvui
This is a subtree merge of the full nixgames repository including
history. Right now it isn't linked to vuizvui at all but we're going to
do that very soon.

The reason why I'm merging both repositories together is that it's
easier for me to manage updates and also facilitate contributions by
people from the OpenLab.

Another reason to merge it is that we can write more generic functions
for building both, the games and the rest.

Right now the licensing is still the Apache License version 2.0, but
we're going to change it to the vuizvui license as well.
Diffstat (limited to 'pkgs/games/humblebundle/fetch-humble-bundle')
-rw-r--r--pkgs/games/humblebundle/fetch-humble-bundle/default.nix87
1 files changed, 87 insertions, 0 deletions
diff --git a/pkgs/games/humblebundle/fetch-humble-bundle/default.nix b/pkgs/games/humblebundle/fetch-humble-bundle/default.nix
new file mode 100644
index 00000000..fbabaa8c
--- /dev/null
+++ b/pkgs/games/humblebundle/fetch-humble-bundle/default.nix
@@ -0,0 +1,87 @@
+{ stdenv, curl, cacert, writeText, fetchFromGitHub, fetchpatch
+, python, buildPythonPackage, pythonPackages
+
+, email, password
+}:
+
+{ name ? null, machineName, downloadName ? "Download", suffix ? "humblebundle", md5 }: let
+  cafile = "${cacert}/etc/ssl/certs/ca-bundle.crt";
+
+  humbleAPI = buildPythonPackage rec {
+    name = "humblebundle-${version}";
+    version = "0.1.1";
+
+    src = fetchFromGitHub {
+      owner = "saik0";
+      repo = "humblebundle-python";
+      rev = version;
+      sha256 = "1kcg42nh7sbjabim1pbqx14468pypznjy7fx2bv7dicy0sqd9b8j";
+    };
+
+    propagatedBuildInputs = with pythonPackages; [ requests2 ];
+  };
+
+  pyStr = str: "'${stdenv.lib.escape ["'" "\\"] str}'";
+
+  getDownloadURL = writeText "gethburl.py" ''
+    import sys, humblebundle
+
+    def get_products(client):
+      gamekeys = client.get_gamekeys()
+      for gamekey in gamekeys:
+        order = hb.get_order(gamekey)
+        if order.subproducts is None:
+          continue
+        for subproduct in order.subproducts:
+          prodname = subproduct.human_name.encode('ascii', 'replace')
+          downloads = [(download.machine_name, download.download_struct)
+                       for download in subproduct.downloads]
+          yield (prodname, downloads)
+
+    def find_download(downloads):
+      for machine_name, dstruct in sum(downloads.values(), []):
+        if machine_name == ${pyStr machineName}:
+          for ds in dstruct:
+            if ds.name == ${pyStr downloadName}:
+              return ds
+          print >>sys.stderr, \
+            ${pyStr "Unable to find ${downloadName} for ${machineName}!"}
+          print >>sys.stderr, 'Available download types:'
+          for ds in dstruct:
+            print >>sys.stderr, "  " + ds.name
+          raise SystemExit(1)
+
+    hb = humblebundle.HumbleApi()
+    hb.login(${pyStr email}, ${pyStr password})
+    products = dict(get_products(hb))
+    dstruct = find_download(products)
+
+    if dstruct is None:
+      print >>sys.stderr, ${pyStr "Cannot find download for ${machineName}!"}
+      print >>sys.stderr, 'Available machine names:'
+      for name, dstructs in sorted(products.items(), key=lambda x: x[0]):
+        print >>sys.stderr, "  * " + name
+        print >>sys.stderr, "    " + ', '.join(map(lambda x: x[0], dstructs))
+      raise SystemExit(1)
+    elif dstruct.md5 != ${pyStr md5}:
+      print >>sys.stderr, \
+        ${pyStr "MD5 for ${machineName} is not ${md5} but "} \
+        + dstruct.md5 + '.'
+      raise SystemExit(1)
+    else:
+      print dstruct.url.web
+  '';
+in stdenv.mkDerivation {
+  name = if name != null then name else "${machineName}.${suffix}";
+  outputHashAlgo = "md5";
+  outputHash = md5;
+
+  buildInputs = [ python humbleAPI ];
+
+  buildCommand = ''
+    url="$(python "${getDownloadURL}")"
+    header "downloading $name from $url"
+    ${curl}/bin/curl --cacert "${cafile}" --fail --output "$out" "$url"
+    stopNest
+  '';
+}