forked from
lass.pds.lassul.us/superconfig
This repository has no description
4.4 kB
142 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 if [[ -f "$filename" ]]; then
67 echo "File already exists: $filename (skipping download)"
68 else
69 echo "$FLAX_URL/$file" >> "$TMPFILE"
70 fi
71 FILES_ARRAY+=("$filename")
72 fi
73 done <<< "$SELECTED"
74
75 # Only start downloads if there are files to download
76 if [[ -s "$TMPFILE" ]]; then
77 echo "Starting background downloads..."
78 # Start aria2c in background with progress bar
79 aria2c --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE" --console-log-level=warn &
80 ARIA_PID=$!
81 else
82 echo "All selected files already exist, nothing to download."
83 ARIA_PID=""
84 fi
85
86 # Play files as they download
87 for file in "${FILES_ARRAY[@]}"; do
88 if [[ -f "$file" ]]; then
89 echo "Playing existing file: $file"
90 mpv "$file"
91 else
92 echo "Waiting for $file to start downloading..."
93 # Wait for file to exist and have some content
94 while [[ ! -f "$file" ]] || [[ ! -s "$file" ]]; do
95 sleep 0.5
96 # Check if aria2c is still running (only if we started it)
97 if [[ -n "$ARIA_PID" ]] && ! kill -0 $ARIA_PID 2>/dev/null; then
98 echo "Download process ended unexpectedly"
99 break 2
100 fi
101 done
102
103 # Give it a moment to buffer
104 sleep 1
105
106 echo "Playing: $file"
107 mpv "$file"
108 fi
109 done
110
111 # Wait for all downloads to complete (only if we started downloads)
112 if [[ -n "$ARIA_PID" ]]; then
113 wait $ARIA_PID
114 fi
115else
116 # Normal mode: download all files with aria2c
117 # Create a temporary file with URLs for aria2c
118 TMPFILE=$(mktemp)
119 trap 'rm -f "$TMPFILE"' EXIT
120
121 while IFS= read -r file; do
122 if [[ -n "$file" ]]; then
123 # Extract just the filename from the path
124 filename=$(basename "$file")
125 if [[ -f "$filename" ]]; then
126 echo "File already exists: $filename (skipping download)"
127 else
128 echo "$FLAX_URL/$file" >> "$TMPFILE"
129 fi
130 fi
131 done <<< "$SELECTED"
132
133 # Only download if there are files to download
134 if [[ -s "$TMPFILE" ]]; then
135 echo "Downloading selected files..."
136 aria2c --http-user="$FLAX_USER" --http-passwd="$FLAX_PASS" -i "$TMPFILE"
137 else
138 echo "All selected files already exist, nothing to download."
139 fi
140fi
141
142echo "Download complete!"