Added cache and better error handling.
[utils.git] / autodlctl
CommitLineData
aba77738
DC
1#!/bin/bash
2
3paths=(/home/pub/video/anime $HOME/dc/autodl/cur)
4
5function getnext
6{
7 echo "checking $tag"
8 sexpr="`cat "$d/.autodl/sexpr"`"
9 unset badsizes
10 if [ -r "$d/.autodl/badlist" ]; then
11 read curep <"$d/.autodl/badlist"
12 echo "downloading bad files, curep $curep"
13 epfrom=badlist
aba77738
DC
14 elif [ -r "$d/.autodl/curep" ]; then
15 curep="`cat "$d/.autodl/curep"`"
16 echo "downloading series, curep $curep"
17 epfrom=curep
18 else
19 echo "no available episode of $tag" >&2
20 echo "$tag" >>"$HOME/dc/autodl/faulty"
21 return 1
22 fi
6c554f39 23 unset badsizesl
24 if [ -r "$d/.autodl/badsizes" ]; then
25 badsizesl="$(sed -n "s/^$curep \(.*\)$/\1/p" "$d/.autodl/badsizes")"
26 fi
45f2d3f3
DC
27 if [ -n "$badsizesl" ]; then
28 read -a badsizes <<<"$badsizesl"
29 unset badsizesl
30 echo "found bad size list: ${badsizes[@]}"
31 fi
aba77738
DC
32 unset args
33 fsexpr="`printf "$sexpr" "$curep"`"
34 if [ "${#badsizes[@]}" -gt 0 ]; then
35 for badsize in "${badsizes[@]}"; do
36 fsexpr="$fsexpr & ! S=$badsize"
37 done
38 fi
34091d15
DC
39 infofile="$d/.autodl/rtinfo"
40 args=(-e "$fsexpr" -t "$tag $curep" -I "$infofile")
aba77738
DC
41 if [ -r "$d/.autodl/uarg" ]; then
42 uarg="`cat "$d/.autodl/uarg"`"
43 elif [ -e "$d/.autodl/autouarg" ]; then
44 uarg="rename:$tag - %02i.avi:move:../autodl/cur/$tag"
45 fi
46 if [ -n "$uarg" ]; then
47 fuarg="`printf "$uarg" "$curep"`"
48 args=("${args[@]}" -a "$fuarg")
49 fi
50 outfile="`mktemp /tmp/autodlXXXXXX`"
51 echo "trying to download -- autodl ${args[@]}"
52 intr=n
53 autodl "${args[@]}" >"$outfile" 2>&1 &
54 pid=$!
55 trap "intr=y; kill -INT $pid" USR1 INT
56 wait $pid
57 stat=$?
58 if [ "$intr" = y ]; then
59 echo "$tag interrupted"
60 else
61 if [ "$stat" -ne 0 ]; then
62 echo "Failure for $tag" >>"$HOME/dc/autodl/errorlog"
63 tail -n 20 "$outfile" >>"$HOME/dc/autodl/errorlog"
64 else
65 echo "episode $curep of $tag done"
66 case "$epfrom" in
67 badlist)
68 echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/baddone"
69 egrep -v "^$curep( |\$)" "$d/.autodl/badlist" >"$d/.autodl/newbadlist"
70 mv -f "$d/.autodl/newbadlist" "$d/.autodl/badlist"
71 if [ `wc -l <"$d/.autodl/badlist"` -eq 0 ]; then
72 rm "$d/.autodl/badlist"
73 if [ -r "$d/.autodl/curep" ]; then
74 if [ -r "$d/.autodl/maxep" ] && [ "`cat "$d/.autodl/curep"`" -gt "`cat "$d/.autodl/maxep"`" ]; then
75 touch "$d/.autodl/disabled"
76 fi
77 else
78 touch "$d/.autodl/disabled"
79 fi
80 echo "$tag has no more bad episodes"
81 echo "$tag" >>"$HOME/dc/autodl/badmaxed"
82 fi
83 ;;
84 curep)
85 echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/done"
86 let curep++
87 echo "$curep" >"$d/.autodl/curep"
88 if [ -r "$d/.autodl/maxep" ]; then
89 if [ "$curep" -gt "`cat "$d/.autodl/maxep"`" ]; then
90 touch "$d/.autodl/disabled"
91 echo "$tag has reached max"
92 echo "$tag" >>"$HOME/dc/autodl/maxed"
93 fi
94 fi
95 ;;
96 esac
97 fi
98 fi
99 rm -f "$outfile"
100 rm -f "$HOME/dc/autodl/run/$tag"
101}
102
103for dir in $HOME/dc/autodl{,/cur,/run}; do
104 if [ -e "$dir" ]; then
105 if [ ! -d "$dir" ]; then
106 echo "$dir is not a directory, please remedy and restart" >&2
107 exit 1
108 fi
109 else
110 mkdir "$dir" || exit 1
111 fi
112done
113
114while [ $# -gt 0 ]; do
115 arg="$1"
116 shift
117 case "$arg" in
118 -k)
119 pid="$(cat "$HOME/dc/autodl/run/master")"
120 if [ -z "$pid" ]; then
121 echo "autodlctl: could not read a PID from $HOME/dc/autodl/run/master" >&2
122 exit 1
123 fi
124 kill "$pid"
125 exit 0
126 ;;
127 *)
128 echo "autodlctl: unrecognized option: \"$arg\"" >&2
129 exit 1
130 ;;
131 esac
132done
133
134lastget=0
135done=n
136trap "done=y" INT QUIT TERM
137echo $$ >"$HOME/dc/autodl/run/master"
138while [ "$done" != y ]; do
139 for pidfile in $HOME/dc/autodl/run/*; do
140 if [ "$pidfile" = "$HOME/dc/autodl/run/*" ]; then break; fi
141 pid="`cat "$pidfile"`"
142 if [ -d /proc/1 -a ! -d "/proc/$pid" ]; then
143 echo "removing stale pidfile $pidfile"
144 rm -f "$pidfile"
145 fi
146 done
147 for p in "${paths[@]}"; do
148 for d in "$p"/*; do
149 if [ -d "$d/.autodl" -a ! -e "$d/.autodl/disabled" ]; then
150 if [ -r "$d/.autodl/tag" ]; then
151 tag="`cat "$d/.autodl/tag"`"
152 else
153 tag="`basename "$d"`"
154 fi
155 if [ -e "$d/.autodl/disable" ]; then
156 echo "disabling $tag per user request"
157 touch "$d/.autodl/disabled"
158 rm -f "$d/.autodl/disable"
159 if [ -r "$HOME/dc/autodl/run/$tag" ]; then
160 pid="`cat "$HOME/dc/autodl/run/$tag"`"
161 echo "sending SIGUSR1 to $pid"
162 kill -USR1 "$pid"
163 else
164 echo "could not find pid for $tag"
165 fi
166 elif [ ! -r "$d/.autodl/sexpr" ]; then
167 touch "$d/.autodl/disabled"
168 echo "$tag lacks sexpr" >&2
169 echo "$tag" >>"$HOME/dc/autodl/faulty"
170 else
171 if [ ! -e "$HOME/dc/autodl/run/$tag" ]; then
172 if [ $((`date +%s` - $lastget)) -gt 20 ]; then
173 getnext "$d" "$tag" &
174 lastget=`date +%s`
175 pid=$!
176 echo "$pid" >"$HOME/dc/autodl/run/$tag"
177 fi
178 fi
179 fi
180 fi
181 done
182 done
183 sleep 10
184done
185
186for pidfile in $HOME/dc/autodl/run/*; do
187 if [ "$pidfile" = "$HOME/dc/autodl/run/*" ]; then break; fi
188 if [ "$(basename "$pidfile")" = master ]; then continue; fi
189 pid="`cat "$pidfile"`"
190 echo "sending SIGUSR1 to $pid for `basename "$pidfile"`"
191 kill -USR1 "$pid"
192done
193
194rm -f "$HOME/dc/autodl/run/master"