Mercurial > ec-dotfiles
annotate moefetch.sh @ 161:52877e2849bb
misc fix. These past commits wasn't actually tested
author | edhoprima@gmail.com <edhoprima@gmail.com> |
---|---|
date | Fri, 05 Jun 2009 19:15:36 +0000 |
parents | 68227a30d0b3 |
children | 1f937c2e8b3f |
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 | |
159 | 73 xsltproc - "${TARGET_DIR}-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([jp][pn]g\)/\1\2.\3/g' | grep ^http > "${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}." | |
119 for FOLDER in temp trash deleted ${TARGET_DIR} | |
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 | |
125 echo "You don't own the ${BASE_DIR}/{$FOLDER}, applying globally writeable permission on it" | |
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 | |
129 [ `echo \`ls "${BASE_DIR}/${TARGET_DIR}" | wc -l\`` -eq 0 ] && ISNEW=1 | |
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() { | |
140 if [ "$ISNEW" -ne 1 ]; 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}" | |
146 for TRASH in `ls * | sed -e 's/\([0-9a-f]\{32\}.*\)//g' | grep -v ^$` | |
148 | 147 do |
159 | 148 mv -f "${TRASH}" "${BASE_DIR}/trash" |
149 echo "Moved ${TRASH} to ${BASE_DIR}/trash" | |
150 done | |
151 printf "" > "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | |
152 for FILE in * | |
153 do | |
154 if [ `${MD5} "${FILE}" | cut -d ' ' -f1 -` != `echo "${FILE}" | cut -d '.' -f1` ] | |
148 | 155 then |
159 | 156 echo |
157 echo "${FILE}" >> "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | |
158 echo "Error: ${FILE}" | |
148 | 159 fi |
160 printf "." | |
161 done | |
162 echo | |
159 | 163 |
164 # current dir: ${BASE_DIR}/temp | |
165 cd ${BASE_DIR}/temp | |
166 TOTAL_ERROR=`echo \`wc -l < "${SITE_DIR}-${TARGET_DIR}-error"\`` | |
167 echo "${TOTAL_ERROR} file(s) error" | |
148 | 168 |
169 echo "Generating list of new files..." | |
170 # THE FILES | |
159 | 171 #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
|
172 # |
159 | 173 ls "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | comm -1 -3 "${SITE_DIR}-${TARGET_DIR}-error" - > "${SITE_DIR}-${TARGET_DIR}-ok" |
174 cat "${SITE_DIR}-${TARGET_DIR}-list" | grep -vf "${SITE_DIR}-${TARGET_DIR}-ok" > "${SITE_DIR}-${TARGET_DIR}-newlist" | |
175 echo "`echo \`wc -l < \"${SITE_DIR}-${TARGET_DIR}-newlist\"\`` file(s) to be downloaded" | |
176 | |
177 # back to target dir | |
178 cd "${BASE_DIR}/${SITE_DIR}/${TARGET_DIR}" | |
148 | 179 |
180 echo "Removing error files" | |
159 | 181 if [ "${TOTAL_ERROR}" -eq 0 ]; then |
153 | 182 echo "No error file. 0 file removed" |
156
d3b002fd944e
fix: my attempt at speeding up things failed. reverting back to trusty grep -vf
edhoprima
parents:
155
diff
changeset
|
183 else |
159 | 184 cat "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-error" | xargs rm |
185 echo "${TOTAL_ERROR} file(s) removed" | |
148 | 186 fi |
187 echo "`echo \`ls | wc -l\`` file(s) available locally" | |
155
78ef726d3845
fix: whoops. Forgot to move back to workdir after
edhoprima
parents:
154
diff
changeset
|
188 cd ../temp |
148 | 189 else |
159 | 190 if [ "${ISQUICK}" ]; then |
152 | 191 echo "quick mode selected. Skipping check" |
192 else | |
193 echo "Empty local repository" | |
194 fi | |
159 | 195 cd "${BASE_DIR}/temp" |
196 cat "${SITE_DIR}-${TARGET_DIR}-list" > "${TARGET_DIR}-newlist" | |
148 | 197 fi |
198 } | |
199 | |
159 | 200 # start downloading the images |
201 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
|
202 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
|
203 if [ `echo \`wc -l < "${SITE_DIR}-${TARGET_DIR}-newlist"\`` -eq 0 ]; then |
148 | 204 echo "No new file" |
205 else | |
206 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
|
207 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
|
208 wget -e continue=on -bi "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}-newlist" -o "${BASE_DIR}/temp/${SITE_DIR}-${TARGET_DIR}.log" |
148 | 209 fi |
210 } | |
211 | |
159 | 212 # initialize base variables and initial command check |
213 Init(){ | |
214 # Get base folder - current folder or fallback to ${HOME} | |
215 [ "${BASE_DIR}" ] || BASE_DIR="${PWD}" | |
216 [ "${BASE_DIR}" ] || BASE_DIR="{$HOME}" | |
217 [ "`echo ${BASE_DIR} | cut -c1 | grep \/`" ] || BASE_DIR="/${BASE_DIR}" | |
218 # path initialization | |
219 [ "${ADDITIONAL_PATH}" ] && PATH=${ADDITIONAL_PATH}:${PATH} | |
220 export PATH | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
221 |
159 | 222 # misc variables |
223 ISQUICK="" | |
224 ISNEW="" | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
225 |
159 | 226 [ $# -lt 2 ] && Err_Help |
227 case "$1" in | |
228 status|fetch|quickfetch) | |
229 echo "Starting..." | |
230 JOB="$1" | |
231 ;; | |
232 *) | |
233 Err_Help | |
234 ;; | |
235 esac | |
236 shift | |
237 SITE= | |
238 case "$1" in | |
239 -s|--site) | |
240 shift | |
241 SITE="$1" | |
242 ;; | |
243 *) | |
161
52877e2849bb
misc fix. These past commits wasn't actually tested
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
160
diff
changeset
|
244 SITE="${DEFAULT_SITE}" |
159 | 245 ;; |
246 esac | |
247 shift | |
248 TAGS="$@" | |
249 echo "Tags: ${TAGS}" | |
250 # slash is not wanted for folder name | |
251 TARGET_DIR=`echo "${TAGS}" | sed -e 's/\//_/g'` | |
161
52877e2849bb
misc fix. These past commits wasn't actually tested
edhoprima@gmail.com <edhoprima@gmail.com>
parents:
160
diff
changeset
|
252 SITE_DIR=`echo "${SITE}" | sed -e 's/\/$//g;s/\//_/g'` |
159 | 253 } |
148 | 254 |
159 | 255 Msg_Welcome |
256 Init "$@" | |
257 Check_Tools | |
258 Check_Folders | |
158
cba73f6a96bb
grep check. OpenSolaris' default grep doesn't support -f
edhoprima
parents:
157
diff
changeset
|
259 |
148 | 260 |
159 | 261 # let's do the job! |
262 case "${JOB}" in | |
263 status) | |
264 Generate_Link | |
265 Check_Files | |
148 | 266 ;; |
159 | 267 fetch) |
268 Generate_Link | |
269 Check_Files | |
270 Fetch_Images | |
271 ;; | |
272 quickfetch) | |
273 ISNEW=1 | |
274 ISQUICK=1 | |
275 Generate_Link | |
276 Check_Files | |
277 Fetch_Images | |
148 | 278 ;; |
279 esac |