Skip to content

Instantly share code, notes, and snippets.

@McFlat
Created November 12, 2020 21:49
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 McFlat/480bcb63841a25d5047a154d5d9c4929 to your computer and use it in GitHub Desktop.
Save McFlat/480bcb63841a25d5047a154d5d9c4929 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# this script downloads instagram posts and ig profile pictures
trap kill_it TERM # 15 - Termination signal
trap kill_it PIPE # 13 - Broken pipe: write to pipe with no
trap kill_it SEGV # 11 - Invalid memory reference
trap kill_it KILL # 9 - Kill signal
trap kill_it FPE # 8 - Floating point exception
trap kill_it ABRT # 6 - Abort signal from abort(3)
trap kill_it ILL # 4 - Illegal Instruction
trap kill_it QUIT # 3 - Quit from keyboard
trap kill_it INT # 2 - Interrupt from keyboard
trap kill_it HUP # 1 - Hangup detected on controlling terminal or death of controlling process
function kill_it() {
echo "Killed $@";
exit 1;
}
if [ "$0" == "-bash" ] || [ "$0" == "bash" ]; then
BASEDIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")";
else
BASEDIR="$(realpath "$(dirname "${0}")")";
fi
cd "${BASEDIR}";
IFS=$(echo -en "\n\b");
function get_picture_hash() {
picture_path="${1}";
md5hash="$(md5sum "${picture_path}")";
[ "$(echo "${md5hash}" | awk '{print $1}')" == 'MD5' ] && md5hash="$(echo "${md5hash}" | awk '{print $NF}')"; # hash on mac
md5hash="$(echo "${md5hash}" | awk '{print $1}')";
echo "${md5hash}";
}
function grab_pp(){ # ahaha
ig_user="$1"
pp_url=$(wget -q https://instagram.com/$ig_user/ -O - | grep "og:image" | sed 's|.*<meta property="og:image" content="||' | sed 's|".*||')
if [ "$pp_url" != "" ]; then
[ ! -d ./instagram/users/$ig_user ] && mkdir -p ./instagram/users/$ig_user;
pic_jpg="./instagram/users/$ig_user/profile.jpg"
wget -q "$pp_url" -O "${pic_jpg}"
if [ -f "${pic_jpg}" ] && [[ "$(file "${pic_jpg}")" != *"empty"* ]]; then
md5hash="$(get_picture_hash "${pic_jpg}")";
backup_pic_jpg="./instagram/users/$ig_user/profile-${md5hash}.jpg";
if [ ! -f "${backup_pic_jpg}" ]; then
cp "${pic_jpg}" "${backup_pic_jpg}";
fi
fi
fi
}
function grab_ig() {
ACTION="$1";
PARAM1="$2";
OPTIONS=${@:3};
if [ -d ./instagram ]; then
if [ "$ACTION" == "user" ]; then
[ ! -d ./instagram/users ] && mkdir -p ./instagram/users;
[ ! -f ./instagram/users.txt ] && touch ./instagram/users.txt;
[ "$(grep -- "$PARAM1" ./instagram/users.txt)" == "" ] && echo $PARAM1 >> ./instagram/users.txt;
instalooter user -v $PARAM1 ./instagram/users/$PARAM1 ${OPTIONS};
fi;
if [ "$ACTION" == "post" ]; then
[ ! -d ./instagram/posts ] && mkdir -p ./instagram/posts;
[ ! -f ./instagram/posts.txt ] && touch ./instagram/posts.txt;
[ "$(grep -- "$PARAM1" ./instagram/posts.txt)" == "" ] && echo $PARAM1 >> ./instagram/posts.txt;
instalooter post -v $PARAM1 ./instagram/posts/$PARAM1 ${OPTIONS};
fi;
if [ "$ACTION" == "hashtag" ]; then
[ ! -d ./instagram/hashtags ] && mkdir -p ./instagram/hashtags;
[ ! -f ./instagram/hashtags.txt ] && touch ./instagram/hashtags.txt;
[ "$(grep -- "$PARAM1" ./instagram/hashtags.txt)" == "" ] && echo $PARAM1 >> ./instagram/hashtags.txt;
instalooter hashtag -v $PARAM1 ./instagram/hashtags/$PARAM1 ${OPTIONS};
fi;
fi
}
if [ "$1" == "u" ] || [ "$1" == "user" ]; then
grab_pp $2
grab_ig user $2 ${@:3}
elif [ "$1" == "p" ] || [ "$1" == "post" ]; then
grab_ig post $2
elif [ "$1" == "h" ] || [ "$1" == "hashtag" ]; then
grab_ig hashtag $2
else
for u in $(ls -1 ./instagram/users/); do
grab_pp $u
grab_ig user $u ${@:3}
done
for h in $(ls -1 ./instagram/hashtags/); do
grab_ig hashtag $h
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment