diff --git a/lib/default.nix b/lib/default.nix index 61a1e7d3..5b003684 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -17,11 +17,13 @@ pkgs.lib.makeExtensible builders = callLib ./builders.nix; trivial = callLib ./trivial.nix; data = callLib ./data.nix; + fetchers = callLib ./fetchers; inherit (self.builders) makeXDGMimeAssociationList makeXDGPortalConfiguration makeXDGDesktopEntry; inherit (self.trivial) countAttrs; inherit (self.data) importYAML renderTeraTemplate; + inherit (self.fetchers) fetchInternetArchive; } // lib.optionalAttrs (builtins ? fetchTree) { flake = callLib ./flake.nix; diff --git a/lib/fetchers/default.nix b/lib/fetchers/default.nix new file mode 100644 index 00000000..e4eceb31 --- /dev/null +++ b/lib/fetchers/default.nix @@ -0,0 +1,5 @@ +{ pkgs, lib, self }: + +{ + fetchInternetArchive = pkgs.callPackage ./fetch-internet-archive { }; +} diff --git a/lib/fetchers/fetch-internet-archive/default.nix b/lib/fetchers/fetch-internet-archive/default.nix new file mode 100644 index 00000000..fc56dcaa --- /dev/null +++ b/lib/fetchers/fetch-internet-archive/default.nix @@ -0,0 +1,27 @@ +{ stdenvNoCC, lib, fetchzip, fetchurl, curl }: + +{ + id, + file ? "", + formats ? [ ], + hash ? "", + name ? "internet-archive-${id}", +}@args: + +let + isFormatIndiciated = formats != [ ]; + url = + if isFormatIndiciated + then "https://archive.org/compress/${lib.escapeURL id}/formats=${lib.concatStringsSep "," formats}" + else "https://archive.org/download/${lib.escapeURL id}/${lib.escapeURL file}"; + + args' = lib.removeAttrs args [ "id" "file" "formats" ] // { + inherit url hash name; + } // lib.optionalAttrs isFormatIndiciated { inherit hash; }; + + fetcher = + if isFormatIndiciated + then fetchzip + else fetchurl; +in + fetcher args' diff --git a/tests/lib/fetchers.nix b/tests/lib/fetchers.nix new file mode 100644 index 00000000..c74abecb --- /dev/null +++ b/tests/lib/fetchers.nix @@ -0,0 +1,21 @@ +{ + pkgs ? import { }, + lib ? pkgs.lib, + self ? import ../../lib { inherit pkgs; }, +}: + +{ + testsInternetArchiveFetcher = + self.fetchers.fetchInternetArchive { + id = "md_music_sonic_the_hedgehog"; + file = "01 - Title Theme - Masato Nakamura.flac"; + hash = "sha256-kGjsVjtjXK9imqyi4GF6qkFVmobiTAe/ZAeEwiouqS4="; + }; + + testsInternetArchiveFetcher2 = + self.fetchers.fetchInternetArchive { + id = "md_music_sonic_the_hedgehog"; + formats = [ "TEXT" "PNG" ]; + hash = "sha256-xbhasJ/wEgcY+EcBAJp5UoYB4N4It3QV/iIeGGdCET8="; + }; +}