Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/5920d0b920e4844cec7e to your computer and use it in GitHub Desktop.
Save eusonlito/5920d0b920e4844cec7e to your computer and use it in GitHub Desktop.
#!/bin/bash
url=$1
if [ "`echo $url | grep 'flickr.com'`" == '' ]; then
echo ''
echo 'This url seems not to be a flickr url ('$url')'
echo ''
exit 1
fi
if [ ! -d 'images' ]; then
mkdir 'images'
fi
if [ ! -d 'cache' ]; then
mkdir 'cache'
fi
md5=`echo -n "$url" | md5sum | sed s/[^a-z0-9]//g`
if [ ! -f "cache/$md5" ]; then
curl -s --cookie-jar 'cookie' "$url" > "cache/$md5"
fi
html=`cat "cache/$md5"`
cookie=`echo $html | grep -o '"magic_cookie":"[^"]\+"' | sed 's/.*:"\([^"]\+\)"/\1/'`
page=0
while true; do
page=$(($page + 1))
md5=`echo -n "$url?data=1&append=1&page=$page&magic_cookie=$cookie" | md5sum | sed s/[^a-z0-9]//g`
if [ ! -f "cache/$md5" ]; then
curl -s --cookie 'cookie' "$url?data=1&append=1&page=$page&magic_cookie=$cookie" > "cache/$md5"
fi
images=`cat "cache/$md5" | grep -o 'https[^"]\+_o\.jpg' | sort | uniq | sed 's#\\\##g; s#//[^\.]\+\.\([^/]\+\)/\([0-9]\)/#//farm\2.\1/#g'`
cnt=`echo $images | grep -o ' ' | wc -l`
echo ''
echo "Downloading $cnt pictures"
echo "$url?data=1&append=1&page=$page&magic_cookie=$cookie"
echo ''
if [ "$cnt" -eq 0 ] || [ "$images" == '' ]; then
break;
fi
i=0
for image in $images; do
i=$(($i + 1))
file='images/'$(basename "$image")
if [ -f "$file" ] && [ -s "$file" ]; then
echo "Picture $file already exists ($i / $cnt)"
continue
fi
echo "Downloading $image ($i / $cnt)"
wget --quiet "$image" -O "$file"
done
done
echo ''
echo 'Download complete'
echo ''
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment