Last active
March 7, 2019 04:29
-
-
Save terrancewong/922f733538a37ce4d4afb08d628473fd to your computer and use it in GitHub Desktop.
TMD: Twitter Media Downloader. usage: tmd <twitter userID>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important!
GNU parallel, curl, wget and youtube-dl need to be installed before use.