Skip to content

Instantly share code, notes, and snippets.

@drench
Created October 2, 2017 01:26
Show Gist options
  • Save drench/1794d3387535ee6c44ddbbe2b5c5a69d to your computer and use it in GitHub Desktop.
Save drench/1794d3387535ee6c44ddbbe2b5c5a69d to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# usage: tw-get-likes twitter_username
#
# Requires:
# * twurl
# * jq
#
# The API is rate-limited to 75 calls in a 15 minute window
# but this doesn't handle rate limit error returns. Who has 15k likes anyway?
max_id=-1
n=0
screen_name=$1
if [ -z "$screen_name" ]; then
echo "Screen name parameter missing"
exit 111
fi
# Start without specifying a max_id
# This will give is the most recent 200 likes
# Get the id (or id_str) of the oldest "like" in the list
# This is probably the last one in the json array
# but we could sort by ID to be sure then use '.[-1]'
# If the array is empty, we're done.
# verified with 'drench': no likes returns just '[]'
# Subtract 1 from this id and use it as the max_id value for the next call
while true; do
outfile=out/$screen_name-likes-$n.json
url="/1.1/favorites/list.json?screen_name=$screen_name&include_entities=false&count=200"
if [ $max_id -gt 0 ]; then
url="${url}&max_id=$max_id"
fi
echo "calling $url and sending output to $outfile"
twurl $url > $outfile
record_count=$(jq 'length' < $outfile)
if [ $record_count = "0" ]; then
echo "done"
rm $outfile
break
fi
max_id=$(jq '.[-1].id' < $outfile)
echo "max_id is now '$max_id'. Sleeping…"
((max_id--))
((n++))
sleep 10
done
if [ -e out/$screen_name-likes-0.json ]; then
cat out/$screen_name-likes-*.json |
jq -s add |
jq -S '.[] | "https://twitter.com/" + .user.screen_name + "/status/" + .id_str' > out/$screen_name-likes.json
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment