How to make your own @trumps_feed

Philip Bump
4 min readAug 19, 2017

--

On Thursday, I released a little tool that I’d made to gather together the Twitter accounts followed by the president so that people could see what he sees when he opens the app.

It was popular.

In the wake of that, a number of people have asked how to duplicate the tool for other users. So, here’s how.

What you need.

There are a lot of ways to do this, but what I’m going to explain is the method I used. You could, for example, make a Twitter list, but that 1) doesn’t allow others to follow as easily and 2) requires maintenance that this tool doesn’t.

For my system, you need:

  1. A server that can run PHP scripts.
  2. The TwitterOAuth library, available on Github.
  3. An ability to run cron jobs.

If you don’t know what those things mean, that’s fine! But you probably won’t be able to set this thing up.

1. Create a Twitter account.

The first thing you need to do is to create the Twitter account that will replicate the user’s feed. In my case, this was the @trumps_feed account. If you don’t know how to set up a Twitter account, well, again, maybe set this project aside.

Once the account has been created, you need to create a new app. Essentially, you’re telling Twitter that you’re using their application program interface (API) to do something. Name it whatever you want. Don’t worry about most of the details like callback URLs. Since this is something that’s only going to be used by you for this one purpose, you don’t need to worry too much about user friendliness.

What you need to get from this system are the keys and access tokens. You’ll need four character strings for the script to work: the consumer key, the consumer secret, the access token and the access token secret.

The first two are automatically generated when you create your app. The second two require you to click a button. Go to the Keys and Access Tokens tab, where you’ll see the consumer keys.

Then, scroll down. There’s that button to click.

That’s it. Off to PHP.

2. Set up the script.

On your server, set up a folder that includes the TwitterOAuth library and your script. Let’s say the script is called feed.php.

It should include this code:

<?php//////////////////////////////////
// for error reporting, if desired
error_reporting(E_ALL);///////////////////
// load the library
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//////////////////
// define the keys
// Replace [KEY] with your keys/tokens from Twitter.
// (Keep the quote marks.)
define('CONSUMER_KEY', '[KEY]');
define('CONSUMER_SECRET', '[KEY]');
define('ACCESS_TOKEN', '[KEY]');
define('ACCESS_TOKEN_SECRET', '[KEY]');
//////////////////////////
// set up the oauth object
$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$profiles = array();
/////////////////////////////////////////////
// figure out who your target account follows
// Replace [WHO] with the username of the person whose timeline
// you're replicating. In my case, it was realdonaldtrump
$ids = $toa->get('friends/ids', array('screen_name' => '[WHO]'));///////////////////////
// check for new tweets
// This runs through $ids -- the people your target account follows
// and checks for new tweets.
foreach ($ids->ids as $id) {
$tweets = $toa->get('statuses/user_timeline', array('user_id' => $id, 'exclude_replies' => 'true', 'count' => 20));

foreach ($tweets as $t) {
$made = strtotime($t->{"created_at"});
$now = strtotime("now");

$diff = $now - $made;

// The 300 below is important. It's how new a tweet should be,
// in seconds, to be retweeted.
// In my case, the script runs every five minutes,
// so only tweets sent in those most recent 300 seconds
// are retweeted.

if ($diff <= 300) {
$rt = $toa->post("statuses/retweet/" . $t->id);

//Un-comment the line below to see successful RTs
//var_dump($rt);

}
}
}

?>

That’s really it.

3. Set up the cron job.

The method for doing this will vary, but, essentially, you want to run the feed.php script on a regular interval. In my case, it’s every five minutes (which is why the script above only retweets tweets added in the last 300 seconds).

Trump only follows 45 people (as of writing), so it’s not that tricky to run the script fairly often. If the person who’s timeline you want to replicate follows more, it my be tricky.

And that’s it. You have your very own window into what some other Twitter user sees.

For better or for worse.

--

--