summaryrefslogtreecommitdiff
path: root/home/wayland/pkgs/wayland-scripts/src/bin/pirowatch
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2026-02-08 22:56:12 +0530
committersinanmohd <sinan@sinanmohd.com>2026-02-08 22:56:12 +0530
commit7542b49a5f093ad0d22f3cd964cdbc7ce338aa94 (patch)
tree39cf94ee897caf4914751c2de7bdfbe36133379c /home/wayland/pkgs/wayland-scripts/src/bin/pirowatch
parent142a0ef7616c870e06850d266137a9799e09f8ea (diff)
feat(home/wayland/wayland-scripts): init new cmds
Diffstat (limited to 'home/wayland/pkgs/wayland-scripts/src/bin/pirowatch')
-rwxr-xr-xhome/wayland/pkgs/wayland-scripts/src/bin/pirowatch107
1 files changed, 107 insertions, 0 deletions
diff --git a/home/wayland/pkgs/wayland-scripts/src/bin/pirowatch b/home/wayland/pkgs/wayland-scripts/src/bin/pirowatch
new file mode 100755
index 0000000..97f703e
--- /dev/null
+++ b/home/wayland/pkgs/wayland-scripts/src/bin/pirowatch
@@ -0,0 +1,107 @@
+#!/bin/sh
+
+# dir where files are cached
+cache_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/pirowatch"
+# dir where torrent files are stored
+torrent_dir="${XDG_DOWNLOAD_DIR:-$HOME/Downloads}"
+torrent_port=49110
+dht_port=49130
+menu="bemenu"
+
+note()
+{
+ # usage: note "message"
+ : "${1:?}"
+
+ command -v "notify-send" 1>/dev/null &&
+ notify-send "󰎁 pirowatch" "$1"
+}
+
+die()
+{
+ # usage: die "reason" [exit_status]
+ : "${1:?}"
+
+ note "$1"
+
+ printf "\033[31;1merr: %b\033[0m\n" "$1"
+ exit "${2:-1}"
+}
+
+dep_check()
+{
+ # usage: dep_check "dep_1" ...
+ : "${1:?}"
+
+ for dep; do
+ command -v "$dep" 1>/dev/null ||
+ die "$dep not found, please install it" 127
+ done
+
+ unset dep
+}
+
+stream() {
+ # usage: stream "torrent"
+ : "${1:?}"
+
+ dep_check "webtorrent" "mpv"
+
+ command -v "notify-send" 1>/dev/null &&
+ note "connecting to peers, wait for few seconds"
+
+ if [ -n "$2" ]
+ then
+ webtorrent --mpv -o "$cache_dir" --torrent-port "$torrent_port" --dht-port "$dht_port" "$1" --select "$2"
+ else
+ webtorrent --mpv -o "$cache_dir" --torrent-port "$torrent_port" --dht-port "$dht_port" "$1"
+ fi ||
+ die "webtorrent failed to play"
+}
+
+set_index() {
+ # usage: get_index "torrent"
+ : "${1:?}"
+
+ dep_check "webtorrent" "$menu"
+
+ fetch=$(webtorrent -o "$cache_dir" --torrent-port "$torrent_port" --dht-port "$dht_port" "$1" --select |
+ grep -Ei "\.(mkv|mp4|webm|avi|mov|flv|flac|opus|ogg|mp3|wav) (.*)$")
+ if [ "$(echo "$fetch" | wc -l)" -gt 1 ]
+ then
+ fetch="$(printf "%s" "$fetch" | "$menu" -p "󰎁 " -l 25)" ||
+ die "empty selection" 66
+ fi
+
+ index="${fetch%%[[:space:]]*}"
+
+ unset fetch
+}
+
+main()
+{
+ torrent=
+ index="tobeset"
+
+ if [ "$1" = "-s" ]
+ then
+ index=
+ shift
+ fi
+ [ -z "$WAYLAND_DISPLAY" ] &&
+ menu="dmenu"
+
+ dep_check "$menu"
+
+ torrent="${1:-$torrent_dir/$(find "$torrent_dir" -type f -name "*.torrent" | sed 's/.*\///g' | "$menu" -p " " -l 25)}"
+ [ -z "${torrent##"${torrent_dir}"/}" ] &&
+ die "magnet or torrent file not entered" 66
+ [ -d "$cache_dir" ] ||
+ mkdir -p "$cache_dir"
+ [ "$index" = "tobeset" ] &&
+ set_index "$torrent"
+
+ stream "$torrent" "$index"
+}
+
+main "$@"