refactor modules
This commit is contained in:
1
modules/tools/bulk-transcode/.envrc
Normal file
1
modules/tools/bulk-transcode/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake ../../#bulk-transcode --show-trace
|
||||
35
modules/tools/bulk-transcode/_derivation.nix
Normal file
35
modules/tools/bulk-transcode/_derivation.nix
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
bash,
|
||||
coreutils,
|
||||
ffmpeg,
|
||||
findutils,
|
||||
fzf,
|
||||
gum,
|
||||
lib,
|
||||
makeWrapper,
|
||||
stdenv,
|
||||
tree,
|
||||
...
|
||||
}:
|
||||
stdenv.mkDerivation (finalAttrs: {
|
||||
name = "bulk-transcode";
|
||||
src = ./.;
|
||||
|
||||
buildInputs = [
|
||||
bash
|
||||
coreutils
|
||||
ffmpeg
|
||||
findutils
|
||||
fzf
|
||||
gum
|
||||
makeWrapper
|
||||
tree
|
||||
];
|
||||
|
||||
installPhase = ''
|
||||
install -Dm755 bulk-transcode.sh $out/bin/bulk-transcode
|
||||
|
||||
wrapProgram $out/bin/bulk-transcode \
|
||||
--set PATH "${lib.makeBinPath finalAttrs.buildInputs}"
|
||||
'';
|
||||
})
|
||||
7
modules/tools/bulk-transcode/_shell.nix
Normal file
7
modules/tools/bulk-transcode/_shell.nix
Normal file
@@ -0,0 +1,7 @@
|
||||
{pkgs ? import <nixpkgs> {}}: let
|
||||
bin = pkgs.callPackage ./derivation.nix {};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
name = "bulk-transcode";
|
||||
inputsFrom = [bin];
|
||||
}
|
||||
96
modules/tools/bulk-transcode/bulk-transcode.sh
Normal file
96
modules/tools/bulk-transcode/bulk-transcode.sh
Normal file
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
declare -rA presets=(
|
||||
[davinci-resolve]="-c:v dnxhd -profile:v dnxhr_hq -pix_fmt yuv422p -c:a pcm_s16le"
|
||||
[instagram]="-vf scale='if(gte(iw/ih,1),1920,-1)':'if(gte(iw/ih,1),-1,1920)':flags=lanczos -r 30 -c:v libx264 -profile:v high -level 4.1 -pix_fmt yuv420p -preset slow -crf 18 -bf 2 -g 15 -keyint_min 15 -x264-params \"open-gop=0:cabac=1:b-pyramid=none\" -movflags +faststart -c:a aac -b:a 96k"
|
||||
[insta-4k]="-r 30 -c:v libx264 -profile:v high -level 4.1 -pix_fmt yuv420p -preset slow -crf 18 -bf 2 -g 15 -keyint_min 15 -x264-params \"open-gop=0:cabac=1:b-pyramid=none\" -movflags +faststart -c:a aac -b:a 96k"
|
||||
[storage-hevc]="-c:v libx265 -preset slower -crf 18 -pix_fmt yuv420p10le -x265-params aq-mode=3:aq-strength=1.0:psy-rd=1.8:psy-rdoq=1.0 -c:a copy"
|
||||
[storage-av1]="-c:v libsvtav1 -preset 6 -crf 28 -pix_fmt yuv420p -g 240 -svtav1-params tune=0:aq-mode=2 -c:a copy"
|
||||
[storage-av1-1080p]="-vf scale='if(gte(iw/ih,1),1920,-1)':'if(gte(iw/ih,1),-1,1920)' -c:v libsvtav1 -preset 6 -crf 28 -pix_fmt yuv420p -g 240 -svtav1-params tune=0:aq-mode=2 -c:a copy"
|
||||
[storage-av1-nvenc]="-c:v av1_nvenc -cq 28 -preset slow -pix_fmt yuv420p10le -c:a copy"
|
||||
[network]="-c:v libx264 -preset slow -crf 22 -pix_fmt yuv420p -c:a aac -b:a 128k"
|
||||
[network-1080p]="-vf scale='if(gte(iw/ih,1),1920,-1)':'if(gte(iw/ih,1),-1,1920)' -c:v libx264 -preset slow -crf 22 -pix_fmt yuv420p -c:a aac -b:a 128k"
|
||||
[whatsapp]="-vf scale='if(gte(iw/ih,1),1920,-1)':'if(gte(iw/ih,1),-1,1920)' -c:v libx264 -preset slow -crf 30 -profile:v baseline -level 3.0 -pix_fmt yuv420p -r 25 -g 50 -c:a aac -b:a 160k -r:a 44100"
|
||||
)
|
||||
|
||||
declare -rA containers=(
|
||||
[davinci-resolve]="mov"
|
||||
[instagram]="mp4"
|
||||
[insta-4k]="mp4"
|
||||
[storage-hevc]="mkv"
|
||||
[storage-av1]="mkv"
|
||||
[storage-av1-1080p]="mkv"
|
||||
[storage-av1-nvenc]="mkv"
|
||||
[network]="mp4"
|
||||
[network-1080p]="mp4"
|
||||
[whatsapp]="mp4"
|
||||
)
|
||||
|
||||
where="${1:-.}"
|
||||
dest="${2:-$where}"
|
||||
|
||||
selection=$(find "$where" -type f | fzf --multi --preview 'ffprobe -v error -show_format -show_streams {}' --preview-window=up:wrap)
|
||||
|
||||
preset=$(
|
||||
printf '%s\n' "${!presets[@]}" | \
|
||||
fzf --multi --prompt "Select a preset"
|
||||
)
|
||||
flags="${presets[$preset]}"
|
||||
container="${containers[$preset]}"
|
||||
|
||||
output_dir=$(find "$dest" -type d ! -name '.*' ! -path '*/.*/*' | fzf --preview 'tree -C {}' --preview-window=up:wrap --prompt "Select output directory: ")
|
||||
|
||||
if gum confirm "Flatten the directory structure?";
|
||||
then
|
||||
flatten=true
|
||||
else
|
||||
flatten=false
|
||||
fi
|
||||
|
||||
|
||||
function transcode_job {
|
||||
local ifile="$1"
|
||||
local output_dir="$2"
|
||||
local flatten="$3"
|
||||
local where="$4"
|
||||
local flags="$5"
|
||||
local container="$6"
|
||||
local fname=$(basename "$ifile")
|
||||
local segment=$(realpath --relative-to="$where" "$ifile")
|
||||
|
||||
|
||||
if [ "$flatten" = true ]; then
|
||||
output_file="$output_dir/$fname.$container"
|
||||
else
|
||||
output_file="$output_dir/$segment.$container"
|
||||
fi
|
||||
|
||||
tmp_file=$(mktemp)
|
||||
|
||||
echo "Running Command: ffmpeg -y -i $ifile $flags $output_file" >> "$tmp_file"
|
||||
|
||||
mkdir -p "$(dirname "$output_file")"
|
||||
|
||||
if ffmpeg -y -i "$ifile" $(echo -n "$flags") "$output_file" 2>> "$tmp_file";
|
||||
then
|
||||
rm -f "$tmp_file"
|
||||
else
|
||||
# gum log "Failed to transcode $ifile. Check ./error.log for details."
|
||||
cat "$tmp_file" >> error.log
|
||||
rm -f "$tmp_file"
|
||||
|
||||
fi
|
||||
}
|
||||
export -f transcode_job
|
||||
|
||||
mapfile -t files <<< "$selection"
|
||||
len=${#files[@]}
|
||||
i=1
|
||||
for file in "${files[@]}"; do
|
||||
if [[ -f "$file" ]]; then
|
||||
gum spin --spinner dot --title "[$i/$len] Transcoding $file" -- bash -c "source <(declare -f transcode_job); transcode_job \"$file\" \"$output_dir\" \"$flatten\" \"$where\" \"$flags\" \"$container\""
|
||||
else
|
||||
echo "Skipping invalid file: $file" >&2
|
||||
fi
|
||||
((i++))
|
||||
done
|
||||
12
modules/tools/bulk-transcode/default.nix
Normal file
12
modules/tools/bulk-transcode/default.nix
Normal file
@@ -0,0 +1,12 @@
|
||||
{self, ...}: {
|
||||
flake.overlays.bulk-transcode = final: prev: {
|
||||
bulk-transcode = final.callPackage ./_derivation.nix {};
|
||||
};
|
||||
flake.nixosModules.bulk-transcode-overlay = {
|
||||
nixpkgs.overlays = [self.overlays.bulk-transcode];
|
||||
};
|
||||
perSystem = {pkgs, ...}: {
|
||||
packages.bulk-transcode = pkgs.callPackage ./_derivation.nix {};
|
||||
devShells.bulk-transcode = import ./_shell.nix {inherit pkgs;};
|
||||
};
|
||||
}
|
||||
40
modules/tools/crossover/_derivation.nix
Normal file
40
modules/tools/crossover/_derivation.nix
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
appimageTools,
|
||||
fetchurl,
|
||||
makeWrapper,
|
||||
...
|
||||
}: let
|
||||
pname = "crossover";
|
||||
version = "3.1.5";
|
||||
src = fetchurl {
|
||||
url = "https://github.com/lacymorrow/crossover/releases/download/v${version}/CrossOver-${version}-x86_64.AppImage";
|
||||
sha256 = "sha256-64RPal8n1PJh1LB+CTyNFt04Pw1lVgcsyc63S8yQ/DA=";
|
||||
};
|
||||
appimageContents = appimageTools.extract {
|
||||
inherit pname version src;
|
||||
};
|
||||
in
|
||||
appimageTools.wrapType2 {
|
||||
inherit pname version src;
|
||||
|
||||
nativeBuildInputs = [makeWrapper];
|
||||
extraInstallCommands = ''
|
||||
wrapProgram $out/bin/${pname} --add-flags "--no-sandbox"
|
||||
|
||||
# Create a minimal .desktop file manually
|
||||
mkdir -p $out/share/applications
|
||||
cat > $out/share/applications/${pname}.desktop <<EOF
|
||||
[Desktop Entry]
|
||||
Name=${pname}
|
||||
Exec=${pname} %U
|
||||
Icon=${pname}
|
||||
Type=Application
|
||||
Categories=Utility;
|
||||
EOF
|
||||
|
||||
# Optionally extract icon from AppImage (if available)
|
||||
# You can also manually install an icon here:
|
||||
mkdir -p $out/share/icons/hicolor/0x0/apps
|
||||
cp ${appimageContents}/usr/share/icons/hicolor/0x0/apps/${pname}.png $out/share/icons/hicolor/0x0/apps/${pname}.png || true
|
||||
'';
|
||||
}
|
||||
11
modules/tools/crossover/default.nix
Normal file
11
modules/tools/crossover/default.nix
Normal file
@@ -0,0 +1,11 @@
|
||||
{self, ...}: {
|
||||
flake.overlays.crossover = final: prev: {
|
||||
crossover = final.callPackage ./_derivation.nix {};
|
||||
};
|
||||
flake.nixosModules.crossover-overlay = {
|
||||
nixpkgs.overlays = [self.overlays.crossover];
|
||||
};
|
||||
perSystem = {pkgs, ...}: {
|
||||
packages.crossover = pkgs.callPackage ./_derivation.nix {};
|
||||
};
|
||||
}
|
||||
37
modules/tools/jj.nix
Normal file
37
modules/tools/jj.nix
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
flake.homeModules.jj = {
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
cfg = config.hive.jj;
|
||||
in {
|
||||
options.hive.jj = {
|
||||
followGit = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "Follow the current git configuration";
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
home.packages = with pkgs; [
|
||||
jujutsu
|
||||
];
|
||||
|
||||
programs.jujutsu = {
|
||||
enable = true;
|
||||
settings = {
|
||||
user = lib.optionalAttrs cfg.followGit {
|
||||
name = config.programs.git.settings.user.name;
|
||||
email = config.programs.git.settings.user.email;
|
||||
};
|
||||
ui = lib.optionalAttrs (cfg.followGit && config.programs.difftastic.enable) {
|
||||
diff-formatter = ["${pkgs.difftastic}/bin/difft" "--color=always" "$left" "$right"];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
102
modules/tools/nix-scripts.nix
Normal file
102
modules/tools/nix-scripts.nix
Normal file
@@ -0,0 +1,102 @@
|
||||
{...}: let
|
||||
home-rebuild = pkgs:
|
||||
pkgs.writeShellScriptBin ".home-rebuild"
|
||||
''
|
||||
set -e
|
||||
pushd ~/.hive/
|
||||
${pkgs.alejandra}/bin/alejandra . &>/dev/null
|
||||
${pkgs.git}/bin/git diff -U0
|
||||
echo "NixOS Rebuilding..."
|
||||
home-manager switch --flake ~/.hive -b backup --log-format internal-json |& ${pkgs.nix-output-monitor}/bin/nom --json
|
||||
gen=$(home-manager generations | head -n1 | ${pkgs.gawk}/bin/awk '{print "Gen" $5 " @ " $1 "-" $2}')
|
||||
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
|
||||
${pkgs.git}/bin/git commit --no-gpg-sign -am "Home $gen by $by"
|
||||
popd
|
||||
'';
|
||||
rebuild = pkgs:
|
||||
pkgs.writeShellScriptBin ".nixos-rebuild"
|
||||
''
|
||||
set -e
|
||||
pushd ~/.hive/
|
||||
${pkgs.alejandra}/bin/alejandra . &>/dev/null
|
||||
${pkgs.git}/bin/git diff -U0
|
||||
echo "NixOS Rebuilding..."
|
||||
${pkgs.nh}/bin/nh os switch ~/.hive
|
||||
gen=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | ${pkgs.gnugrep}/bin/grep current | ${pkgs.gawk}/bin/awk '{print "Gen" $1 " @ " $2 "-" $3}')
|
||||
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
|
||||
${pkgs.git}/bin/git commit --no-gpg-sign -am "System $gen by $by"
|
||||
popd
|
||||
'';
|
||||
upgrade = pkgs:
|
||||
pkgs.writeShellScriptBin ".nixos-upgrade"
|
||||
''
|
||||
set -e
|
||||
pushd ~/.hive/
|
||||
if [ -n "$(${pkgs.git}/bin/git status --porcelain)" ]; then
|
||||
echo ".hive is unclean!"
|
||||
exit 1
|
||||
fi
|
||||
branch_staging="staging-update"
|
||||
|
||||
if ${pkgs.git}/bin/git rev-parse --verify "$branch_staging" >/dev/null 2>&1; then
|
||||
echo "Using staging update branch."
|
||||
else
|
||||
echo "No staging update branch found."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
${pkgs.git}/bin/git checkout "$branch_staging" flake.lock
|
||||
echo "Updating nix-flake..."
|
||||
nix flake update --flake .
|
||||
echo "NixOS Rebuilding..."
|
||||
${pkgs.nh}/bin/nh os switch ~/.hive
|
||||
gen=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | ${pkgs.gnugrep}/bin/grep current | ${pkgs.gawk}/bin/awk '{print "Gen" $1 " @ " $2 "-" $3}')
|
||||
by="$(${pkgs.coreutils-full}/bin/whoami)@$(${pkgs.nettools}/bin/hostname)"
|
||||
${pkgs.git}/bin/git commit --no-gpg-sign -am "Upgrade $gen by $by"
|
||||
${pkgs.git}/bin/git branch -D "$branch_staging"
|
||||
popd
|
||||
'';
|
||||
update = pkgs:
|
||||
pkgs.writeShellScriptBin ".nixos-update"
|
||||
''
|
||||
set -e
|
||||
pushd ~/.hive/
|
||||
if [ -n "$(${pkgs.git}/bin/git status --porcelain)" ]; then
|
||||
echo ".hive is unclean!"
|
||||
exit 1
|
||||
fi
|
||||
branch_staging="staging-update"
|
||||
branch_current="$(${pkgs.git}/bin/git branch --show-current)"
|
||||
|
||||
if ${pkgs.git}/bin/git rev-parse --verify "$branch_staging" >/dev/null 2>&1; then
|
||||
echo "There is already a staging update branch."
|
||||
else
|
||||
echo "Creating a new staging update branch."
|
||||
${pkgs.git}/bin/git switch -c "$branch_staging"
|
||||
nix flake update --verbose --flake .
|
||||
${pkgs.git}/bin/git add flake.lock
|
||||
${pkgs.git}/bin/git commit --no-gpg-sign -m "staging update"
|
||||
${pkgs.git}/bin/git switch "$branch_current"
|
||||
fi
|
||||
|
||||
nix store --log-format internal-json -v diff-closures \
|
||||
'.?ref='"$branch_current"'#nixosConfigurations.'"$(${pkgs.hostname}/bin/hostname)"'.config.system.build.toplevel' \
|
||||
'.?ref='"$branch_staging"'#nixosConfigurations.'"$(${pkgs.hostname}/bin/hostname)"'.config.system.build.toplevel' \
|
||||
|& ${pkgs.nix-output-monitor}/bin/nom --json
|
||||
|
||||
popd
|
||||
'';
|
||||
in {
|
||||
flake.nixosModules.nix-scripts = {pkgs, ...}: {
|
||||
environment.systemPackages = [
|
||||
(rebuild pkgs)
|
||||
(upgrade pkgs)
|
||||
(update pkgs)
|
||||
];
|
||||
};
|
||||
flake.homeModules.nix-scripts = {pkgs, ...}: {
|
||||
home.packages = [
|
||||
(home-rebuild pkgs)
|
||||
];
|
||||
};
|
||||
}
|
||||
1
modules/tools/spotify-shortcuts/.envrc
Normal file
1
modules/tools/spotify-shortcuts/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use flake ../../#spotify-shortcuts --show-trace
|
||||
9
modules/tools/spotify-shortcuts/_derivation.nix
Normal file
9
modules/tools/spotify-shortcuts/_derivation.nix
Normal file
@@ -0,0 +1,9 @@
|
||||
{python3Packages}:
|
||||
with python3Packages;
|
||||
buildPythonApplication {
|
||||
name = "spotify-shortcuts";
|
||||
propagatedBuildInputs = [spotipy pyxdg desktop-notifier];
|
||||
pyproject = true;
|
||||
build-system = [setuptools];
|
||||
src = ./.;
|
||||
}
|
||||
15
modules/tools/spotify-shortcuts/_shell.nix
Normal file
15
modules/tools/spotify-shortcuts/_shell.nix
Normal file
@@ -0,0 +1,15 @@
|
||||
{pkgs ? import <nixpkgs> {}}: let
|
||||
drv = pkgs.callPackage ./derivation.nix {};
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [
|
||||
pkgs.pyright
|
||||
pkgs.black
|
||||
];
|
||||
|
||||
inputsFrom = [drv];
|
||||
|
||||
shellHook = ''
|
||||
export PYTHONPATH="$PYTHONPATH:$(pwd)"
|
||||
'';
|
||||
}
|
||||
56
modules/tools/spotify-shortcuts/default.nix
Normal file
56
modules/tools/spotify-shortcuts/default.nix
Normal file
@@ -0,0 +1,56 @@
|
||||
{self, ...}: {
|
||||
flake.overlays.spotify-shortcuts = final: prev: {
|
||||
spotify-shortcuts = final.callPackage ./_derivation.nix {};
|
||||
};
|
||||
perSystem = {pkgs, ...}: {
|
||||
packages.spotify-shortcuts = pkgs.callPackage ./_derivation.nix {};
|
||||
devShells.spotify-shortcuts = import ./_shell.nix {inherit pkgs;};
|
||||
};
|
||||
|
||||
flake.nixosModules.spotify-shortcuts-overlay = {
|
||||
nixpkgs.overlays = [
|
||||
self.overlays.spotify-shortcuts
|
||||
];
|
||||
};
|
||||
|
||||
flake.nixosModules.spotify-shortcuts = {
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}: let
|
||||
cfg = config.hive.programs.spotify-shortcuts;
|
||||
in {
|
||||
options.hive.programs.spotify-shortcuts = {
|
||||
enable = lib.mkEnableOption "Enable Spotify Shortcuts";
|
||||
clientIdSopsKey = lib.mkOption {
|
||||
type = lib.types.singleLineStr;
|
||||
description = "Spotify API Client ID sops secret name";
|
||||
};
|
||||
clientSecretSopsKey = lib.mkOption {
|
||||
type = lib.types.singleLineStr;
|
||||
description = "Spotify API Client Secret Path sops secret name";
|
||||
};
|
||||
};
|
||||
|
||||
imports = [
|
||||
self.nixosModules.spotify-shortcuts-overlay
|
||||
];
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
environment.systemPackages = [pkgs.spotify-shortcuts];
|
||||
environment.variables = {
|
||||
SPOTIFY_SHORTCUTS_CONFIG = config.sops.templates."spotify-shortcuts-client.json".path;
|
||||
};
|
||||
sops.templates."spotify-shortcuts-client.json" = {
|
||||
mode = "444";
|
||||
content = ''
|
||||
{
|
||||
"clientId": "${config.sops.placeholder.${cfg.clientIdSopsKey}}",
|
||||
"clientSecret": "${config.sops.placeholder.${cfg.clientSecretSopsKey}}"
|
||||
}
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
16
modules/tools/spotify-shortcuts/setup.py
Normal file
16
modules/tools/spotify-shortcuts/setup.py
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name="spotify_shortcuts",
|
||||
version="1.0",
|
||||
packages=find_packages(),
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"spotisc=spotify_shortcuts.run:main",
|
||||
"spotify-like=spotify_shortcuts.spotify_like:main",
|
||||
"spotify-pl-add=spotify_shortcuts.spotify_pl_add:main",
|
||||
],
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
from spotify_shortcuts.run import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
74
modules/tools/spotify-shortcuts/spotify_shortcuts/config.py
Normal file
74
modules/tools/spotify-shortcuts/spotify_shortcuts/config.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
from argparse import ArgumentParser
|
||||
from typing import List
|
||||
from os import getenv
|
||||
from sys import argv
|
||||
from xdg.BaseDirectory import xdg_cache_home
|
||||
from json import loads
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
cache_file: Path = Path(xdg_cache_home) / Path("spotify-shortcuts.json")
|
||||
client_id: str | None = None
|
||||
client_secret: str | None = None
|
||||
notifications: bool = True
|
||||
|
||||
@staticmethod
|
||||
def from_file(path: Path):
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"Configuration file {path} does not exist.")
|
||||
with open(path, "r") as f:
|
||||
data = loads(f.read())
|
||||
|
||||
return Config(
|
||||
cache_file=Path(data.get("cacheFile", Config.cache_file)),
|
||||
client_id=data.get("clientId", Config.client_id),
|
||||
client_secret=data.get("clientSecret", Config.client_secret),
|
||||
)
|
||||
|
||||
|
||||
def load_config(args: List[str] = argv[1:]) -> Config:
|
||||
parser = ArgumentParser(description="Spotify CLI Tool")
|
||||
parser.add_argument(
|
||||
"--cache-file",
|
||||
type=Path,
|
||||
default=Config.cache_file,
|
||||
help="Path to the cache file for Spotify authentication",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--client-id",
|
||||
type=str,
|
||||
default=Config.client_id,
|
||||
help="Spotify API Client ID",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--client-secret",
|
||||
type=str,
|
||||
default=Config.client_secret,
|
||||
help="Spotify API Client Secret",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--config-file",
|
||||
type=str,
|
||||
help="Path to a json configuration file with keys clientId and clientSecret",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-notifications",
|
||||
action="store_true",
|
||||
help="Disable desktop notifications",
|
||||
)
|
||||
|
||||
ns = parser.parse_args(args)
|
||||
|
||||
cfg = Config()
|
||||
if (cfg_file := ns.config_file or getenv("SPOTIFY_SHORTCUTS_CONFIG", None)) != None:
|
||||
cfg = Config.from_file(Path(cfg_file))
|
||||
|
||||
return Config(
|
||||
cache_file=ns.cache_file or cfg.cache_file,
|
||||
client_id=ns.client_id or cfg.client_id,
|
||||
client_secret=ns.client_secret or cfg.client_secret,
|
||||
notifications=not ns.no_notifications or cfg.notifications,
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
from spotify_shortcuts.spotify_like import SpotifyLike
|
||||
from spotify_shortcuts.spotify_pl_add import SpotifyPlAdd
|
||||
|
||||
SHORTCUT_REGISTRY = {
|
||||
"like": SpotifyLike(),
|
||||
"pl_add": SpotifyPlAdd(),
|
||||
}
|
||||
47
modules/tools/spotify-shortcuts/spotify_shortcuts/run.py
Normal file
47
modules/tools/spotify-shortcuts/spotify_shortcuts/run.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from spotify_shortcuts.config import Config, load_config
|
||||
from spotify_shortcuts.shortcut import Shortcut
|
||||
from spotify_shortcuts.spotify_auth import authenticated_session
|
||||
from itertools import chain
|
||||
|
||||
|
||||
def all_scopes() -> list[str]:
|
||||
from spotify_shortcuts.registry import SHORTCUT_REGISTRY
|
||||
|
||||
return list(
|
||||
set(
|
||||
chain.from_iterable(
|
||||
shortcut.get_scopes() for shortcut in SHORTCUT_REGISTRY.values()
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def run_shortcut(shortcut: Shortcut, config: Config):
|
||||
client = authenticated_session(
|
||||
config, scopes=all_scopes() # use all scopes to avoid re-authentication
|
||||
)
|
||||
|
||||
shortcut.execute(client, config)
|
||||
|
||||
|
||||
def main():
|
||||
from spotify_shortcuts.registry import SHORTCUT_REGISTRY
|
||||
import sys
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: {sys.argv[0]} <shortcut_name>")
|
||||
sys.exit(1)
|
||||
|
||||
shortcut_name = sys.argv[1]
|
||||
if shortcut_name not in SHORTCUT_REGISTRY:
|
||||
print(f"Shortcut '{shortcut_name}' not found.")
|
||||
sys.exit(1)
|
||||
|
||||
shortcut = SHORTCUT_REGISTRY[shortcut_name]
|
||||
config = load_config(args=sys.argv[2:])
|
||||
|
||||
run_shortcut(shortcut, config)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,21 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from spotify_shortcuts.config import Config
|
||||
from spotipy import Spotify
|
||||
|
||||
|
||||
class Shortcut(ABC):
|
||||
@abstractmethod
|
||||
def execute(self, client: Spotify, config: Config):
|
||||
"""Execute the shortcut action."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_help(self) -> str:
|
||||
"""Return a description of the shortcut."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_scopes(self) -> list[str]:
|
||||
"""Return the spotify API scopes required for the shortcut."""
|
||||
pass
|
||||
@@ -0,0 +1,20 @@
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
from spotipy import Spotify
|
||||
from spotify_shortcuts.config import Config
|
||||
|
||||
CALLBACK_URI = "http://127.0.0.1:45632/callback"
|
||||
|
||||
|
||||
def authenticated_session(cfg: Config, scopes: list[str]) -> Spotify:
|
||||
assert cfg.client_id, "Spotify client ID is required"
|
||||
assert cfg.client_secret, "Spotify client secret is required"
|
||||
|
||||
return Spotify(
|
||||
auth_manager=SpotifyOAuth(
|
||||
client_id=cfg.client_id,
|
||||
client_secret=cfg.client_secret,
|
||||
redirect_uri=CALLBACK_URI,
|
||||
scope=" ".join(scopes),
|
||||
cache_path=cfg.cache_file,
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,46 @@
|
||||
from spotify_shortcuts.run import run_shortcut
|
||||
from spotify_shortcuts.shortcut import Shortcut
|
||||
from spotify_shortcuts.config import load_config
|
||||
from desktop_notifier import DesktopNotifierSync
|
||||
|
||||
SCOPES = [
|
||||
"user-library-read",
|
||||
"user-library-modify",
|
||||
"user-read-playback-state",
|
||||
]
|
||||
|
||||
|
||||
class SpotifyLike(Shortcut):
|
||||
def execute(self, client, config):
|
||||
if (playback := client.current_playback()) is None:
|
||||
print("No current playback found.")
|
||||
return
|
||||
|
||||
if (uri := playback.get("item", {}).get("uri", None)) is None:
|
||||
print("No track URI found in current playback.")
|
||||
return
|
||||
|
||||
client.current_user_saved_tracks_add(tracks=[uri])
|
||||
|
||||
if config.notifications:
|
||||
dn = DesktopNotifierSync()
|
||||
dn.send(
|
||||
title="Track Liked",
|
||||
message=f"Track \"{playback.get('item', {}).get('name', '<no-name>')}\" by \"{
|
||||
", ".join(a.get('name', '<no-name>') for a in playback.get('item', {}).get('artists', []))
|
||||
}\" has been liked.",
|
||||
)
|
||||
|
||||
def get_help(self) -> str:
|
||||
return "Like the currently playing track."
|
||||
|
||||
def get_scopes(self) -> list[str]:
|
||||
return SCOPES
|
||||
|
||||
|
||||
def main():
|
||||
run_shortcut(SpotifyLike(), load_config())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,57 @@
|
||||
from spotify_shortcuts.shortcut import Shortcut
|
||||
from spotify_shortcuts.config import load_config
|
||||
from desktop_notifier import DesktopNotifierSync
|
||||
from spotify_shortcuts.run import run_shortcut
|
||||
|
||||
SCOPES = [
|
||||
"playlist-modify-public",
|
||||
"playlist-modify-private",
|
||||
"user-read-playback-state",
|
||||
]
|
||||
|
||||
|
||||
class SpotifyPlAdd(Shortcut):
|
||||
def execute(self, client, config):
|
||||
if (playback := client.current_playback()) is None:
|
||||
print("No current playback found.")
|
||||
return
|
||||
|
||||
if (track_uri := playback.get("item", {}).get("uri", None)) is None:
|
||||
print("No track URI found in current playback.")
|
||||
return
|
||||
|
||||
if (context_uri := playback.get("context", {}).get("uri", None)) is None:
|
||||
print("No context URI found in current playback.")
|
||||
return
|
||||
|
||||
client.playlist_add_items(context_uri, items=[track_uri])
|
||||
|
||||
if config.notifications:
|
||||
track_name = playback.get("item", {}).get("name", "<no-name>")
|
||||
artists = ", ".join(
|
||||
a.get("name", "<no-name>")
|
||||
for a in playback.get("item", {}).get("artists", [])
|
||||
)
|
||||
playlist_name = (client.playlist(context_uri) or {}).get(
|
||||
"name", "<no-name>"
|
||||
)
|
||||
|
||||
dn = DesktopNotifierSync()
|
||||
dn.send(
|
||||
title="Track Added to Playlist",
|
||||
message=f'Track "{track_name}" by "{artists}" has been added to {playlist_name}.',
|
||||
)
|
||||
|
||||
def get_help(self) -> str:
|
||||
return "Add the currently playing track to the current playlist."
|
||||
|
||||
def get_scopes(self) -> list[str]:
|
||||
return SCOPES
|
||||
|
||||
|
||||
def main():
|
||||
run_shortcut(SpotifyPlAdd(), load_config())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user