added threadwatcher tool

This commit is contained in:
Alexander Bocken 2021-04-07 15:23:34 +02:00
parent fed75ab9c2
commit e99cbf38a5
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8
2 changed files with 94 additions and 1 deletions

85
.local/bin/threadwatcher Executable file
View File

@ -0,0 +1,85 @@
#!/bin/bash
#A script that interacts with 4chans API to checks for media to download out of threads.
#It uses the file name used by the uploader.
#consider using it in a cronjob intermittently with something like
#*/10 * * * * /home/<yourname>/.local/bin/threadwatcher scan
THREADWATCHER_DIR=URLFILE=${XDG_DATA_HOME-:$HOME/.local/share}/4chan_watcher
URLFILE="$THREADWATCHER_DIR/threads"
TMP_URLFILE=/tmp/4chan_thread_watcher_tmp$$
[ -d "$THREADWATCHER_DIR" ] || mkdir -p "$THREADWATCHER_DIR"
[ -f "$URLFILE" ] || touch "$URLFILE"
scan(){
echo "SCANNING"
while read -r line; do
url="$(echo "$line" | cut -f1)"
echo "scanning $url"
dl_location="$(echo "$line" | cut -f2)"
echo "downloading to $dl_location"
mkdir -p "$dl_location"
json_url="$(echo "$url" | sed -E 's/boards\.(4chan|4channel)/a.4cdn/; s/$/.json/')"
curl -s -L "$json_url" | jq . > /tmp/content$$
#delete archived threads from watchlist
if [ -z "$(</tmp/content$$)" ]; then
echo "Thread $url not found ($dl_location) deleting from cached list of threads to watch"
continue
else
echo "$line" >> "$TMP_URLFILE"
fi
files_json="$(jq '.posts[] | if has("filename") then {filename: "\(.filename)\(.ext)", location: "\(.tim)\(.ext)", md5: .md5} else null end ' < /tmp/content$$ | grep -vE '^null$')"
echo "$files_json" | jq '.filename' | tr -d '"' >> /tmp/filename$$
echo "$files_json" | jq '.location' | tr -d '"' >> /tmp/location$$
echo "$files_json" | jq '.md5' | tr -d '"' >> /tmp/md5$$
paste /tmp/filename$$ /tmp/location$$ >> /tmp/tmp$$
files="$(paste /tmp/tmp$$ /tmp/md5$$)"
rm /tmp/tmp$$ /tmp/md5$$ /tmp/location$$ /tmp/filename$$ /tmp/content$$
#echo "$files"
echo "$files" | while read -r file_line; do
filename="$(echo "$file_line" | cut -f1)"
master_location="$(echo "$file_line" | cut -f2 | tr -d '"')"
filelocation="$dl_location/$filename"
correct_md5="$(echo "$file_line" | cut -f3)"
board="$(echo "$url" | cut -d '/' -f4)"
file_url="https://i.4cdn.org/$board/$master_location"
mkdir -p "$dl_location"
#TODO: implement actual md5 checksum check instead of just looking whether the file exists
if [ -f "$filelocation" ]; then
true
else
touch "$filelocation" #to keep atime order correct?
wget -q -O "$filelocation" "$file_url" &
echo downloading "$filelocation"
fi
done
done<"$URLFILE"
mv "$TMP_URLFILE" "$URLFILE"
}
case "$1" in
"add") if echo "$3" | grep -qE '^/'; then
dl_location="$3"
else
dl_location="$HOME/$3"
fi
if grep -q "^$2\t" "$URLFILE"; then
dl_location_already="$(grep "^$2\t" "$URLFILE" | cut -f2)"
notify-send "threadwatcher" "Thread already being watched. currently downloads to $dl_location_already"
new_location="$(dmenuinput "If you want to change location of thread to download, please insert new directory now:")"
sed -i "s|$dl_location_already|$new_location" "$URLFILE|"
mv "$dl_location_already" "$new_location"
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"
fi
scan;;
"scan") scan;;
"list") printf "Thread:\t\t\t\t\t\tDownload location:\n"
sed "s|$HOME|~|" "$URLFILE";;
"clean")
echo "Watchlist used up to now:"
cat "$URLFILE"
echo "Deleting..."
rm "$URLFILE"
touch "$URLFILE";;
esac

View File

@ -2,7 +2,12 @@
# Feed this script a link and it will give dmenu
# some choice programs to use to open it.
case "$(printf "mpv\\ncopy url\\nqueue download\\nqueue youtube-dl\\nbrowser\\nrip media\\ncast" | dmenu -i -p "Open link with what program?")" in
if echo "$1" | grep -q "4chan"; then
choice="$(printf "mpv\\ncopy url\\nqueue download\\nqueue youtube-dl\\nbrowser\\nwatch thread\\nrip media\\ncast" | dmenu -i -p "Open link with what program?")"
else
choice="$(printf "mpv\\ncopy url\\nqueue download\\nqueue youtube-dl\\nbrowser\\nrip media\\ncast" | dmenu -i -p "Open link with what program?")"
fi
case "$choice" in
"copy url") echo "$1" | xclip -selection clipboard ;;
mpv) setsid mpv --quiet --ytdl-format='bestvideo[height<=1080]+bestaudio/best[height<=1080]' "$1" & ;;
"mpv (loop)") setsid mpv --input-ipc-server=/tmp/mpvsoc$(date +%s) -quiet --loop "$1" >/dev/null 2>&1 & ;;
@ -16,6 +21,9 @@ case "$(printf "mpv\\ncopy url\\nqueue download\\nqueue youtube-dl\\nbrowser\\nr
#feh) setsid feh "$1" >/dev/null 2>&1 & ;;
#w3m) w3m "$1" >/dev/null 2>&1 ;;
#"mpv (float)") setsid mpv --input-ipc-server=/tmp/mpvsoc$(date +%s) --geometry=+0-0 --autofit=30% --title="mpvfloat" "$1" >/dev/null 2>&1 & ;;
"watch thread") dest="$(dmenuinput 'name of folder:')"
threadwatcher add "$1" "$dest"
notify-send "dmenuhandler" "Thread watcher started. Download initialized.";;
"rip media") dest="$(dmenuinput 'name of folder:')"
lynx --dump --nonumbers --listonly "$1" | grep -E "\.(webm|mp4|gif|jpg|jpeg|png|pdf|epub|mobi|djvu)" | sort -u | xargs wget -P "$HOME/$dest"
notify-send "dmenuhandler" "all media ripped into $dest";;