Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,53 @@ | ||
#!/bin/sh | ||
# clone/inspo: https://github.com/enjuus/chandler | ||
# I have stepbrother named chandler, who goes by reese. | ||
# depends on: xclip, awk, https://github.com/ericchiang/pup | ||
# depends on: curl, sed, awk, https://github.com/ericchiang/pup, xclip(optional) | ||
|
||
url="" | ||
target="$HOME/4ch/{BOARD}/{THREAD}" | ||
# valid templates: BOARD, THREADID, THREAD (which falls back to THREADID) | ||
target="$HOME/4ch/{BOARD}/{THREADID}" | ||
watch_mode=false | ||
|
||
while getopts "s:d:h" opt; do | ||
while getopts "s:d:wh" opt; do | ||
case ${opt} in | ||
d) target="$OPTARG" ;; | ||
s) url="$OPTARG" ;; | ||
w) watch_mode=true ;; | ||
h) echo "read it"; exit ;; | ||
esac | ||
done | ||
|
||
# fallback to clipboard if -s not specified | ||
[ -z "$url" ] && url="$(xclip -o -selection clipboard)" | ||
echo "url: $url" | ||
page="$(curl -s $url)" | ||
[ $? -ne 0 ] && echo "invalid url" && exit 1 | ||
|
||
# eg http://boards.4chan.org/wg/thread/7354127 | ||
board=$(echo "$url" | awk '{split($0,a,"/"); print a[4]}') | ||
thread_id=$(echo "$url" | awk '{split($0,a,"/"); print a[6]}' | sed 's/#.*//') | ||
page="$(curl -s $url)" | ||
[ $? -ne 0 ] && echo "invalid url" && exit | ||
|
||
thread="$(echo "$page" | pup '.board .subject:first-of-type text{}')" | ||
[ -z "$thread" ] && thread="$thread_id" | ||
|
||
target="$(echo "$target" | sed -e "s/{THREAD}/${thread}/" -e "s/{THREADID}/${thread_id}/" -e "s/{BOARD}/${board}/")" | ||
target="$(echo "$target" | sed -e "s/{THREAD}/${thread}/;" -e "s/{THREADID}/${thread_id}/" -e "s/{BOARD}/${board}/")" | ||
mkdir -p "$target" | ||
|
||
echo "$page" | pup '.board a.fileThumb attr{href}' | \ | ||
while read file; do | ||
filename="$(basename $file)" | ||
# don't duplicate downloads, but fork off every call. | ||
[ -f "$target/$filename" ] || echo getting "https:$file to $target/$filename" | ||
[ -f "$target/$filename" ] || curl -s "https:$file" > "$target/$filename" & | ||
done | ||
|
||
wait | ||
get_images() { | ||
echo "$page" | pup '.board a.fileThumb attr{href}' | \ | ||
while read file; do | ||
filename="$(basename $file)" | ||
# don't duplicate downloads, but fork off every call. | ||
[ -f "$target/$filename" ] || echo getting "https:$file to $target/$filename" | ||
[ -f "$target/$filename" ] || curl -s "https:$file" > "$target/$filename" & | ||
done | ||
|
||
wait | ||
} | ||
get_images | ||
|
||
while $watch_mode; do | ||
sleep 60 | ||
page="$(curl -s $url)" | ||
page_title="$(echo "$page" | pup 'head title text{}')" | ||
[ "$page_title" = "302 Found" ] && echo "Thread died." && exit 1 | ||
get_images | ||
done |