comparison moefetch.sh @ 227:8b1f6f6b6a3b

Bugfixes: - Login and password are not always used now. Added new global variable to check it. - 'export PATH' is now only done if additional path(s) specified. Not really enhancements: - New coding style: - Local variables now start with double underscores and global variables start with single underscore. Both lower letter. - Function name is now snake_case. Camel_And_Snake_Case doesn't actually make sense :> - More comments. Still need to add even more comments.
author edhoprima@gmail.com <edhoprima@gmail.com>
date Mon, 14 Dec 2009 09:57:35 +0000
parents f8be4a3d3b4a
children 5d3a0645b504
comparison
equal deleted inserted replaced
226:f8be4a3d3b4a 227:8b1f6f6b6a3b
37 ### TODO: 37 ### TODO:
38 ### - sanity validator(?) 38 ### - sanity validator(?)
39 ### - unified repository to save bandwidth 39 ### - unified repository to save bandwidth
40 ### - bug stomping 40 ### - bug stomping
41 ### - sanity checking 41 ### - sanity checking
42 ### - MOAR comments
42 ### WILL BE FOR 0.3 43 ### WILL BE FOR 0.3
43 44
44 # useless welcome message. Also version 45 # useless welcome message. Also version
45 Msg_Welcome() { 46 Msg_Welcome() {
46 MOEFETCHVERSION="0.3-beta2" 47 MOEFETCHVERSION="0.3-beta2"
90 Fetching XML file" 91 Fetching XML file"
91 tempnum=1000 92 tempnum=1000
92 iternum=1 93 iternum=1
93 > "${TEMP_PREFIX}-list" 94 > "${TEMP_PREFIX}-list"
94 while [ "${tempnum}" -ge 1000 ]; do 95 while [ "${tempnum}" -ge 1000 ]; do
95 wget "http://${SITE}/post/index.xml?tags=$(get_cleantags "${TAGS}")&offset=0&limit=1000&page=${iternum}&login=${LOGIN_USER}&password_hash=${LOGIN_PASS}" -O "${TEMP_PREFIX}-xml" -e continue=off 96 __url="http://${SITE}/post/index.xml?tags=$(get_cleantags "${TAGS}")&offset=0&limit=1000&page=${iternum}"
97 [ ${_use_login} -eq 1 ] && __url="${__url}&login=${LOGIN_USER}&password_hash=${LOGIN_PASS}"
98 wget "${__url}" -O "${TEMP_PREFIX}-xml" -e continue=off || Err_Fatal "Failed download catalog file"
96 printf "Processing XML file... " 99 printf "Processing XML file... "
97 # xslt evilry 100 # xslt evilry
98 xsltproc - "${TEMP_PREFIX}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([^\.]*\)/\1\2.\3/g' | grep ^http > "${TEMP_PREFIX}-templist" 101 xsltproc - "${TEMP_PREFIX}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([^\.]*\)/\1\2.\3/g' | grep ^http > "${TEMP_PREFIX}-templist"
99 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 102 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
100 <xsl:output method="xml" indent="yes"/> 103 <xsl:output method="xml" indent="yes"/>
278 wget -e continue=on -bi "${TEMP_PREFIX}-newlist" -o "${TEMP_PREFIX}.log" 281 wget -e continue=on -bi "${TEMP_PREFIX}-newlist" -o "${TEMP_PREFIX}.log"
279 fi 282 fi
280 } 283 }
281 284
282 # initialize base variables and initial command check 285 # initialize base variables and initial command check
283 Init(){ 286 init()
287 {
284 # path initialization 288 # path initialization
285 [ -n "${ADDITIONAL_PATH}" ] && PATH="${ADDITIONAL_PATH}:${PATH}" 289 # check if additional path is specified
286 export PATH 290 if [ -n "${ADDITIONAL_PATH}" ]
291 then
292 # insert the additional path
293 PATH="${ADDITIONAL_PATH}:${PATH}"
294 export PATH
295 fi
287 296
288 # misc variables 297 # misc variables
289 ISQUICK= 298 ISQUICK=
290 ISNEW= 299 ISNEW=
300 # variable to check whether a login is used or not
301 _use_login=0
291 302
303 # minimum number of arguments: 2 (command and tag). If less than two, exit and print help message
292 [ $# -lt 2 ] && Err_Help 304 [ $# -lt 2 ] && Err_Help
293 case "$1" in 305 case "$1" in
294 check|fetch|quickfetch) 306 check|fetch|quickfetch)
295 echo "Starting..." 307 echo "Starting..."
296 JOB="$1" 308 JOB="$1"
300 ;; 312 ;;
301 esac 313 esac
302 shift 314 shift
303 SITE= 315 SITE=
304 TAGS= 316 TAGS=
305 x=1 317 __has_pass=0
318 __has_user=0
306 while getopts "s:(site)n(noclean)u:(user)p:(password)" opt 319 while getopts "s:(site)n(noclean)u:(user)p:(password)" opt
307 do 320 do
308 case "$opt" in 321 case "$opt" in
309 s) SITE="$OPTARG";; 322 s) SITE="$OPTARG";;
310 n) NOCLEAN=1;; 323 n) NOCLEAN=1;;
311 p) LOGIN_PASS=$(printf "%s" "$OPTARG" | openssl dgst -sha1);; 324 p)
312 u) LOGIN_USER="$OPTARG";; 325 LOGIN_PASS=$(printf "%s" "$OPTARG" | openssl dgst -sha1)
326 __has_pass=1
327 ;;
328 u)
329 LOGIN_USER="$OPTARG"
330 __has_user=1
331 ;;
313 esac 332 esac
314 x=${OPTIND} 333 done
315 done
316 shift $(($x-1))
317 TAGS="$@" 334 TAGS="$@"
318 [ -n "${SITE}" ] || SITE=${DEFAULT_SITE} 335 [ -n "${SITE}" ] || SITE=${DEFAULT_SITE}
319 [ -n "${TAGS}" ] || Err_Fatal "No tag specified" 336 [ -n "${TAGS}" ] || Err_Fatal "No tag specified"
320 # Get base folder - default, current folder or fallback to ${HOME} 337 # Get base folder - default, current folder or fallback to ${HOME}
321 [ -n "${BASE_DIR}" ] || BASE_DIR=${PWD} 338 [ -n "${BASE_DIR}" ] || BASE_DIR=${PWD}
322 [ -n "${BASE_DIR}" ] || BASE_DIR=${HOME} 339 [ -n "${BASE_DIR}" ] || BASE_DIR=${HOME}
323 [ -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" ] || BASE_DIR="/${BASE_DIR}" 340 [ -n "$(echo "${BASE_DIR}" | cut -c1 | grep \/)" ] || BASE_DIR="/${BASE_DIR}"
341 # see if both pass and use are set. If they're set, switch _use_login variable content to 1.
342 [ ${__has_pass} -eq 1 -a ${__has_user} -eq 1 ] && _use_login=1
324 343
325 echo "Tags: ${TAGS}" 344 echo "Tags: ${TAGS}"
326 # slash is not wanted for folder name 345 # slash is not wanted for folder name
327 TARGET_DIR=$(echo "${TAGS}" | sed -e 's/\//_/g') 346 TARGET_DIR=$(echo "${TAGS}" | sed -e 's/\//_/g')
328 SITE_DIR=$(echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g') 347 SITE_DIR=$(echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g')
329 TEMP_PREFIX="${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}" 348 TEMP_PREFIX="${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}"
330 } 349 }
331 350
332 # initialization 351 main()
333 Msg_Welcome 352 {
334 Init "$@" 353 # initialization
335 Check_Tools 354 Msg_Welcome
336 Check_Folders 355 init "$@"
337 356 Check_Tools
338 357 Check_Folders
339 # let's do the job! 358
340 case "${JOB}" in 359
341 check) 360 # let's do the job!
342 Generate_Link 361 case "${JOB}" in
343 Check_Files 362 check)
344 ;; 363 Generate_Link
345 fetch) 364 Check_Files
346 Generate_Link 365 ;;
347 Check_Files 366 fetch)
348 Fetch_Images 367 Generate_Link
349 ;; 368 Check_Files
350 quickfetch) 369 Fetch_Images
351 ISNEW=1 370 ;;
352 ISQUICK=1 371 quickfetch)
353 Generate_Link 372 ISNEW=1
354 Check_Files 373 ISQUICK=1
355 Fetch_Images 374 Generate_Link
356 ;; 375 Check_Files
357 esac 376 Fetch_Images
377 ;;
378 esac
379 }
380
381 # call the main routine!
382 main "$@"
383