Guest User

flickr-get

a guest
Nov 29th, 2010
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.21 KB | None | 0 0
  1. #!/bin/sh
  2. # flickr-get, version 0.5.
  3. #
  4. # Downloads entire flickr photostreams with the help of wget.
  5. # This is somewhat hacky, and will probably break when flickr
  6. # changes its website layout.
  7. #
  8. # Licenced under the Public Domain or whatever.
  9.  
  10. # Temporary directory location.
  11. tempdir=".flickr-get.$$"
  12.  
  13. # Check for command-line arguments.
  14. if [ -z "$1" ]
  15. then
  16.     echo "Usage: $0 [flickr-url] [cookies-file]"
  17.     exit 1
  18. fi
  19.  
  20. # Check if [cookies-file] was specified. If it was, set $cookies to the
  21. # absolute path of the cookies file.
  22. if [ -n "$2" ]
  23. then
  24.     cd "`dirname "$2"`"
  25.     cookies="`pwd`/`basename "$2"`"
  26.     cd "$OLDPWD"
  27. fi
  28.  
  29. mkdir $tempdir
  30. cd $tempdir
  31.  
  32. # Download first page, check if it returns a photo-gallery. If not, retry
  33. # with the use of a cookies file (if specified).
  34. wget -q -E "$1/page1"
  35. grep photo_container page1.html &>/dev/null
  36. if [ $? -eq 1 ]
  37. then
  38.     if [ -e "$cookies" ]
  39.     then
  40.         wget_cookies="--load-cookies "$cookies""
  41.         rm page1.html
  42.         wget -q -E $wget_cookies "$1/page1"
  43.     else
  44.         echo -e "\033[1mDownloading this photostream requires a valid cookies.txt file.\033[0m"
  45.         rm -Rf $tempdir
  46.         exit 1
  47.     fi
  48. fi
  49.  
  50. # Find the last page in a photostream.
  51. lastpage=`grep -A 2 '<span class="break">...</span>' page1.html | tail -1 | cut -d">" -f2 | cut -d"<" -f1`
  52.  
  53. # Start downloading all pages up to the last one. Cookies will be used
  54. # if they were needed for the first page.
  55. echo -e "\033[1mDownloading temporary files...\033[0m"
  56. for i in `seq 2 $lastpage`
  57. do
  58.     wget -q -E $wget_cookies "$1/page$i"
  59. done
  60.  
  61. # Extract a list of photo URLs, removing the size specification (along
  62. # with the extension) and dumping the resulting list in 'flickr-photos'.
  63. grep -h photo_container *.html | cut -d'"' -f8 | sed 's/_m.jpg//' > flickr-photos
  64.  
  65. cd "$OLDPWD"
  66.  
  67. # Start downloading photos, first trying for original (or large) size
  68. # images and moving to medium size images if the originals are unavailable.
  69. echo -e "\033[1mDownloading photos...\033[0m"
  70. for i in `cat $tempdir/flickr-photos`
  71. do
  72.     wget -q -nc "${i}_b.jpg"
  73.     if [ -f "photo_unavailable.gif" ]
  74.     then
  75.         rm "photo_unavailable.gif"
  76.         wget -q -nc "${i}_z.jpg"
  77.     fi
  78. done
  79.  
  80. # Clean up and exit.
  81. rm -Rf $tempdir
  82. echo -e "\033[1mDone!\033[0m"
  83.  
  84. exit 0
Advertisement
Add Comment
Please, Sign In to add comment