Skip to content

Instantly share code, notes, and snippets.

@drench
Last active May 20, 2017 21:30
Show Gist options
  • Save drench/f068ac13816bca51369e3fcb1d1b4177 to your computer and use it in GitHub Desktop.
Save drench/f068ac13816bca51369e3fcb1d1b4177 to your computer and use it in GitHub Desktop.
A shell script to fetch your twitter followers and followings. Requires https://github.com/twitter/twurl and https://github.com/stedolan/jq
#!/bin/sh
case "$1" in
followers)
api_endpoint="/1.1/followers/list.json"
output_prefix="followers"
;;
following)
api_endpoint="/1.1/friends/list.json"
output_prefix="following"
;;
*)
echo "Specify either 'followers' or 'following'"
exit 111
;;
esac
cursor=-1
n=0
while true; do
outfile="${output_prefix}-${n}.json"
url="${api_endpoint}?skip_status=true&count=200&cursor=${cursor}"
echo "calling $url and sending output to $outfile"
twurl $url > $outfile
error_code=$(jq '.errors[0].code' < $outfile)
if [ "$error_code" == "88" ]; then
echo "Rate limited. Let's wait a minute."
sleep 60
rm $outfile
continue
else
cursor=$(jq '.next_cursor' < $outfile)
fi
if [ $cursor == "0" ]; then
echo "done"
break
fi
sleep 2
((n++))
done
@drench
Copy link
Author

drench commented May 20, 2017

To combine the JSON files and sort the result by twitter user id:

cat following-*.json | jq -S '.users' | jq -s add | jq 'sort_by(.id)'

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