From b073bcbacb448616df66147b3569106936a5f6bc Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Wed, 18 Jan 2023 21:30:31 +0100 Subject: [PATCH] Add some pkgs to nix config --- bin/absorb.py | 93 +++++++++++++++++++++++++++++++ bin/default.nix | 3 + home-manager/home.nix | 1 - nixos/machines/frumar/default.nix | 5 ++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 bin/absorb.py diff --git a/bin/absorb.py b/bin/absorb.py new file mode 100644 index 0000000..1af51a9 --- /dev/null +++ b/bin/absorb.py @@ -0,0 +1,93 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python3 -p python3 python3.pkgs.requests -I nixpkgs=channel:nixos-22.05 +import json, os, sys, hashlib +from pathlib import Path +import shutil +import tempfile +import requests + +if len(sys.argv) < 3: + print(f"Usage: sudo {sys.argv[0]} [dest] [files..]", file=sys.stderr) + sys.exit(2) + +if os.geteuid() != 0: + print("Please run as root", file=sys.stderr) + sys.exit(1) + +plexmedia = Path("/data/plexmedia") +dest = plexmedia / sys.argv[1] + +sections = { + "movies": 1, + "series": 2, + "anime-movies": 3, + "anime-series": 4, + "talks": 5 +} + +if not dest.is_dir(): + print(f"Error: {dest} is not a directory/does not exist") + sys.exit(1) + +sid = sections[sys.argv[1]] + +def hashfile(path: Path) -> str: + hash = hashlib.sha256() + buf = bytearray(128 * 1024) # 128kb + mv = memoryview(buf) + with path.open('rb') as f: + while n := f.readinto(mv): + hash.update(mv[:n]) + return hash.hexdigest() + +def symlink_force(name: Path, target: Path) -> None: + try: + name.symlink_to(target) + except FileExistsError: + tmp = Path(tempfile.mktemp(dir=name.parent)) + tmp.symlink_to(target) + tmp.replace(name) + +def ca_import(source: Path) -> Path: + if not sourcepath.is_file() or sourcepath.is_symlink(): + print(f"{sourcepath} is not a regular file") + if sourcepath.is_symlink(): + rsv = sourcepath.resolve() + if rsv.parent == plexmedia / "ca": + return rsv + else: + sys.exit(2) + return + # CA import + print(f"[{source.name}] hash") + hash = hashfile(sourcepath) + print(hash) + ca_dest = (plexmedia / "ca" / hash).with_suffix(sourcepath.suffix) + print(f"[{source.name}] copy") + try: + shutil.copyfile(sourcepath, ca_dest) + except shutil.SameFileError: + print(f"warning: skipping copy, already in place") + #print(f"chown plex:plex {ca_dest}") + shutil.chown(ca_dest, user="plex", group="plex") + return ca_dest + +for sourcefile in sys.argv[2:]: + sourcepath = Path(sourcefile) + print("CA", sourcepath) + ca_dest = ca_import(sourcepath) + # plex import + destpath = dest / sourcepath.name + print(f"ln -s {ca_dest} {destpath}") + symlink_force(destpath, ca_dest) + shutil.chown(destpath, user="plex", group="plex") + # torrent link + symlink_force(sourcepath, ca_dest) + + +with open("/home/yorick/plex_token", 'r') as f: + PLEX_TOKEN = f.read().splitlines()[0] + +requests.get(f"https://plex.yori.cc/library/sections/{sid}/refresh", headers={ + "X-Plex-Token": PLEX_TOKEN +}) diff --git a/bin/default.nix b/bin/default.nix index 21485d2..b704de1 100644 --- a/bin/default.nix +++ b/bin/default.nix @@ -26,4 +26,7 @@ in lib.mapAttrs (k: f: f k) { y-cal-widget = makeWrap ./cal.py "${ python3.withPackages (p: [ (p.toPythonModule gcalcli) p.i3ipc ]) }/bin/python"; + absorb = makeWrap ./absorb.py "${ + python3.withPackages (p: [ p.requests ]) + }/bin/python"; } diff --git a/home-manager/home.nix b/home-manager/home.nix index c331893..de1a9be 100644 --- a/home-manager/home.nix +++ b/home-manager/home.nix @@ -269,7 +269,6 @@ in { yubioath-flutter ## games - minecraft prismlauncher steam # minecraft diff --git a/nixos/machines/frumar/default.nix b/nixos/machines/frumar/default.nix index 09311fb..791268d 100644 --- a/nixos/machines/frumar/default.nix +++ b/nixos/machines/frumar/default.nix @@ -246,6 +246,11 @@ ranger pyroscope rtorrent + jq + mcrcon + # jre_headless # fails to build + unzip + yscripts.absorb ]; services.acme-sh.certs.wildcard-yori-cc = { mainDomain = "*.yori.cc";