kenorb

imgur album downloader (v20121220)

Nov 11th, 2012
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.84 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # imgur profile album downloader
  4. # Homepage: github.com/kenorb/imgur-downloader
  5. #
  6. # Usage: ./$0 URL
  7. #
  8. # Supported links:
  9. # - profile pages (http://user.imgur.com/)
  10. # - albums (http://imgur.com/a/xXyYz)
  11. # - multiple albums (http://imgur.com/a/xXyYz http://imgur.com/a/xXyYz)
  12. #
  13. # Not supported:
  14. # - direct links (http://imgur.com/xXxXz)
  15. # - galleries (http://imgur.com/r/gallery)
  16. #
  17. # Requirements: seq or gseq, wget, uniq
  18. # Tested on: Linux, Mac
  19.  
  20. INDEX=index.html.$RANDOM
  21. ALIST=albums.list.$RANDOM
  22. ILIST=images.list.$RANDOM
  23. EXTS="(jpg|jpeg|gif)"
  24. ALBUM=$(echo $1 | grep -oE "\w+" | sed -n 2p)
  25. DELAY=0.2 # Delay for each request (e.g. 0.2, 1)
  26. WGET="wget -w$DELAY --content-disposition -UMozilla"
  27. SEQ=$(which seq || which gseq)
  28. which $SEQ && which wget && which uniq || exit 1  # Check the requirements
  29.  
  30. # Fetch the user's album page.
  31. $WGET -O $INDEX $*
  32. # Strip the user's page down to a list of album hyperlinks.
  33. cat $INDEX | grep -Eo "imgur.com/a/\w+" > $ALIST
  34.  
  35. # Count how many albums we are to fetch.
  36. COUNT=$(wc -l $ALIST | awk '{print $1}')
  37.  
  38. # Descend into a loop to fetch all of the images.
  39. for i in $($SEQ 1 $COUNT)
  40. do
  41.         # Make a directory.
  42.         ADIR=$(cat $ALIST | sed -n "${i}p" | awk -F/ '{print $NF}' || echo $i)
  43.         mkdir -vp $ALBUM/$ADIR || continue
  44.         cd $ALBUM/$ADIR
  45.         # Fetch this particular album index.
  46.         $WGET -c $(cat ../../$ALIST | sed -n "${i}p")        # Variable-ize it.
  47.         mv $(ls -1 . | grep -vE $EXTS) $ILIST
  48.         # Strip the file down to the actual image links.
  49.         cat $ILIST | grep -Eo "http://i.imgur.com.*$EXTS"  | sed -E "s/s.(jpg|jpeg|gif)/.\1/g" | uniq > $ILIST
  50.         # Fetch the album's images.
  51.         $WGET -c -i $ILIST
  52.         # Cleanup.
  53.         rm -v $ILIST
  54.         # Repeat.
  55.         cd ../..
  56. done
  57.  
  58. # Cleanup.
  59. rm -v $INDEX $ALIST
Advertisement
Add Comment
Please, Sign In to add comment