root > misc > scripts > image-get

image-get

text/x-shellscript, 1790 bytes (load raw)
#!/bin/bash

wiki="$1"
img="$2"
tgt="$3"

ext="${img##*.}"
base="${img%.*}"

if [ -z "$tgt" ]; then tgt='.'; fi
if [ -d "$tgt" ]; then tgt="$tgt/$base"; fi

if [ -z "$wiki" ] || [ "$wiki" == '-' ]; then wiki="commons.wikimedia.org"; fi

u="http://$wiki/wiki/Image:$img"
echo "$u" > "$tgt.url"

u="$u?action=render"
echo "fetching description from <$u>..."
wget -q -O "$tgt.html" "$u" 

if [ $? -ne 0 ]; then
	echo "failed to fetch $u"
	exit 6
fi

echo "
<hr/><p style='text-align:center'><b>From: <a href='http://$wiki/wiki/Image:$img'>http://$wiki/wiki/Image:$img</a></b></p>
" >> "$tgt.html"

u="http://$wiki/w/query.php?what=imageinfo&iihistory&iiurl&titles=Image:$img"

echo "fetching metadata from <$u>..."
wget -q -O "$tgt.meta.phps" "$u&format=php" && wget -q -O "$tgt.meta.xml" "$u&format=xml" && wget -q -O "$tgt.meta.json" "$u&format=json"

if [ $? -ne 0 ]; then
	echo "failed to fetch $u"
	exit 7
fi

xsl=`dirname "$0"`/upload-history.xsl

if [ -e "$xsl" ] && which xsltproc >/dev/null 2>&1; then
	echo "processing metadata..."
	xsltproc "$xsl" "$tgt.meta.xml" >> "$tgt.html"
fi

u="http://$wiki/wiki/Special:Export?action=submit&pages=Image:$img"
#u="$u&curonly=1" #NOTE: we want all revisions. but that was broken for a while

echo "fetching page export from <$u>..."
wget -q -O "$tgt.page.xml" "$u" 

if [ $? -ne 0 ]; then
	echo "failed to fetch $u"
	exit 8
fi

u="http://$wiki/wiki/Image:$img?action=raw"

echo "fetching page text from <$u>..."
wget -q -O "$tgt.page.wiki" "$u" 

if [ $? -ne 0 ]; then
	echo "failed to fetch $u"
	exit 8
fi

u="http://$wiki/wiki/Special:Filepath/$img"

echo "fetching image from <$u>..."
wget -q -O "$tgt.$ext" "$u" 

if [ $? -ne 0 ]; then
	echo "failed to fetch $u"
	exit 9
fi

echo "$img" > "$tgt.name"

echo "fetched image $img to $tgt.*"