changeset 159:75fe19903b74

Major cleanup
author edhoprima@gmail.com <edhoprima@gmail.com>
date Fri, 05 Jun 2009 15:20:36 +0000
parents cba73f6a96bb
children 68227a30d0b3
files moefetch.sh
diffstat 1 files changed, 199 insertions(+), 165 deletions(-) [+]
line wrap: on
line diff
--- a/moefetch.sh	Tue May 05 17:56:54 2009 +0000
+++ b/moefetch.sh	Fri Jun 05 15:20:36 2009 +0000
@@ -13,39 +13,64 @@
 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-# Version 0.1-alpha1. Lots of bugs here. Use with care
+#
+# Lots of bugs here. Use with care
 # USE WITH CARE
-
-# what it does: fetch every picture that has the specified tags.
-
+#
+# what it does: fetch every picture that has the specified TAGS.
 # requirement: wget, libxslt, md5sum (or md5)
 
-# configs
-# program additional paths for: cut, sed, wc, md5(sum), wget, xsltproc, grep
-extrapath=
+# program additional paths for: cut, sed, wc, MD5(sum), wget, xsltproc, grep
+ADDITIONAL_PATH=
 
-# md5 calculation, expected output: <32digit md5><space(s)><filename>
-# gnu: "md5sum", bsd: "md5 -r"
-md5="md5 -r"
+# custom md5 path with arguments, expected output: <32digit md5><space(s)><filename>
+# Leave empty for "md5sum" (Linux, Solaris), "md5 -r" (*BSD)
+MD5=
 
-# server address. Danbooru only! I do not take responsibility of stupidity.
-site="moe.imouto.org"
+# default server address. Danbooru only! I do not take responsibility of stupidity.
+DEFAULT_SITE="moe.imouto.org"
 
 # base directory. make sure it's writeable. I do not take responsibility if you don't own the folder and files as no check is done for this one.
-# Structure is $basedir/<tags>
-basedir="/home/ifail/test"
+# Structure is ${BASE_DIR}/<TAGS>
+# Absolute path only.
+# Leave empty to use whatever folder you're running this at
+BASE_DIR=""
 
 # not user modifiable from here
 
-GENERATE()
-{
+# useless welcome message. Also version
+Msg_Welcome() {
+	MOEFETCHVERSION="0.1-beta"
+	cat <<EOF
+moefetch ${MOEFETCHVERSION}
+Copyright (c) 2009 edogawaconan <me@myconan.net>
+
+EOF
+}
+
+# fatal error handler
+Err_Fatal() {
+	echo "Fatal error: ${1}"
+	exit 1
+}
+
+# help message
+Err_Help() {
+	cat <<EOF
+Usage: moefetch (quick)fetch|status <TAGS>
+EOF
+	exit 0
+}
+
+# generate link by transforming xml
+Generate_Link() {
+	cd "${BASE_DIR}/temp"
 	echo
 	echo "Fetching xml file"
-	wget "http://$site/post/index.xml?tags=$tags&offset=0&limit=100000" -O "$outdir-xml" -e continue=off
+	wget "http://${SITE}/post/index.xml?tags=${TAGS}&offset=0&limit=100000" -O "${SITE_DIR}-${TARGET_DIR}-xml" -e continue=off
 	echo "Processing XML file..."
 	# xslt evilry
-	xsltproc - "$outdir-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([jp][pn]g\)/\1\2.\3/g' | grep ^http > "$outdir-list"
+	xsltproc - "${TARGET_DIR}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([jp][pn]g\)/\1\2.\3/g' | grep ^http > "${TARGET_DIR}-list"
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="xml" indent="yes"/>
 <xsl:template match="post">
@@ -53,192 +78,201 @@
 </xsl:template>
 </xsl:stylesheet>
 EOF
-	echo "`echo \`wc -l < \"$outdir-list\" \`` file(s) available on server"
-	#output file: $outdir-list
+	echo "`echo \`wc -l < \"${SITE_DIR}-${TARGET_DIR}-list\" \`` file(s) available on server"
+	#output file: ${TARGET_DIR}-list
 }
 
-CHECK()
-{
-	if [ "$ISNEW" -ne 1 ];then
+# check tools availability
+Check_Tools() {
+	# verify all programs required do indeed exist
+	#MD5
+	if [ ! "${MD5}" ]; then
+		case `uname` in
+			*BSD) MD5="md5 -r";;
+			Linux|SunOS) MD5="md5sum";;
+			*) Fatal_Err "No known md5 tool for this platform. Please specify manually"
+		esac
+	fi
+	MD5_COMMAND=`echo ${MD5} | cut -d' ' -f1`
+	# basic tools
+	COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep ${MD5_COMMAND}"
+	for COMMAND in ${COMMANDS}
+	do
+		COMMAND_CHECK=`command -v "${COMMAND}"`
+		 [ "${COMMAND_CHECK}" ] || Err_Fatal "${COMMAND} doesn't exist in ${PATH}"
+	done
+
+	# grep checking
+	# originally created for workaround on solaris
+	#if [ `uname` = "SunOS" ]; then
+	FAIL=""
+	echo "blah" > superrandomtestfile
+	echo "blah" > superrandomtestfile.2
+	grep -f superrandomtestfile.2 superrandomtestfile > /dev/null 2>&1 || FAIL=1
+	rm -f superrandomtestfile superrandomtestfile.2
+	[ "${FAIL}" ] && Err_Fatal "Your grep is not compatible. Please install or set path of correct grep"
+}
+
+# verify required folders exist and writeable
+Check_Folders(){
+	[ -O "${BASE_DIR}" ] || Err_Fatal "You don't own ${BASE_DIR}. Please fix ${BASE_DIR}."
+	for FOLDER in temp trash deleted ${TARGET_DIR}
+	do
+		if [ ! -d "${BASE_DIR}/${FOLDER}" ]; then
+			mkdir "${BASE_DIR}/${FOLDER}" || Err_Fatal "${FOLDER} folder creation failed"
+		fi
+		if [ ! -O "${BASE_DIR}/${FOLDER}" ]; then
+			echo "You don't own the ${BASE_DIR}/{$FOLDER}, applying globally writeable permission on it"
+			chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${FOLDER}" || Err_Fatal "Error changing ownership. This shouldn't happen"
+		fi
+	done
+	[ `echo \`ls "${BASE_DIR}/${TARGET_DIR}" | wc -l\`` -eq 0 ] && ISNEW=1
+	# let's move to workdir
+	cd "${BASE_DIR}/temp"
+	for i in error ok list newlist; do
+		touch "${TARGET_DIR}-${i}" || Fatal_Err "Error creating ${TARGET_DIR}-${i}. This shouldn't happen"
+	done
+	#
+}
+
+# check files correctness
+Check_Files() {
+	if [ "$ISNEW" -ne 1 ]; then
 		echo "Checking for errors..."
 		# THE FILES
-		printf "" > "$outdir-error"
-		cd "../$outdir"
-		for file in `ls`
+		
+		# current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}
+		cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}"
+		for TRASH in `ls * | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$`
 		do
-			if [ `$md5 "$file" | cut -d ' ' -f1 -` != `echo "$file" | cut -d '.' -f1` ]
+			mv -f "${TRASH}" "${BASE_DIR}/trash"
+			echo "Moved ${TRASH} to ${BASE_DIR}/trash"
+		done
+		printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
+		for FILE in *
+		do
+			if [ `${MD5} "${FILE}" | cut -d ' ' -f1 -` != `echo "${FILE}" | cut -d '.' -f1` ]
 			then
-				echo "$file" >> "../temp/$outdir-error"
-				echo "Error: $file"
+				echo
+				echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error"
+				echo "Error: ${FILE}"
 			fi
 			printf "."
 		done
 		echo
-		cd ../temp
-		totalerr=`echo \`wc -l < "$outdir-error"\``
-		echo "$totalerr file(s) error"
+		
+		# current dir: ${BASE_DIR}/temp
+		cd ${BASE_DIR}/temp
+		TOTAL_ERROR=`echo \`wc -l < "${SITE_DIR}-${TARGET_DIR}-error"\``
+		echo "${TOTAL_ERROR} file(s) error"
 
 		echo "Generating list of new files..."
 		# THE FILES
-		#ls "../$outdir" | grep -vf "$outdir-error" > "$outdir-ok"
+		#ls "../${TARGET_DIR}" | grep -vf "${TARGET_DIR}-error" > "${TARGET_DIR}-ok"
 		#
-		ls "../$outdir" | comm -1 -3 "$outdir-error" - > "$outdir-ok"
-		cat "$outdir-list" | grep -vf "$outdir-ok" > "$outdir-newlist"
-		echo "`echo \`wc -l < \"$outdir-newlist\"\`` file(s) to be downloaded"
-		cd "../$outdir"
+		ls "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | comm -1 -3 "${SITE_DIR}-${TARGET_DIR}-error" - > "${SITE_DIR}-${TARGET_DIR}-ok"
+		cat "${SITE_DIR}-${TARGET_DIR}-list" | grep -vf "${SITE_DIR}-${TARGET_DIR}-ok" > "${SITE_DIR}-${TARGET_DIR}-newlist"
+		echo "`echo \`wc -l < \"${SITE_DIR}-${TARGET_DIR}-newlist\"\`` file(s) to be downloaded"
+		
+		# back to target dir
+		cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}"
 
 		echo "Removing error files"
-		if [ $totalerr -eq 0 ]; then
+		if [ "${TOTAL_ERROR}" -eq 0 ]; then
 			echo "No error file. 0 file removed"
 		else
-			cat "../temp/$outdir-error" | xargs rm
-			echo "$totalerr file(s) removed"
+			cat "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | xargs rm
+			echo "${TOTAL_ERROR} file(s) removed"
 		fi
 		echo "`echo \`ls | wc -l\`` file(s) available locally"
 		cd ../temp
 	else
-		if [ $ISQUICK -eq 1 ]; then
+		if [ "${ISQUICK}" ]; then
 			echo "quick mode selected. Skipping check"
 		else
 			echo "Empty local repository"
 		fi
-		cat "$outdir-list" > "$outdir-newlist"
+		cd "${BASE_DIR}/temp"
+		cat "${SITE_DIR}-${TARGET_DIR}-list" > "${TARGET_DIR}-newlist"
 	fi
 }
 
-FETCH()
-{
-	if [ `echo \`wc -l < "$outdir-newlist"\`` -eq 0 ]; then
+# start downloading the images
+Fetch_Images() {
+	if [ `echo \`wc -l < "${TARGET_DIR}-newlist"\`` -eq 0 ]; then
 		echo "No new file"
 	else
 		echo "Starting wget"
-		cd "../$outdir"
-		wget -e continue=on -bi "../temp/$outdir-newlist" -o "../temp/$outdir.log"
+		cd "../${TARGET_DIR}"
+		wget -e continue=on -bi "../temp/${TARGET_DIR}-newlist" -o "../temp/${TARGET_DIR}.log"
 	fi
 }
 
-
-# path initialization
-export PATH=$extrapath:${PATH}
-
-# verify all programs required do indeed exist
+# initialize base variables and initial command check
+Init(){
+	# Get base folder - current folder or fallback to ${HOME}
+	[ "${BASE_DIR}"	] || BASE_DIR="${PWD}"
+	[ "${BASE_DIR}" ] || BASE_DIR="{$HOME}"
+	[ "`echo ${BASE_DIR} | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}"	
+	# path initialization
+	[ "${ADDITIONAL_PATH}" ] && PATH=${ADDITIONAL_PATH}:${PATH}
+	export PATH
 	
+	# misc variables
+	ISQUICK=""
+	ISNEW=""
 
-# basic tools
-commands="cut sed wc wget xsltproc xargs rm mkdir chown comm grep"
-cmderr=" "
-for cmd in $commands
-do
-	command -v "$cmd" >/dev/null || cmderr="$cmderr $cmd"
-done
-if [ x"$cmderr" != x" " ]; then
-	echo "$cmderr doesn't exist in $PATH"
-	exit 1
-fi
-#md5
-md5base=`echo $md5 | cut -d ' ' -f 1 -`
-if [ x`command -v "$md5base" >/dev/null || echo x` != "x" ]; then
-	echo "$md5base doesn't exist in $PATH"
-	exit 1
-fi
-mdtest=
-if [ `echo test | $md5 | cut -d ' ' -f 1 -` != "d8e8fca2dc0f896fd7cb4cb0031ba249" ]; then
-	echo "$md5 doesn't produce wanted output"
-	exit 1
-fi
+	[ $# -lt 2 ] && Err_Help
+	case "$1" in
+		status|fetch|quickfetch)
+			echo "Starting..."
+			JOB="$1"
+		;;
+		*)
+			Err_Help
+		;;
+	esac
+	shift
+	SITE=
+	case "$1" in
+		-s|--site)
+			shift
+			SITE="$1"
+		;;
+		*)
+			SITE=DEFAULT_SITE
+		;;
+	esac
+	shift
+	TAGS="$@"
+	echo "Tags: ${TAGS}"
+	# slash is not wanted for folder name
+	TARGET_DIR=`echo "${TAGS}" | sed -e 's/\//_/g'`
+	SITE_DIR=`echo "${SITE}" | sed -e 's/\//_/g'`
+}
 
-# grep checking
-# originally created for workaround on solaris
-#if [ `uname` = "SunOS" ]; then
-	FAIL=0
-	echo "blah" > superrandomtestfile
-	echo "blah" > superrandomtestfile.2
-	grep -f superrandomtestfile.2 superrandomtestfile > /dev/null 2>&1 || FAIL=1
-	rm superrandomtestfile superrandomtestfile.2 superrandomtestfile.3 > /dev/null 2>&1
-	if [ $FAIL = 1 ]; then
-		echo "Your grep is not compatible. Please install or set path of correct grep"
-		exit 1
-	fi
-#fi
+Msg_Welcome
+Init "$@"
+Check_Tools
+Check_Folders
 
 
-# all green (part 1)! let's go (until we check the tag)
-
-# initialization
-# are we really doing it?
-HELP="Usage: moefetch (quick)fetch|status <tags>"
-
-if [ $# -lt 2 ]; then
-    echo "$HELP"
-    exit 1
-fi
-
-case "$1" in
-	status|fetch|quickfetch)
-		echo "Starting..."
+# let's do the job!
+case "${JOB}" in
+	status)
+		Generate_Link
+		Check_Files
 	;;
-	*)
-		echo "$HELP"
-		exit 1
+	fetch)
+		Generate_Link
+		Check_Files
+		Fetch_Images
+	;;
+	quickfetch)
+		ISNEW=1
+		ISQUICK=1
+		Generate_Link
+		Check_Files
+		Fetch_Images
 	;;
 esac
-
-# we did it indeed
-# get started
-
-# do we own the files
-tags=`echo "$@" | cut -d ' ' -f 2- -`
-echo "Tags: $tags"
-# slash do not want
-outdir=`echo "$tags" | sed -e 's/\//_/g'`
-ISNEW=0
-if [ -O "$basedir" ]; then
-	if [ ! -d "$basedir/$outdir" ]; then
-		ISNEW=1
-		mkdir "$basedir/$outdir"
-	fi
-	if [ ! -O "$basedir/$outdir" ]; then
-		echo "You don't own the $basedir/$outdir, applying globally writeable permission on it"
-		chmod -R u=rwX,g=rwX,o=rwX "$basedir/$outdir"
-	fi
-	if [ `echo \`ls "$basedir/$outdir" | wc -l\`` -eq 0 ]; then
-		ISNEW=1
-	fi
-	if [ ! -d "$basedir/temp" ]; then
-		mkdir "$basedir/temp"
-	fi
-	if [ ! -O "$basedir/temp" ]; then
-		echo "You don't own the $basedir/temp, applying globally writeable permission on it"
-		chmod -R u=rwX,g=rwX,o=rwX "$basedir/temp"
-	fi
-else
-	echo "Fatal error: you don't own ${basedir}. Please fix ${basedir}. Stopping"
-	exit 1
-fi
-# let's move to workdir
-cd "$basedir/temp"
-touch "$outdir-error"
-touch "$outdir-ok"
-touch "$outdir-list"
-touch "$outdir-newlist"
-# 
-
-# let's do the job!
-ISQUICK=0
-case "$1" in
-	status)
-		GENERATE
-		CHECK
-	;;
-	fetch)
-		GENERATE
-		CHECK
-		FETCH
-	;;
-	quickfetch)
-		GENERATE
-		ISNEW=1
-		ISQUICK=1
-		CHECK
-		FETCH
-esac