-
Notifications
You must be signed in to change notification settings - Fork 3
/
dld-me-com.sh
executable file
·358 lines (301 loc) · 10.3 KB
/
dld-me-com.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# Script for downloading the contents of a .me.com domain for one user.
#
# Usage: dld-me-com.sh ${DOMAIN} ${USERNAME}
# where DOMAIN is one of gallery.me.com
# web.me.com
# public.me.com
# homepage.mac.com
#
VERSION="20120621.01"
# this script needs wget-warc, which you can find on the ArchiveTeam wiki.
# set the WGET_WARC environment variable to point to the wget-warc executable.
if [[ ! -x $WGET_WARC ]]
then
WGET_WARC=$(which wget)
if ! $WGET_WARC --help | grep -q WARC
then
echo "${WGET_WARC} does not support WARC. Set the WGET_WARC environment variable."
exit 3
fi
fi
if [[ ! -x $WGET_WARC ]]
then
echo "wget-warc not found. Set the WGET_WARC environment variable."
exit 3
fi
# the script also needs curl with SSL support
if ! builtin type -p curl &>/dev/null
then
echo "You don't have curl."
exit 3
fi
if ! curl -V | grep -q SSL
then
echo "Your version of curl doesn't have SSL support."
exit 3
fi
USER_AGENT="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
if [ -z $DATA_DIR ]
then
DATA_DIR=data
fi
domain="$1"
username="$2"
userdir="$DATA_DIR/${username:0:1}/${username:0:2}/${username:0:3}/${username}/${domain}"
if [[ -f "${userdir}/.incomplete" ]]
then
echo " Deleting incomplete result for ${domain}/${username}"
rm -rf "${userdir}"
fi
if [[ -d "${userdir}" ]]
then
echo " Already downloaded ${domain}/${username}"
exit 2
fi
mkdir -p "${userdir}"
touch "${userdir}/.incomplete"
echo " Downloading ${domain}/${username}"
# step 1: download the list of files
if [[ "$domain" =~ "public.me.com" ]]
then
# public.me.com has real WebDAV
# PROPFIND with Depth: infinity lists all files
echo -n " - Discovering urls (XML)..."
curl "https://public.me.com/ix/${username}/" \
--silent \
--request PROPFIND \
--header "Content-Type: text/xml; charset=\"utf-8\"" \
--header "Depth: infinity" \
--data '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>' \
--user-agent "${USER_AGENT}" \
> "$userdir/webdav-feed.xml"
result=$?
if [ $result -ne 0 ]
then
echo " ERROR ($result)."
exit 1
fi
echo " done."
# grep for href, strip <D:href> and prepend https://public.me.com
grep -o -E "<D:href>[^<]+" "$userdir/webdav-feed.xml" | cut -c 9- | awk '/[^\/]$/ { print "https://public.me.com" $1 }' | sort | uniq > "$userdir/urls.txt"
count=$( cat "$userdir/urls.txt" | wc -l )
elif [[ ! "$domain" =~ "homepage.mac.com" ]]
then
# web.me.com and gallery.me.com use query-string WebDAV
# there's a json feed...
echo -n " - Discovering urls (JSON)..."
curl "http://${domain}/${username}/?webdav-method=truthget&feedfmt=json&depth=Infinity" \
--silent \
--user-agent "${USER_AGENT}" \
> "$userdir/webdav-feed.json"
result=$?
if [ $result -ne 0 ]
then
echo " ERROR ($result)."
exit 1
fi
echo " done."
# ... and an xml feed
echo -n " - Discovering urls (XML)..."
curl "http://${domain}/${username}/?webdav-method=truthget&depth=Infinity" \
--silent \
--user-agent "${USER_AGENT}" \
> "$userdir/webdav-feed.xml"
result=$?
if [ $result -ne 0 ]
then
echo " ERROR ($result)."
exit 1
fi
echo " done."
# for web.me.com we look at the xml feed, which contains the files,
# for gallery.me.com we use the json feed, which lists the images
if [[ "$domain" =~ "web.me.com" ]]
then
grep "href=\"" "$userdir/webdav-feed.xml" | grep -oE "http://${domain}/[^\"<]+" | sort | uniq > "$userdir/urls.txt"
elif [[ "$domain" =~ "gallery.me.com" ]]
then
# we do not want the ?derivative=...
grep -oE "http://${domain}/[^\"<]+" "$userdir/webdav-feed.json" \
| grep -E "\.([a-zA-Z0-9]+)$" \
| sort | uniq \
> "$userdir/urls.txt"
else
echo " Invalid domain ${domain}."
exit 1
fi
# let's save the feeds in the warc file
echo "http://${domain}/${username}/?webdav-method=truthget&feedfmt=json&depth=Infinity" >> "$userdir/urls.txt"
echo "http://${domain}/${username}/?webdav-method=truthget&depth=Infinity" >> "$userdir/urls.txt"
count=$( cat "$userdir/urls.txt" | wc -l )
fi
# some web.me.com sites use iWeb, which doesn't always show up in the feed-XML
if [[ "$domain" =~ "web.me.com" ]]
then
# first, we crawl the site
echo -n " - Discovering iWeb (directories)..."
$WGET_WARC -U "$USER_AGENT" -nv -o "$userdir/wget-discovery.log" \
--directory-prefix="$userdir/files/" \
-r -l inf --no-remove-listing \
--trust-server-names \
"http://${domain}/$username/" \
--no-check-certificate
result=$?
if [ $result -ne 0 ] && [ $result -ne 6 ] && [ $result -ne 8 ]
then
echo " ERROR ($result)."
exit 1
fi
rm -rf "$userdir/files/"
echo " done."
# we should download the files we've discovered
cut -d " " -f 3 "$userdir/wget-discovery.log" \
| grep URL: | cut -c 5- >> "$userdir/urls.txt"
echo -n " - Discovering iWeb (feed.xml)..."
# then we look at the directories we've discovered
directories=$( grep -oE "http://web.me.com.+/" "$userdir/urls.txt" | sort | uniq )
for d in $directories
do
# download the feed.xml for this directory
feedxml_url="${d}feed.xml"
extra_files=$( curl "${feedxml_url}" --silent --user-agent "${USER_AGENT}" \
| grep -oE 'href="[^"]+' | cut -c 7- )
for f in $extra_files
do
if [[ ! $f =~ ^http ]]
then
f="${d}${f}"
fi
echo $f >> "$userdir/urls.txt"
done
# add it to the final download
echo "$feedxml_url" >> "$userdir/urls.txt"
done
echo " done."
# some sites have a Sites.rss with urls
echo -n " - Looking for Sites.rss..."
# get Sites.rss, extract urls
curl "http://${domain}/${username}/Sites.rss" --silent --user-agent "${USER_AGENT}" \
| grep -oE '<link>[^<]+' | cut -c 7- | sed "s/web.mac.com/web.me.com/" >> "$userdir/urls.txt"
# add Sites.rss to WARC
echo "http://${domain}/${username}/Sites.rss" >> "$userdir/urls.txt"
echo " done."
# sometimes Sites.rss or the feeds include links to external domains,
# the user's web site. these domains don't always exist.
#
# to prevent wget from returning a dns error and since the content
# on web.me.com is the same as that on the external domain we
# only include the urls from web.me.com
echo -n " - Sorting url list..."
cat "$userdir/urls.txt" \
| grep -E "^http://web.me.com/" \
| sort | uniq > "$userdir/unique-urls.txt"
mv "$userdir/unique-urls.txt" "$userdir/urls.txt"
echo " done."
count=$( cat "$userdir/urls.txt" | wc -l )
fi
# step 2: use the url list to download the files
if [[ "$domain" =~ "homepage.mac.com" ]]
then
# homepage.mac.com doesn't have a feed with file names, so we'll use wget --mirror
echo -n " - Running wget --mirror (takes a while)..."
$WGET_WARC -U "$USER_AGENT" -nv -o "$userdir/wget.log" \
--directory-prefix="$userdir/files/" \
-r -l inf --no-remove-listing \
--accept-regex="homepage\.mac\.com/i/|/${username}/" \
--reject-regex='///|\.html?/' \
--trust-server-names \
--page-requisites "http://${domain}/$username/" \
--exclude-directories="/WebObjects/FileSharing.woa/" \
--no-check-certificate \
--warc-file="$userdir/${domain}-$username" --warc-max-size=inf \
--warc-header="operator: Archive Team" \
--warc-header="mobileme-dld-script-version: ${VERSION}" \
--warc-header="mobileme: ${domain}, ${username}"
result=$?
if [ $result -ne 0 ] && [ $result -ne 6 ] && [ $result -ne 8 ]
then
echo " ERROR ($result)."
exit 1
fi
rm -rf "$userdir/files/"
echo " done."
elif [[ "$domain" =~ "web.me.com" ]]
then
# for web.me.com we should use --mirror and --page-requisites
# for some reason wget does not always create the directories,
# so we'll do it in advance
echo -n " - Preparing directory structure..."
mkdir -p "$userdir/files"
if builtin type -p perl &>/dev/null
then
perl -M'File::Basename' -n -e '
chomp;
s,^https?://,,;
s/\+/ /g;
s/%(..)/chr(hex($1))/eg;
print dirname($_), "\0"' "$userdir/urls.txt" \
| (cd "$userdir/files" && xargs -0 mkdir -p)
else
cat "$userdir/urls.txt" | while read url
do
url=${url/#http:\/\//}
url=${url/#https:\/\//}
url=$( echo "$url" | sed 's/+/ /g; s/%/\\x/g' )
url=$( echo -e "$url" )
url_path="$userdir/files/"$( dirname "$url" )
[ ! -d "$url_path" ] && mkdir -p "$url_path"
done
fi
echo " done."
echo -n " - Running wget --mirror (at least ${count} files)..."
$WGET_WARC -U "$USER_AGENT" -nv -o "$userdir/wget.log" \
-i "$userdir/urls.txt" \
--directory-prefix="$userdir/files/" \
-r -l inf --no-remove-listing \
--accept-regex="web\.me\.com/i/|www\.me\.com|/${username}/" \
--reject-regex='///|\.html?/' \
--trust-server-names \
--page-requisites \
--span-hosts --domains="web.me.com,www.me.com" \
--exclude-directories="/g/" \
--no-check-certificate \
--warc-file="$userdir/${domain}-$username" --warc-max-size=inf \
--warc-header="operator: Archive Team" \
--warc-header="mobileme-dld-script-version: ${VERSION}" \
--warc-header="mobileme: ${domain}, ${username}"
result=$?
if [ $result -ne 0 ] && [ $result -ne 6 ] && [ $result -ne 8 ]
then
echo " ERROR ($result)."
exit 1
fi
rm -rf "$userdir/files/"
echo " done."
else
# for the other domains we just grab every url on the list
echo -n " - Downloading (${count} files)..."
$WGET_WARC -U "$USER_AGENT" -nv -o "$userdir/wget.log" -i "$userdir/urls.txt" -O /dev/null \
--no-check-certificate \
--warc-file="$userdir/${domain}-$username" --warc-max-size=inf \
--warc-header="operator: Archive Team" \
--warc-header="mobileme-dld-script-version: ${VERSION}" \
--warc-header="mobileme: ${domain}, ${username}"
result=$?
if [ $result -ne 0 ] && [ $result -ne 6 ] && [ $result -ne 8 ]
then
echo " ERROR ($result)."
exit 1
fi
echo " done."
fi
echo -n " - Result: "
if du --help | grep -q apparent-size
then
du --apparent-size -hs "$userdir/${domain}-$username"* | cut -f 1
else
du -hs "$userdir/${domain}-$username"* | cut -f 1
fi
rm "${userdir}/.incomplete"
exit 0