Skip to content

Instantly share code, notes, and snippets.

@devonzuegel
Last active December 31, 2020 01:01
Show Gist options
  • Save devonzuegel/afaf87f11518e38908057a88e8afadd9 to your computer and use it in GitHub Desktop.
Save devonzuegel/afaf87f11518e38908057a88e8afadd9 to your computer and use it in GitHub Desktop.
Backup your Twitter data
#!/bin/sh
export DAY=`date +'%Y-%m-%d'`
user=devonzuegel
tweetsFile="$DAY--$user--tweets.csv"
retweetsFile="$DAY--$user--retweets.csv"
favoritesFile="$DAY--$user--favorites.csv"
followingsFile="$DAY--$user--followings.csv"
max=10000
echo "Backing up tweets..."; t timeline ${user} --csv --number ${max} > "${tweetsFile}"
echo "Backing up retweets..."; t retweets ${user} --csv --number ${max} > "${retweetsFile}"
echo "Backing up favorites..."; t favorites ${user} --csv --number ${max} > "${favoritesFile}"
echo "Backing up followings..."; t followings ${user} --csv > "${followingsFile}"
echo "\nBacked up the following for ${user}'s data:"
numTweetsWithLabel=$(csvstat --count ${tweetsFile})
numTweets="${numTweetsWithLabel#"Row count: "}"
numRetweetsWithLabel=$(csvstat --count ${retweetsFile})
numRetweets="${numRetweetsWithLabel#"Row count: "}"
numFavoritesWithLabel=$(csvstat --count ${favoritesFile})
numFavorites="${numFavoritesWithLabel#"Row count: "}"
numFollowingsWithLabel=$(csvstat --count ${followingsFile})
numFollowings="${numFollowingsWithLabel#"Row count: "}"
echo "- ${numTweets} tweets"
echo "- ${numRetweets} retweets"
echo "- ${numFavorites} favorites"
echo "- ${numFollowings} followings"
echo "\nDone 🎉\n"
gem install t # Assumes that you have Ruby installed already
sudo easy_install csvkit # More info: https://github.com/amandabee/CUNY-data-storytelling/wiki/Tutorial:-Installing-CSVKit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment