annotate moefetch.sh @ 151:3d6b2152f4a3

it didn't work on freebsd's md5
author edhoprima
date Sat, 25 Apr 2009 15:47:27 +0000
parents 9338901a348a
children 67df02877319
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
edhoprima
parents:
diff changeset
1 #!/bin/sh
edhoprima
parents:
diff changeset
2
edhoprima
parents:
diff changeset
3 # Copyright (c) 2009, edogawaconan <me@myconan.net>
edhoprima
parents:
diff changeset
4 #
edhoprima
parents:
diff changeset
5 # Permission to use, copy, modify, and/or distribute this software for any
edhoprima
parents:
diff changeset
6 # purpose with or without fee is hereby granted, provided that the above
edhoprima
parents:
diff changeset
7 # copyright notice and this permission notice appear in all copies.
edhoprima
parents:
diff changeset
8 #
edhoprima
parents:
diff changeset
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
edhoprima
parents:
diff changeset
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
edhoprima
parents:
diff changeset
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
edhoprima
parents:
diff changeset
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
edhoprima
parents:
diff changeset
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
edhoprima
parents:
diff changeset
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
edhoprima
parents:
diff changeset
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
edhoprima
parents:
diff changeset
16
edhoprima
parents:
diff changeset
17 # Version 0.1-alpha1. Lots of bugs here. Use with care
edhoprima
parents:
diff changeset
18 # USE WITH CARE
edhoprima
parents:
diff changeset
19
edhoprima
parents:
diff changeset
20 # what it does: fetch every picture that has the specified tags.
edhoprima
parents:
diff changeset
21
edhoprima
parents:
diff changeset
22 # requirement: wget, libxslt, md5sum (or md5)
edhoprima
parents:
diff changeset
23
edhoprima
parents:
diff changeset
24 # configs
edhoprima
parents:
diff changeset
25 # program additional paths for: cut, sed, wc, md5(sum), wget, xsltproc
edhoprima
parents:
diff changeset
26 extrapath=
edhoprima
parents:
diff changeset
27
edhoprima
parents:
diff changeset
28 # md5 calculation, expected output: <32digit md5><space(s)><filename>
edhoprima
parents:
diff changeset
29 # gnu: "md5sum", bsd: "md5 -r"
edhoprima
parents:
diff changeset
30 md5="md5 -r"
edhoprima
parents:
diff changeset
31
edhoprima
parents:
diff changeset
32 # server address. Danbooru only! I do not take responsibility of stupidity.
edhoprima
parents:
diff changeset
33 site="moe.imouto.org"
edhoprima
parents:
diff changeset
34
edhoprima
parents:
diff changeset
35 # 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.
edhoprima
parents:
diff changeset
36 # Structure is $basedir/<tags>
edhoprima
parents:
diff changeset
37 basedir="/home/ifail/test"
edhoprima
parents:
diff changeset
38
edhoprima
parents:
diff changeset
39 # not user modifiable from here
edhoprima
parents:
diff changeset
40
edhoprima
parents:
diff changeset
41 GENERATE()
edhoprima
parents:
diff changeset
42 {
edhoprima
parents:
diff changeset
43 echo
edhoprima
parents:
diff changeset
44 echo "Fetching xml file"
edhoprima
parents:
diff changeset
45 wget "http://$site/post/index.xml?tags=$tags&offset=0&limit=100000" -O "$outdir-xml" -e continue=off
edhoprima
parents:
diff changeset
46 echo "Processing XML file..."
edhoprima
parents:
diff changeset
47 # xslt evilry
edhoprima
parents:
diff changeset
48 xsltproc - "$outdir-xml" <<EOF | sed 's/.*\(http.*\)\(\/[a-f0-9]\{32\}\).*\.\([jp][pn]g\)/\1\2.\3/g' | grep ^http > "$outdir-list"
edhoprima
parents:
diff changeset
49 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
edhoprima
parents:
diff changeset
50 <xsl:output method="xml" indent="yes"/>
edhoprima
parents:
diff changeset
51 <xsl:template match="post">
edhoprima
parents:
diff changeset
52 <xsl:value-of select="@file_url" />
edhoprima
parents:
diff changeset
53 </xsl:template>
edhoprima
parents:
diff changeset
54 </xsl:stylesheet>
edhoprima
parents:
diff changeset
55 EOF
edhoprima
parents:
diff changeset
56 echo "`echo \`wc -l < \"$outdir-list\" \`` file(s) available on server"
edhoprima
parents:
diff changeset
57 #output file: $outdir-list
edhoprima
parents:
diff changeset
58 }
edhoprima
parents:
diff changeset
59
edhoprima
parents:
diff changeset
60 CHECK()
edhoprima
parents:
diff changeset
61 {
edhoprima
parents:
diff changeset
62 if [ "$ISNEW" -ne 1 ];then
edhoprima
parents:
diff changeset
63 echo "Checking for errors..."
edhoprima
parents:
diff changeset
64 # THE FILES
edhoprima
parents:
diff changeset
65 echo > "$outdir-error"
edhoprima
parents:
diff changeset
66 cd "../$outdir"
edhoprima
parents:
diff changeset
67 for file in `ls`
edhoprima
parents:
diff changeset
68 do
edhoprima
parents:
diff changeset
69 if [ `$md5 "$file" | cut -d ' ' -f1 -` != `echo "$file" | cut -d '.' -f1` ]
edhoprima
parents:
diff changeset
70 then
edhoprima
parents:
diff changeset
71 echo "$file" >> "../temp/$outdir-error"
edhoprima
parents:
diff changeset
72 echo "Error: $file"
edhoprima
parents:
diff changeset
73 fi
edhoprima
parents:
diff changeset
74 printf "."
edhoprima
parents:
diff changeset
75 done
edhoprima
parents:
diff changeset
76 echo
edhoprima
parents:
diff changeset
77 cd ../temp
edhoprima
parents:
diff changeset
78 totalerr=`wc -l < $tags-error`
edhoprima
parents:
diff changeset
79 echo "$totalerr file(s) error"
edhoprima
parents:
diff changeset
80
edhoprima
parents:
diff changeset
81 echo "Generating list of new files..."
edhoprima
parents:
diff changeset
82 # THE FILES
edhoprima
parents:
diff changeset
83 #ls "../$outdir" | grep -vf "$outdir-error" > "$outdir-ok"
edhoprima
parents:
diff changeset
84 #cat "$outdir-list" | grep -vf "$outdir-ok" > "$outdir-newlist"
edhoprima
parents:
diff changeset
85 ls "../$outdir" | comm -1 -2 "$outdir-error" - > "$outdir-ok"
edhoprima
parents:
diff changeset
86 comm -1 -2 "$outdir-list" "$outdir-error" > "$outdir-newlist"
edhoprima
parents:
diff changeset
87 echo "`echo \`wc -l < \"$outdir-newlist\"\`` file(s) to be downloaded"
edhoprima
parents:
diff changeset
88 cd "../$outdir"
edhoprima
parents:
diff changeset
89
edhoprima
parents:
diff changeset
90 echo "Removing error files"
edhoprima
parents:
diff changeset
91 if [ $totalerr -gt 0 ]
edhoprima
parents:
diff changeset
92 then
edhoprima
parents:
diff changeset
93 cat "../temp/$outdir-error" | xargs rm
edhoprima
parents:
diff changeset
94 fi
edhoprima
parents:
diff changeset
95 echo "$totalerr file(s) removed"
edhoprima
parents:
diff changeset
96 echo "`echo \`ls | wc -l\`` file(s) available locally"
edhoprima
parents:
diff changeset
97 cd ..
edhoprima
parents:
diff changeset
98 else
edhoprima
parents:
diff changeset
99 echo "Empty local repository"
edhoprima
parents:
diff changeset
100 cat "$outdir-list" > "$outdir-newlist"
edhoprima
parents:
diff changeset
101 fi
edhoprima
parents:
diff changeset
102 }
edhoprima
parents:
diff changeset
103
edhoprima
parents:
diff changeset
104 FETCH()
edhoprima
parents:
diff changeset
105 {
edhoprima
parents:
diff changeset
106 if [ `wc -l < "$outdir-newlist"` -eq 0 ]
edhoprima
parents:
diff changeset
107 then
edhoprima
parents:
diff changeset
108 echo "No new file"
edhoprima
parents:
diff changeset
109 else
edhoprima
parents:
diff changeset
110 echo "Starting wget"
edhoprima
parents:
diff changeset
111 cd "../$outdir"
edhoprima
parents:
diff changeset
112 wget -bi "../temp/$outdir-newlist" -o "../temp/$outdir.log"
edhoprima
parents:
diff changeset
113 fi
edhoprima
parents:
diff changeset
114 }
edhoprima
parents:
diff changeset
115
edhoprima
parents:
diff changeset
116
edhoprima
parents:
diff changeset
117 # path initialization
edhoprima
parents:
diff changeset
118 export PATH=${PATH}:$extrapath
edhoprima
parents:
diff changeset
119
edhoprima
parents:
diff changeset
120 # verify all programs required do indeed exist
edhoprima
parents:
diff changeset
121 # basic tools
edhoprima
parents:
diff changeset
122 commands="cut sed wc wget xsltproc xargs rm mkdir chown comm"
edhoprima
parents:
diff changeset
123 cmderr=" "
edhoprima
parents:
diff changeset
124 for cmd in $commands
edhoprima
parents:
diff changeset
125 do
edhoprima
parents:
diff changeset
126 command -v "$cmd" >/dev/null || cmderr="$cmderr $cmd"
edhoprima
parents:
diff changeset
127 done
edhoprima
parents:
diff changeset
128 if [ x"$cmderr" != x" " ]; then
edhoprima
parents:
diff changeset
129 echo "$cmderr doesn't exist in $PATH"
edhoprima
parents:
diff changeset
130 exit 1
edhoprima
parents:
diff changeset
131 fi
edhoprima
parents:
diff changeset
132 #md5
edhoprima
parents:
diff changeset
133 md5base=`echo $md5 | cut -d ' ' -f 1 -`
edhoprima
parents:
diff changeset
134 if [ x`command -v "$md5base" >/dev/null || echo x` != "x" ]; then
edhoprima
parents:
diff changeset
135 echo "$md5base doesn't exist in $PATH"
edhoprima
parents:
diff changeset
136 exit 1
edhoprima
parents:
diff changeset
137 fi
150
9338901a348a hahaha I broke it
edhoprima
parents: 149
diff changeset
138 mdtest=
151
3d6b2152f4a3 it didn't work on freebsd's md5
edhoprima
parents: 150
diff changeset
139 if [ `echo test | $md5 | cut -d ' ' -f 1 -` != "d8e8fca2dc0f896fd7cb4cb0031ba249" ]; then
148
edhoprima
parents:
diff changeset
140 echo "$md5 doesn't produce wanted output"
edhoprima
parents:
diff changeset
141 exit 1
edhoprima
parents:
diff changeset
142 fi
edhoprima
parents:
diff changeset
143
edhoprima
parents:
diff changeset
144
edhoprima
parents:
diff changeset
145 # all green (part 1)! let's go (until we check the tag)
edhoprima
parents:
diff changeset
146
edhoprima
parents:
diff changeset
147 # initialization
edhoprima
parents:
diff changeset
148 # are we really doing it?
edhoprima
parents:
diff changeset
149 if [ $# -lt 2 ]; then
149
7200b4339aea whoops.
edhoprima
parents: 148
diff changeset
150 echo "Usage: moefetch status|fetch|update <tags>"
148
edhoprima
parents:
diff changeset
151 exit 1
edhoprima
parents:
diff changeset
152 fi
edhoprima
parents:
diff changeset
153
edhoprima
parents:
diff changeset
154 case "$1" in
edhoprima
parents:
diff changeset
155 status|get|update)
edhoprima
parents:
diff changeset
156 echo "Starting..."
edhoprima
parents:
diff changeset
157 ;;
edhoprima
parents:
diff changeset
158 *)
edhoprima
parents:
diff changeset
159 echo "Usage: moefetch status|fetch|update <tags>"
edhoprima
parents:
diff changeset
160 exit 1
edhoprima
parents:
diff changeset
161 ;;
edhoprima
parents:
diff changeset
162 esac
edhoprima
parents:
diff changeset
163
edhoprima
parents:
diff changeset
164 # we did it indeed
edhoprima
parents:
diff changeset
165 # get started
edhoprima
parents:
diff changeset
166
edhoprima
parents:
diff changeset
167 # do we own the files
edhoprima
parents:
diff changeset
168 tags=`echo "$@" | cut -d ' ' -f 2- -`
edhoprima
parents:
diff changeset
169 echo "Tags: $tags"
edhoprima
parents:
diff changeset
170 # slash do not want
edhoprima
parents:
diff changeset
171 outdir=`echo "$tags" | sed -e 's/\//_/g'`
edhoprima
parents:
diff changeset
172 if [ -O "$basedir" ]; then
edhoprima
parents:
diff changeset
173 if [ ! -d "$basedir/$outdir" ]; then
edhoprima
parents:
diff changeset
174 ISNEW=1
edhoprima
parents:
diff changeset
175 mkdir "$basedir/$outdir"
edhoprima
parents:
diff changeset
176 fi
edhoprima
parents:
diff changeset
177 if [ ! -O "$basedir/$outdir" ]; then
edhoprima
parents:
diff changeset
178 echo "You don't own the $basedir/$outdir, applying globally writeable permission on it"
edhoprima
parents:
diff changeset
179 chmod -R u=rwX,g=rwX,o=rwX "$basedir/$outdir"
edhoprima
parents:
diff changeset
180 fi
edhoprima
parents:
diff changeset
181 if [ `echo \`ls "$basedir/$outdir" | wc -l\`` -eq 0 ]; then
edhoprima
parents:
diff changeset
182 ISNEW=1
edhoprima
parents:
diff changeset
183 fi
edhoprima
parents:
diff changeset
184 if [ ! -d "$basedir/temp" ]; then
edhoprima
parents:
diff changeset
185 mkdir "$basedir/temp"
edhoprima
parents:
diff changeset
186 fi
edhoprima
parents:
diff changeset
187 if [ ! -O "$basedir/temp" ]; then
edhoprima
parents:
diff changeset
188 echo "You don't own the $basedir/temp, applying globally writeable permission on it"
edhoprima
parents:
diff changeset
189 chmod -R u=rwX,g=rwX,o=rwX "$basedir/temp"
edhoprima
parents:
diff changeset
190 fi
edhoprima
parents:
diff changeset
191 else
edhoprima
parents:
diff changeset
192 echo "Fatal error: you don't own ${basedir}. Please fix ${basedir}. Stopping"
edhoprima
parents:
diff changeset
193 exit 1
edhoprima
parents:
diff changeset
194 fi
edhoprima
parents:
diff changeset
195 # let's move to workdir
edhoprima
parents:
diff changeset
196 cd "$basedir/temp"
edhoprima
parents:
diff changeset
197 touch "$outdir-error"
edhoprima
parents:
diff changeset
198 touch "$outdir-ok"
edhoprima
parents:
diff changeset
199 touch "$outdir-list"
edhoprima
parents:
diff changeset
200 touch "$outdir-newlist"
edhoprima
parents:
diff changeset
201 #
edhoprima
parents:
diff changeset
202
edhoprima
parents:
diff changeset
203 # let's do the job!
edhoprima
parents:
diff changeset
204 case "$1" in
edhoprima
parents:
diff changeset
205 status)
edhoprima
parents:
diff changeset
206 GENERATE
edhoprima
parents:
diff changeset
207 CHECK
edhoprima
parents:
diff changeset
208 ;;
edhoprima
parents:
diff changeset
209 get)
edhoprima
parents:
diff changeset
210 GENERATE
edhoprima
parents:
diff changeset
211 CHECK
edhoprima
parents:
diff changeset
212 FETCH
edhoprima
parents:
diff changeset
213 ;;
edhoprima
parents:
diff changeset
214 update)
edhoprima
parents:
diff changeset
215 FETCH
edhoprima
parents:
diff changeset
216 ;;
edhoprima
parents:
diff changeset
217 esac