CCCPaste

Instagram Crawler

#!/bin/bash

dir=`pwd`
list="$dir/instacreep.txt";

getFile(){
  prompt="Please select a file:"
  options=( $(find -maxdepth 1 -print0 | xargs -0) )

  PS3="$prompt "
  select opt in "${options[@]}" "Quit" ; do 
      if (( REPLY == 1 + ${#options[@]} )) ; then
          exit

      elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
          echo  "You picked `basename $opt` which is file $REPLY"
          break

      else
          echo "Invalid option. Try another one."
      fi
  done    
  list="`pwd`/`basename $opt`"
  echo $list
}

if [ ! -f "$list" ]
then
  list="./"
  while [ 1 ]
  do
    getFile
    [[ -d "$list" ]] && cd "$list"
    [[ -f "$list" ]] && break 
  done
fi

if [ -z "$1" ]
then
  echo "Where do you want to save images to? [Enter for current dir]"
  read savepath
else
  savepath="$1"
fi

if [ -z "$savepath" ]
then
  echo "Saving to current dir"
fi

creep(){
  username="$1"
  if cd "$savepath"
  then
     if [[ -d "${username}" ]]
     then
        echo "${username} exists, crawling."
     else
        mkdir "${username}"
     fi
     if cd "${username}"
     then
        mapfile -t links < <(wget --quiet -O - "https://www.instagram.com/${username}/media" | sed -e 's/standard/\n/g' | sed -e 's/_resolution":{"url":"//g'| grep -v status | sed -e 's/".*//g' -e 's/\\//g')
        maxid=$(wget --quiet -O - "https://www.instagram.com/${username}/media" | sed -e 's/standard/\n/g' | grep -e "\"image\",\"id\"" | sed -e 's/.*\"image\",\"id\"\:\"//g' -e 's/\".*//g' | tail -n 1)
        for i in "${links[@]}"
        do
              wget -nc "$i"
        done
        until [[ "$maxid" == "" ]]
        do
           mapfile -t links < <(wget --quiet -O - "https://www.instagram.com/${username}/media/?max_id=${maxid}" | sed -e 's/standard/\n/g' | sed -e 's/_resolution":{"url":"//g'| grep -v status | sed -e 's/".*//g' -e 's/\\//g')
           maxid=$(wget --quiet -O - "https://www.instagram.com/${username}/media/?max_id=${maxid}" | sed -e 's/standard/\n/g' | grep -e "\"image\",\"id\"" | sed -e 's/.*\"image\",\"id\"\:\"//g' -e 's/\".*//g' | tail -n 1)
           for i in "${links[@]}"
           do
                 wget -nc "$i"
           done
           sleep 1
        done
     fi
  else
     echo "INSTACREEP FAILED"
  fi
}
if cd "$savepath"
then
   mapfile -t usernames < <(cat "$list")
   for i in "${usernames[@]}"
   do
      creep "$i"
   done
else
   echo "COULD NOT ENTER DIRECTORY"
fi