Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active August 3, 2021 22:49
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edsu/94901f4a6454805f04fff6d9c10b0b8a to your computer and use it in GitHub Desktop.
Save edsu/94901f4a6454805f04fff6d9c10b0b8a to your computer and use it in GitHub Desktop.

Got Retweets?

Even though Twitter's statuses/retweets API endpoint is limited to the last 100 retweets it is possible to use the search/tweets endpoint to search for the retweets using the text of the tweet. Caveat: This is only possible for tweets that have happened in the last 7 days, which is furthest back Twitter allow you to search for tweets in.

For example here is how you can get the retweets for this tweet and analyze the users in a spreadsheet.

  1. First install twarc for interacting with the Twitter API from the command line. If you don't have Python yet you'll need to go get that first.

    pip install twarc

  2. Now you are ready to tell twarc about your Twitter API keys. Go over to apps.twitter.com, create an app and note the keys down so you can tell twarc about them:

    twarc configure

  3. Collect the tweets using twarc's search command using some identifying text of the tweet in question, then write them to a file:

    twarc search 'Bring on the bots and sock puppet accounts Amazing how a tweet about Putin' > krebs.jsonl

  4. If you want to be absolutely sure the file only includes retweets of that specific tweet you can use jq to filter them using the tweet id of the original tweet:

    jq -cr 'select(.retweeted_status.id_str = "902545914304319491")' krebs.jsonl > krebs-filtered.jsonl

  5. Now that you have the JSON for the retweets you can do analysis of the users by creating a CSV file of information about them. For example:

    jq -r '[.user.screen_name, .user.followers_count, .user.friends_count, .user.statuses_count, .user.created_at, .user.lang] | @csv' krebs-filtered.jsonl > users.csv

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