diff bin/xkcdmania @ 138:f963d52c031a

Merge.
author Edho Arief <edho@myconan.net>
date Wed, 02 Nov 2011 22:12:16 +0700
parents 5bafb912837e
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/xkcdmania	Wed Nov 02 22:12:16 2011 +0700
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+dir="/anime/misc/webcomics/xkcd"
+[ -d "${dir}" ] || exit 1
+
+fetchlink() {
+  if [ "$1" -a "$2" ]; then
+    wget --quiet -O "$2" "$1"
+  else
+    return 1
+  fi
+}
+latest=$(fetchlink http://xkcd.com/ - | grep 'Permanent link' | sed -e 's/.*\/\([0-9]*\)\/.*/\1/')
+[ -n "${latest}" ] || exit 1
+next=1
+while [ "${next}" -le "${latest}" ]; do
+  to_dl="${to_dl} ${next}"
+  next=$((next + 1))
+done
+
+for i in $to_dl
+do
+  if [ ! -n "$(find "$dir/$(printf '%04d' $i)"* -type f 2>/dev/null)" ] && [ "${i}" -ne 404 ]; then
+    page=$(fetchlink "http://xkcd.com/$i/" -)
+    img=$(echo "$page" | grep 'Image URL' | sed -e 's/.*\(http[^<]*\).*/\1/' | head -n 1)
+    echo "$(echo "$page" | grep "src" | grep "$img" | sed -e 's/.*title="\([^\"]*\)".*/\1/')" > "$dir/$(printf '%04d' $i) $(basename $img | sed -e 's/\(.*\)\..*/\1/').txt"
+    fetchlink "${img}" "$dir/$(printf '%04d' $i) $(basename "$img")"
+  fi
+done