This repository has no description
3.3 kB
114 lines
1#!/usr/bin/env bash
2set -euo pipefail
3
4FLAX_URL="https://flax.lassul.us"
5FLAX_USER="krebs"
6STREAM_MODE=false
7
8# Parse arguments
9while [[ $# -gt 0 ]]; do
10 case "$1" in
11 -s|--stream)
12 STREAM_MODE=true
13 shift
14 ;;
15 *)
16 echo "Usage: $0 [-s|--stream]"
17 echo " -s, --stream Stream files to mpv while downloading"
18 exit 1
19 ;;
20 esac
21done
22
23# Try to get password from pass first
24if command -v pass >/dev/null 2>&1 && pass git ls-files | grep -qE "^www/flax\.lassul\.us/pass\.(gpg|age)$"; then
25 FLAX_PASS=$(pass show www/flax.lassul.us/pass)
26else
27 read -rsp "Password for $FLAX_USER@flax.lassul.us: " FLAX_PASS
28 echo
29fi
30
31echo "Fetching file list..."
32FILES=$(curl -s -u "$FLAX_USER:$FLAX_PASS" "$FLAX_URL/index")
33
34if [[ -z "$FILES" ]]; then
35 echo "Error: Failed to fetch file list or no files available"
36 exit 1
37fi
38
39SELECTED=$(echo "$FILES" | fzf --multi --header="Select files to download (TAB to select multiple, ENTER to confirm)")
40
41if [[ -z "$SELECTED" ]]; then
42 echo "No files selected"
43 exit 0
44fi
45
46if [[ "$STREAM_MODE" == "true" ]]; then
47 # Check if mpv is available
48 if ! command -v mpv >/dev/null 2>&1; then
49 echo "Error: mpv is not installed. Stream mode requires mpv."
50 exit 1
51 fi
52
53 # Stream mode: start aria2c in background to download all files
54 # while playing them sequentially as they become available
55
56 # Create a temporary file with URLs for aria2c
57 TMPFILE=$(mktemp)
58 trap 'rm -f "$TMPFILE"' EXIT
59
60 # Store filenames in an array for playback
61 declare -a FILES_ARRAY=()
62 while IFS= read -r file; do
63 if [[ -n "$file" ]]; then
64 # Extract just the filename from the path
65 filename=$(basename "$file")
66 echo "$FLAX_URL/$file" >> "$TMPFILE"
67 FILES_ARRAY+=("$filename")
68 fi
69 done <<< "$SELECTED"
70
71 echo "Starting background downloads..."
72 # Start aria2c in background with progress bar
73 aria2c --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE" --console-log-level=warn &
74 ARIA_PID=$!
75
76 # Play files as they download
77 for file in "${FILES_ARRAY[@]}"; do
78 echo "Waiting for $file to start downloading..."
79 # Wait for file to exist and have some content
80 while [[ ! -f "$file" ]] || [[ ! -s "$file" ]]; do
81 sleep 0.5
82 # Check if aria2c is still running
83 if ! kill -0 $ARIA_PID 2>/dev/null; then
84 echo "Download process ended unexpectedly"
85 break 2
86 fi
87 done
88
89 # Give it a moment to buffer
90 sleep 1
91
92 echo "Playing: $file"
93 mpv "$file"
94 done
95
96 # Wait for all downloads to complete
97 wait $ARIA_PID
98else
99 # Normal mode: download all files with aria2c
100 # Create a temporary file with URLs for aria2c
101 TMPFILE=$(mktemp)
102 trap 'rm -f "$TMPFILE"' EXIT
103
104 while IFS= read -r file; do
105 if [[ -n "$file" ]]; then
106 echo "$FLAX_URL/$file" >> "$TMPFILE"
107 fi
108 done <<< "$SELECTED"
109
110 echo "Downloading selected files..."
111 aria2c --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE"
112fi
113
114echo "Download complete!"