Mercurial > ec-dotfiles
comparison bin/xkcdmania @ 138:f963d52c031a
Merge.
author | Edho Arief <edho@myconan.net> |
---|---|
date | Wed, 02 Nov 2011 22:12:16 +0700 |
parents | 5bafb912837e |
children |
comparison
equal
deleted
inserted
replaced
137:983beb13c736 | 138:f963d52c031a |
---|---|
1 #!/bin/sh | |
2 | |
3 dir="/anime/misc/webcomics/xkcd" | |
4 [ -d "${dir}" ] || exit 1 | |
5 | |
6 fetchlink() { | |
7 if [ "$1" -a "$2" ]; then | |
8 wget --quiet -O "$2" "$1" | |
9 else | |
10 return 1 | |
11 fi | |
12 } | |
13 latest=$(fetchlink http://xkcd.com/ - | grep 'Permanent link' | sed -e 's/.*\/\([0-9]*\)\/.*/\1/') | |
14 [ -n "${latest}" ] || exit 1 | |
15 next=1 | |
16 while [ "${next}" -le "${latest}" ]; do | |
17 to_dl="${to_dl} ${next}" | |
18 next=$((next + 1)) | |
19 done | |
20 | |
21 for i in $to_dl | |
22 do | |
23 if [ ! -n "$(find "$dir/$(printf '%04d' $i)"* -type f 2>/dev/null)" ] && [ "${i}" -ne 404 ]; then | |
24 page=$(fetchlink "http://xkcd.com/$i/" -) | |
25 img=$(echo "$page" | grep 'Image URL' | sed -e 's/.*\(http[^<]*\).*/\1/' | head -n 1) | |
26 echo "$(echo "$page" | grep "src" | grep "$img" | sed -e 's/.*title="\([^\"]*\)".*/\1/')" > "$dir/$(printf '%04d' $i) $(basename $img | sed -e 's/\(.*\)\..*/\1/').txt" | |
27 fetchlink "${img}" "$dir/$(printf '%04d' $i) $(basename "$img")" | |
28 fi | |
29 done |