Skip to content

Instantly share code, notes, and snippets.

@pituz
Created February 7, 2014 15:43
Show Gist options
  • Save pituz/8865136 to your computer and use it in GitHub Desktop.
Save pituz/8865136 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
############################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
############################################################################
help() {
cat <<EOF
Usage: $0 [options] thread_url [destination_directory]
Options:
-i download images only
-c re-check page and get files until error
-s interval sleep interval between page checks (default 20s; see man sleep)
-n don't notify on page changes
-N don't download anything; implies -c
-p save additional page files (thumbs, css, etc.)
default destination directory - thread number
EOF
}
# [r]ecursive, [c]ontinue, recursion [l]evel, N - check by timestamp only,
# nd - no directories
wgetopts="-e robots=off -r -l1 -N -nd -H --no-check-certificate --convert-links --no-verbose"
s=20s; c=""; dl=1; page=""
[ "$DISPLAY" ] && [ "$(which notify-send)" ] && notify=1
while getopts "ics:nNp" o; do
case "$o" in
i) wgetopts+=" -A png,gif,jpg" ;;
c) c=1 ;;
s) s="$OPTARG" ;;
n) notify="" ;;
N) dl=""; c=1 ;;
p) page=1 ;;
*) help
exit 1 ;;
esac
done
shift $((OPTIND - 1))
[ -z "${dl}${notify}" ] && echo "$0: nothing to do">&2 && exit 1
[ -z "$1" ] && help && exit 1
[ "$3" ] && help && exit 1
domain=${1#*://}; domain=${domain#www.}; domain=${domain%%/*}
b="$(expr "$1" : ".*/\([^/]\+\)/res")"
t="$(expr "$1" : ".*/res/\([0-9]\+\).html")"
if [ "$dl" ]; then
[ "$2" ] && destdir="$2" || destdir="$t"
[ -d "$destdir" ] || mkdir -p "$destdir"
cd "$destdir" || exit 1
fi
case "$domain" in
2-ch.ru) [ "$page" ] && idirs="/$b/thumb,/$b/res,/$b/src,/css,/js,/math,/icons,/plugins" \
|| idirs="/$b/src" ;;
*) [ "$page" ] && idirs="/$b/thumb,/$b/res,/$b/src,/css,/js" \
|| idirs="/$b/src" ;;
esac
getFiles() {
lm_=$(wget --no-check-certificate --spider "$1" -S 2>&1 | grep Last-Modified)
[ "$lm_" ] || exit 1
[ "$lm" == "$lm_" ] && return
[ "$notify" ] && [ "$lm" ] && notify-send "$1: new posts"
lm="$lm_"
[ -z "$dl" ] && return
wget "$1" --include-directories="$idirs" $wgetopts
}
while getFiles "$1" && [ "$c" ]; do
sleep "$s"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment