Created
October 2, 2017 01:26
-
-
Save drench/1794d3387535ee6c44ddbbe2b5c5a69d to your computer and use it in GitHub Desktop.
This file contains 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/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
Reference: https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-favorites-list