This repository has no description
4.6 kB
148 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 # Only start downloads if there are files to download
72 if [[ -s "$TMPFILE" ]]; then
73 echo "Starting background downloads..."
74 # Start aria2c in background with progress bar
75 aria2c --continue=true --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE" --console-log-level=warn &
76 ARIA_PID=$!
77 else
78 echo "No files to download."
79 ARIA_PID=""
80 fi
81
82 # Play files as they download
83 for file in "${FILES_ARRAY[@]}"; do
84 if [[ -f "$file" ]]; then
85 echo "Playing existing file: $file"
86 mpv "$file"
87 else
88 echo "Waiting for $file to start downloading..."
89 # Wait for file to exist and have some content
90 while [[ ! -f "$file" ]] || [[ ! -s "$file" ]]; do
91 sleep 0.5
92 # Check if aria2c is still running (only if we started it)
93 if [[ -n "$ARIA_PID" ]] && ! kill -0 $ARIA_PID 2>/dev/null; then
94 echo "Download process ended unexpectedly"
95 break 2
96 fi
97 done
98
99 # Give it a moment to buffer
100 sleep 1
101
102 echo "Playing: $file"
103 # Try to play the file with retries
104 MAX_RETRIES=3
105 RETRY_DELAY=2
106 for ((retry=1; retry<=MAX_RETRIES; retry++)); do
107 if mpv "$file"; then
108 # mpv exited successfully
109 break
110 else
111 exit_code=$?
112 if [[ $retry -lt $MAX_RETRIES ]]; then
113 echo "Failed to open $file (attempt $retry/$MAX_RETRIES, exit code: $exit_code). Retrying in ${RETRY_DELAY}s..."
114 sleep $RETRY_DELAY
115 else
116 echo "Failed to open $file after $MAX_RETRIES attempts. Skipping."
117 fi
118 fi
119 done
120 fi
121 done
122
123 # Wait for all downloads to complete (only if we started downloads)
124 if [[ -n "$ARIA_PID" ]]; then
125 wait $ARIA_PID
126 fi
127else
128 # Normal mode: download all files with aria2c
129 # Create a temporary file with URLs for aria2c
130 TMPFILE=$(mktemp)
131 trap 'rm -f "$TMPFILE"' EXIT
132
133 while IFS= read -r file; do
134 if [[ -n "$file" ]]; then
135 echo "$FLAX_URL/$file" >> "$TMPFILE"
136 fi
137 done <<< "$SELECTED"
138
139 # Only download if there are files to download
140 if [[ -s "$TMPFILE" ]]; then
141 echo "Downloading selected files..."
142 aria2c --continue=true --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE"
143 else
144 echo "No files to download."
145 fi
146fi
147
148echo "Download complete!"