-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharia2c.sh
executable file
·81 lines (68 loc) · 1.8 KB
/
aria2c.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/aria2c.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-04-25T12:06:03+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
links_file="$HOME/Public/downloads/links.txt"
# help
script=$(basename "$0")
help="$script [-h/--help] -- wrapper script to download with aria2c
Usage:
$script [-a/--add] <urls>
Settings:
without given settings, continue downloading from links file
[-a/--add] = add urls to links file (don't restart aria2c)
<urls> = file links
Examples:
$script
$script \"https://link.xyz/download1.tar.gz\" \"https://link.xyz/download2.tar.gz\"
$script -a \"https://link.xyz/download1.tar.gz\" \"https://link.xyz/download2.tar.gz\"
Config:
links file = $links_file"
aria2c_stop() {
pid=$(pgrep -x aria2c$)
[ -n "$pid" ] \
&& kill "$pid" \
&& while pgrep -x aria2c >/dev/null; do
sleep .1
done
}
aria2c_start() {
aria2c \
--input-file="$links_file" \
--save-session="$links_file"
}
add_urls() {
urls=$(for url in "$@"; do
printf "%s\n" "$url" \
| grep -s -v "^-"
done
)
[ -s "$links_file" ] \
&& links=$(grep -v " gid=" "$links_file") \
&& printf "%s\n%s\n" "$links" "$urls" \
| tr -d '[:blank:]' \
| sort -u > "$links_file" \
&& return 0
printf "%s\n" "$urls" \
| tr -d '[:blank:]' > "$links_file"
}
case "$1" in
-h | --help)
printf "%s\n" "$help"
;;
-a | --add)
[ -n "$2" ] \
&& add_urls "$@"
;;
*)
aria2c_stop
[ -n "$1" ] \
&& add_urls "$@"
aria2c_start
;;
esac