threadwatcher only moves appropriate files now

This commit is contained in:
Alexander Bocken 2021-04-17 17:28:57 +02:00
parent 16c4ba37b6
commit d23a7cbd55
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8

View File

@ -16,6 +16,9 @@ DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
export DBUS_SESSION_BUS_ADDRESS
export DISPLAY=:0.0
getfilelist(){
true
}
scan(){
ping -q -c 1 1.1.1.1 > /dev/null || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { echo "No internet connection detected."; exit ;}
if [ $(wc -l < "$URLFILE") -gt 0 ]; then
@ -35,7 +38,7 @@ scan(){
curl -s -L "$json_url" | jq . > /tmp/content$$
if [ -z "$(</tmp/content$$)" ]; then
echo "Thread $url not found ($dl_location) deleting from cached list of threads to watch"
notify-send "threadwatcher" "Thread downloading ${$dl_location//\/home\/alex/~} is complete now."
notify-send "threadwatcher" "Thread downloading $(echo "$dl_location" | sed "s|$HOME|~|") is complete now."
continue
else
echo "$line" >> "$TMP_URLFILE"
@ -89,13 +92,28 @@ case "$1" in
"add") dl_location="$(makepathabsolute "$3")"
if grep -qP "^$2\t" "$URLFILE"; then
dl_location_already="$(grep -P "^$2\t" "$URLFILE" | cut -f2)"
notify-send "threadwatcher" "Thread already being watched. currently downloads to $dl_location_already"
choice="$(dmenuinput "If you want to change location of thread to download, please insert new directory now:")"
new_location="$(makepathabsolute "$choice")"
notify-send "threadwatcher" "Thread already being watched. currently downloads to $(echo "$dl_location_already" | sed "s|$HOME|~|")"
prompt "Do you want to change download directory to $3?" &&
new_location="$dl_location" ||
exit 0
[ -z "$new_location" ] && exit
sed -i "s|$dl_location_already|$new_location|" "$URLFILE"
mv "$dl_location_already"/* "$new_location"
## Move already downloaded files to new location
mkdir -p "$new_location"
url="$2"
json_url="$(echo "$url" | sed -E 's/boards\.(4chan|4channel)/a.4cdn/; s/$/.json/')"
curl -s -L "$json_url" | jq . > /tmp/content$$
files_json="$(jq '.posts[] | if has("filename") then {filename: "\(.no)_\(.filename)\(.ext)", location: "\(.tim)\(.ext)"} else null end ' < /tmp/content$$ | grep -vE '^null$')"
rm /tmp/content$$
files="$(paste <(echo "$files_json" | jq '.filename' | tr -d '"') <(echo "$files_json" | jq '.location' | tr -d '"'))"
echo "$files" | while read -r file_line; do
filename="$(echo "$file_line" | cut -f1 | tr ' ' '_')"
mv -v "$dl_location_already/$filename" "$new_location"
done
rmdir --ignore-fail-on-non-empty "$dl_location_already"
notify-send "threadwatcher" "already downloaded files moved to $new_location. New files will also be downloaded there"
else
printf "%s\t%s\n" "$2" "$dl_location" | tee -ai "$URLFILE"
echo "added $2 to threadwatcher list. Downloading to $dl_location"