Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hellogerard committed Apr 14, 2010
1 parent 15d13a4 commit 40e9203
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License

Copyright (c) 2010 Gerard Sychay - http://straylightrun.net - hellogerard@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
@@ -0,0 +1,25 @@
Twitter Roll
============


A small PHP script that displays the members of a public Twitter list with their
picture, bio, and last tweet.

Inspired by http://theskiff.org/crew/.

This code includes the TwitterOAuth library (version 0.1)
<http://github.com/abraham/twitteroauth> for PHP written by Abraham Williams.


Setup
=====

1. Create a Twitter app at http://twitter.com/apps. Record the app consumer
tokens.
2. Authorize the app for your (or someone's) Twitter account. Record the
resulting OAuth tokens.
3. Create a Twitter list.
4. Configure the script with your data.


_see LICENSE for copyright and license info_
129 changes: 129 additions & 0 deletions twitter_roll.php
@@ -0,0 +1,129 @@
<?php


require('OAuth.php');
require('TwitterOAuth.php');


class TwitterListMembers
{
const CONSUMER_KEY = ''; // your Twitter app consumer key
const CONSUMER_SECRET = ''; // you Twitter app consumer secret
const OAUTH_TOKEN = ''; // your Twitter OAuth token for this app
const OAUTH_SECRET = ''; // your Twitter OAuth secret for this app
const TWITTER_LIST = 'yourtwittername/yourtwitterlist'; // Twitter list to display

public function get($list)
{
// Unfortunately, the GET LIST MEMBERS API requires authentication for
// some reason, even when the same data is available on the website. So
// this requires hard-coding (or otherwise storing) a user's OAuth
// credentials in here. Be sure that the application has read-only
// permissions to reduce the chance of tomfoolery.

$oauth = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET, self::OAUTH_TOKEN, self::OAUTH_SECRET);
$response = $oauth->OAuthRequest("http://api.twitter.com/1/$list/members.json", array(), 'GET');
$members = json_decode($response);

usort($members->users, array(self, 'lastUpdated'));
return $members->users;
}

public static function lastUpdated($member1, $member2)
{
$lastStatus1 = strtotime($member1->status->created_at);
$lastStatus2 = strtotime($member2->status->created_at);

return ($lastStatus1 > $lastStatus2) ? -1 : 1;
}

public static function howLongAgo($then)
{
$delta = time() - $then;

if ($delta < 60)
{
$howlong = 'less than a minute ago';
}
else if ($delta < 120)
{
$howlong = 'about a minute ago';
}
else if ($delta < (60 * 60))
{
$howlong = round((float)($delta / 60.0)) . ' minutes ago';
}
else if ($delta < (120 * 60))
{
$howlong = 'about an hour ago';
}
else if ($delta < (24 * 60 * 60))
{
$howlong = 'about ' . round((float) ($delta / 3600.0)) . ' hours ago';
}
else if ($delta < (48 * 60 * 60))
{
$howlong = '1 day ago';
}
else
{
$howlong = round((float) ($delta / 86400.0)) . ' days ago';
}

return $howlong;
}

public function render($members)
{
foreach ((array) $members as $member)
{
$howLongAgo = self::howLongAgo(strtotime($member->status->created_at));

echo <<<EOF
<div id="twitter_roll" style="font-family: Arial; font-size: 8pt; width: 50%;">
<h3>{$member->name} ({$member->screen_name})</h3>
<img src="{$member->profile_image_url}" style="float: left; margin-right: 10px;">
<p>{$member->description}</p>
EOF;

$status = "<span> {$member->status->text} </span> <a style=\"font-size: 85%;\" href=\"http://twitter.com/{$member->screen_name}/statuses/{$member->status->id}\"> $howLongAgo </a>";
if ($member->protected)
{
$status = "<em> This person has protected their tweets. </em>";
}

echo <<<EOF
<ul style="margin-left: 50px;">
<li> $status </li>
</ul>
<p> <a href="http://twitter.com/{$member->screen_name}" rel="me"> Follow {$member->screen_name} on Twitter</a> </p>
</div>
EOF;
}
}
}

?>

<html>
<body>
<h2>Welcome to the Twitter roll!</h2>

<p>
<?php
$list = new TwitterListMembers();

try
{
$members = $list->get(TwitterListMembers::TWITTER_LIST);
$list->render($members);
}
catch (Exception $e)
{
echo "<pre>Twitter API error. No Twitter roll for you!</pre>";
}
?>
</p>

</body>
</html>

0 comments on commit 40e9203

Please sign in to comment.