Skip to content

Instantly share code, notes, and snippets.

@terrancewong
Last active March 7, 2019 04:29
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 terrancewong/922f733538a37ce4d4afb08d628473fd to your computer and use it in GitHub Desktop.
Save terrancewong/922f733538a37ce4d4afb08d628473fd to your computer and use it in GitHub Desktop.
TMD: Twitter Media Downloader. usage: tmd <twitter userID>
#!/bin/bash
t=`mktemp`
t2=`mktemp`
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
echo "** Trapped CTRL-C"
popd
exit 3
}
function dv() {
if [ $# -eq 2 ] ; then
echo fetching $1 for max-position $2
curl -s "https://twitter.com/${1}/media?max_position=${2}" | sed -e 's/\\n/\n/g' -e 's/\\//g' > ${t}
elif [ $# -eq 1 ] ; then
curl -s "https://twitter.com/${1}/media" | sed -e 's/\\n/\n/g' -e 's/\\//g' > ${t}
fi
mapfile -t links < <(cat ${t} | grep -e "data-tweet-id" | cut -d '"' -f 2)
echo Links: ${links[@]}
for i in "${links[@]}"
do
URL="https://twitter.com/${1}/status/$i"
rm -f ${t2} 2>/dev/null
wget -q -O ${t2} ${URL}
if cat ${t2} | grep '"og:image"' > /dev/null ; then
#if cat ${t2} | grep '"og:image"' ; then
if cat ${t2} | grep '"og:image"' | grep "ext_tw_video_thumb" > /dev/null ; then
echo "Downloading video"
youtube-dl --ignore-config -w --no-part -i "https://twitter.com/${1}/status/$i"
else
echo "Downloading Images"
cat ${t2} | grep '"og:image"' | awk '/content/ {f=NR} f&&NR-1==f' RS="=" | cut -d '"' -f 2 | grep '\.jpg:' | sed -e 's/:large//g' | parallel -n 1 -j 4 'if [ ! -f {/} ] ; then echo "get {/}" ; wget -qO {/} "{}:orig" ; else echo "skip {/}" ; fi'
fi
fi
rm -f ${t2} 2>/dev/null
done
}
mkdir -p $1
pushd $1
dv $1
min=$(cat ${t} | grep "min-position" | cut -d '"' -f 4)
until [[ "$min" == "" ]] ; do
dv $1 $min
min=$(cat ${t} | grep "min-position" | cut -d '"' -f 4)
done
@terrancewong
Copy link
Author

Important!
GNU parallel, curl, wget and youtube-dl need to be installed before use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment