Mercurial > ec-dotfiles
annotate moefetch.sh @ 173:2b7c8c1ecdfe
better trash handler
author | edhoprima@gmail.com <edhoprima@gmail.com> |
---|---|
date | Fri, 05 Jun 2009 20:12:15 +0000 |
parents | 30de37b6fe47 |
children | 0948e76a57a1 |
rev | line source |
---|---|
148 | 1 #!/bin/sh |
2 | |
3 # Copyright (c) 2009, edogawaconan <me@myconan.net> | |
4 # | |
5 # Permission to use, copy, modify, and/or distribute this software for any | |
6 # purpose with or without fee is hereby granted, provided that the above | |
7 # copyright notice and this permission notice appear in all copies. | |
8 # | |
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
159 | 16 # |
17 # Lots of bugs here. Use with care | |
148 | 18 # USE WITH CARE |
159 | 19 # |
20 # what it does: fetch every picture that has the specified TAGS. | |
148 | 21 # requirement: wget, libxslt, md5sum (or md5) |
22 | |
159 | 23 # program additional paths for: cut, sed, wc, MD5(sum), wget, xsltproc, grep |
24 ADDITIONAL_PATH= | |
148 | 25 |
159 | 26 # custom md5 path with arguments, expected output: <32digit md5><space(s)><filename> |
27 # Leave empty for "md5sum" (Linux, Solaris), "md5 -r" (*BSD) | |
28 MD5= | |
148 | 29 |
159 | 30 # default server address. Danbooru only! I do not take responsibility of stupidity. |
31 DEFAULT_SITE="moe.imouto.org" | |
148 | 32 |
33 # 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. | |
159 | 34 # Structure is ${BASE_DIR}/<TAGS> |
35 # Absolute path only. | |
36 # Leave empty to use whatever folder you're running this at | |
37 BASE_DIR="" | |
148 | 38 |
39 # not user modifiable from here | |
40 | |
159 | 41 # useless welcome message. Also version |
42 Msg_Welcome() { | |
43 MOEFETCHVERSION="0.1-beta" | |
44 cat <<EOF | |
45 moefetch ${MOEFETCHVERSION} | |
46 Copyright (c) 2009 edogawaconan <me@myconan.net> | |
47 | |
48 EOF | |
49 } | |
50 | |
51 # fatal error handler | |
52 Err_Fatal() { | |
53 echo "Fatal error: ${1}" | |
54 exit 1 | |
55 } | |
56 | |
57 # help message | |
58 Err_Help() { | |
59 cat <<EOF | |
60 Usage: moefetch (quick)fetch|status <TAGS> | |
61 EOF | |
62 exit 0 | |
63 } | |
64 | |
65 # generate link by transforming xml | |
66 Generate_Link() { | |
67 cd "${BASE_DIR}/temp" | |
148 | 68 echo |
69 echo "Fetching xml file" | |
159 | 70 wget "http://${SITE}/post/index.xml?tags=${TAGS}&offset=0&limit=100000" -O "${SITE_DIR}-${TARGET_DIR}-xml" -e continue=off |
148 | 71 echo "Processing XML file..." |
72 # xslt evilry | |
165 | 73 xsltproc - "${SITE_DIR}-${TARGET_DIR}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([jp][pn]g\)/\1\2.\3/g' | grep ^http > "${SITE_DIR}-${TARGET_DIR}-list" |
148 | 74 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> |
75 <xsl:output method="xml" indent="yes"/> | |
76 <xsl:template match="post"> | |
77 <xsl:value-of select="@file_url" /> | |
78 </xsl:template> | |
79 </xsl:stylesheet> | |
80 EOF | |
159 | 81 echo "`echo \`wc -l < \"${SITE_DIR}-${TARGET_DIR}-list\" \`` file(s) available on server" |
82 #output file: ${TARGET_DIR}-list | |
148 | 83 } |
84 | |
159 | 85 # check tools availability |
86 Check_Tools() { | |
87 # verify all programs required do indeed exist | |
88 #MD5 | |
89 if [ ! "${MD5}" ]; then | |
90 case `uname` in | |
91 *BSD) MD5="md5 -r";; | |
92 Linux|SunOS) MD5="md5sum";; | |
93 *) Fatal_Err "No known md5 tool for this platform. Please specify manually" | |
94 esac | |
95 fi | |
96 MD5_COMMAND=`echo ${MD5} | cut -d' ' -f1` | |
97 # basic tools | |
98 COMMANDS="cut sed wc wget xsltproc xargs rm mkdir chown comm grep ${MD5_COMMAND}" | |
99 for COMMAND in ${COMMANDS} | |
100 do | |
101 COMMAND_CHECK=`command -v "${COMMAND}"` | |
102 [ "${COMMAND_CHECK}" ] || Err_Fatal "${COMMAND} doesn't exist in ${PATH}" | |
103 done | |
104 | |
105 # grep checking | |
106 # originally created for workaround on solaris | |
107 #if [ `uname` = "SunOS" ]; then | |
108 FAIL="" | |
109 echo "blah" > superrandomtestfile | |
110 echo "blah" > superrandomtestfile.2 | |
111 grep -f superrandomtestfile.2 superrandomtestfile > /dev/null 2>&1 || FAIL=1 | |
112 rm -f superrandomtestfile superrandomtestfile.2 | |
113 [ "${FAIL}" ] && Err_Fatal "Your grep is not compatible. Please install or set path of correct grep" | |
114 } | |
115 | |
116 # verify required folders exist and writeable | |
117 Check_Folders(){ | |
118 [ -O "${BASE_DIR}" ] || Err_Fatal "You don't own ${BASE_DIR}. Please fix ${BASE_DIR}." | |
167 | 119 for FOLDER in temp trash deleted ${SITE_DIR}/${TARGET_DIR} |
159 | 120 do |
121 if [ ! -d "${BASE_DIR}/${FOLDER}" ]; then | |
122 mkdir "${BASE_DIR}/${FOLDER}" || Err_Fatal "${FOLDER} folder creation failed" | |
123 fi | |
124 if [ ! -O "${BASE_DIR}/${FOLDER}" ]; then | |
167 | 125 echo "You don't own the ${BASE_DIR}/${FOLDER}, applying globally writeable permission on it" |
159 | 126 chmod -R u=rwX,g=rwX,o=rwX "${BASE_DIR}/${FOLDER}" || Err_Fatal "Error changing ownership. This shouldn't happen" |
127 fi | |
128 done | |
167 | 129 [ `echo \`ls "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | wc -l\`` -eq 0 ] && ISNEW=1 |
159 | 130 # let's move to workdir |
131 cd "${BASE_DIR}/temp" | |
132 for i in error ok list newlist; do | |
161
52877e2849bb
misc fix. These past commits wasn't actually tested
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
160
diff
changeset
|
133 touch "${SITE_DIR}-${TARGET_DIR}-${i}" || Fatal_Err "Error creating ${TARGET_DIR}-${i}. This shouldn't happen" |
159 | 134 done |
135 # | |
136 } | |
137 | |
138 # check files correctness | |
139 Check_Files() { | |
166 | 140 if [ ! "${ISNEW}" ]; then |
148 | 141 echo "Checking for errors..." |
142 # THE FILES | |
159 | 143 |
144 # current dir: ${BASE_DIR}/${SITE_DIR}/${TARGET_DIR} | |
145 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | |
173
2b7c8c1ecdfe
better trash handler
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
172
diff
changeset
|
146 TRASH_DIR="${SITE_DIR}-${TARGET_DIR}-`date -u +%Y%m%d-%H.%M`" |
2b7c8c1ecdfe
better trash handler
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
172
diff
changeset
|
147 mkdir -p "${BASE_DIR}/trash/${TRASH_DIR}" |
172 | 148 for TRASH in `ls | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$` |
148 | 149 do |
173
2b7c8c1ecdfe
better trash handler
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
172
diff
changeset
|
150 mv -f "${TRASH}" "${BASE_DIR}/trash/${TRASH_DIR}" || Err_Fatal "Error deleting files" |
2b7c8c1ecdfe
better trash handler
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
172
diff
changeset
|
151 echo "Moved ${TRASH} to ${BASE_DIR}/trash/${TRASH_DIR}" |
159 | 152 done |
173
2b7c8c1ecdfe
better trash handler
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
172
diff
changeset
|
153 [ "`ls "${BASE_DIR}/trash/${TRASH_DIR}"`" ] || rmdir "${BASE_DIR}/trash/${TRASH_DIR}" |
159 | 154 printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" |
155 for FILE in * | |
156 do | |
170 | 157 if [ "`${MD5} "${FILE}" | cut -d ' ' -f1 -`" != "`echo "${FILE}" | cut -d '.' -f1`" ] |
148 | 158 then |
159 | 159 echo |
160 echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | |
161 echo "Error: ${FILE}" | |
148 | 162 fi |
163 printf "." | |
164 done | |
165 echo | |
159 | 166 |
170 | 167 # back to target dir |
168 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | |
169 | |
170 echo "Removing error files" | |
171 if [ "${TOTAL_ERROR}" -eq 0 ]; then | |
172 echo "No error file. 0 file removed" | |
173 else | |
174 cat "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | xargs rm | |
175 echo "${TOTAL_ERROR} file(s) removed" | |
176 fi | |
177 echo "`echo \`ls | wc -l\`` file(s) available locally" | |
178 | |
159 | 179 # current dir: ${BASE_DIR}/temp |
180 cd ${BASE_DIR}/temp | |
181 TOTAL_ERROR=`echo \`wc -l < "${SITE_DIR}-${TARGET_DIR}-error"\`` | |
182 echo "${TOTAL_ERROR} file(s) error" | |
148 | 183 |
184 echo "Generating list of new files..." | |
185 # THE FILES | |
159 | 186 #ls "../${TARGET_DIR}" | grep -vf "${TARGET_DIR}-error" > "${TARGET_DIR}-ok" |
156
d3b002fd944e
fix: my attempt at speeding up things failed. reverting back to trusty grep -vf
edhoprima
parents:
155
diff
changeset
|
187 # |
159 | 188 ls "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | comm -1 -3 "${SITE_DIR}-${TARGET_DIR}-error" - > "${SITE_DIR}-${TARGET_DIR}-ok" |
189 cat "${SITE_DIR}-${TARGET_DIR}-list" | grep -vf "${SITE_DIR}-${TARGET_DIR}-ok" > "${SITE_DIR}-${TARGET_DIR}-newlist" | |
190 echo "`echo \`wc -l < \"${SITE_DIR}-${TARGET_DIR}-newlist\"\`` file(s) to be downloaded" | |
191 | |
148 | 192 else |
159 | 193 if [ "${ISQUICK}" ]; then |
152 | 194 echo "quick mode selected. Skipping check" |
195 else | |
196 echo "Empty local repository" | |
197 fi | |
159 | 198 cd "${BASE_DIR}/temp" |
166 | 199 cat "${SITE_DIR}-${TARGET_DIR}-list" > "${SITE_DIR}-${TARGET_DIR}-newlist" |
148 | 200 fi |
201 } | |
202 | |
159 | 203 # start downloading the images |
204 Fetch_Images() { | |
160
68227a30d0b3
forgot to fix Fetch_Images to reflect new folder naming scheme
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
159
diff
changeset
|
205 cd "${BASE_DIR}/temp" |
68227a30d0b3
forgot to fix Fetch_Images to reflect new folder naming scheme
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
159
diff
changeset
|
206 if [ `echo \`wc -l < "${SITE_DIR}-${TARGET_DIR}-newlist"\`` -eq 0 ]; then |
148 | 207 echo "No new file" |
208 else | |
209 echo "Starting wget" | |
160
68227a30d0b3
forgot to fix Fetch_Images to reflect new folder naming scheme
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
159
diff
changeset
|
210 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" |
68227a30d0b3
forgot to fix Fetch_Images to reflect new folder naming scheme
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
159
diff
changeset
|
211 wget -e continue=on -bi "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-newlist" -o "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}.log" |
148 | 212 fi |
213 } | |
214 | |
159 | 215 # initialize base variables and initial command check |
216 Init(){ | |
217 # Get base folder - current folder or fallback to ${HOME} | |
218 [ "${BASE_DIR}" ] || BASE_DIR="${PWD}" | |
219 [ "${BASE_DIR}" ] || BASE_DIR="{$HOME}" | |
220 [ "`echo ${BASE_DIR} | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}" | |
221 # path initialization | |
222 [ "${ADDITIONAL_PATH}" ] && PATH=${ADDITIONAL_PATH}:${PATH} | |
223 export PATH | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
224 |
159 | 225 # misc variables |
166 | 226 ISQUICK= |
227 ISNEW= | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
228 |
159 | 229 [ $# -lt 2 ] && Err_Help |
230 case "$1" in | |
231 status|fetch|quickfetch) | |
232 echo "Starting..." | |
233 JOB="$1" | |
234 ;; | |
235 *) | |
236 Err_Help | |
237 ;; | |
238 esac | |
239 shift | |
240 SITE= | |
241 case "$1" in | |
242 -s|--site) | |
243 shift | |
244 SITE="$1" | |
163
e2149ba6ab9c
shift placed at wrong place.
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
162
diff
changeset
|
245 shift |
159 | 246 ;; |
247 *) | |
161
52877e2849bb
misc fix. These past commits wasn't actually tested
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
160
diff
changeset
|
248 SITE="${DEFAULT_SITE}" |
159 | 249 ;; |
250 esac | |
166 | 251 TAGS="$@" |
159 | 252 echo "Tags: ${TAGS}" |
253 # slash is not wanted for folder name | |
162
1f937c2e8b3f
tags doesn't get parsed :(
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
161
diff
changeset
|
254 TARGET_DIR="`echo "${TAGS}" | sed -e 's/\//_/g'`" |
1f937c2e8b3f
tags doesn't get parsed :(
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
161
diff
changeset
|
255 SITE_DIR="`echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g'`" |
159 | 256 } |
148 | 257 |
159 | 258 Msg_Welcome |
259 Init "$@" | |
260 Check_Tools | |
261 Check_Folders | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
262 |
148 | 263 |
159 | 264 # let's do the job! |
265 case "${JOB}" in | |
266 status) | |
267 Generate_Link | |
268 Check_Files | |
148 | 269 ;; |
159 | 270 fetch) |
271 Generate_Link | |
272 Check_Files | |
273 Fetch_Images | |
274 ;; | |
275 quickfetch) | |
276 ISNEW=1 | |
277 ISQUICK=1 | |
278 Generate_Link | |
279 Check_Files | |
280 Fetch_Images | |
148 | 281 ;; |
282 esac |