Created
May 17, 2010 13:52
-
-
Save trobrock/403776 to your computer and use it in GitHub Desktop.
Will parse the Twitter firehose for a hash tag, and then emulate the process of an sms short code voting system using Twitter as the backend. Built overnight at the Twitter Chirp conference
This file contains hidden or 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
<?php | |
require('phirehose_0.2.4/lib/Phirehose.php'); | |
class SmsShortCode extends Phirehose { | |
/** | |
* Enqueue each status | |
* | |
* @param string $status | |
*/ | |
public function enqueueStatus($status) | |
{ | |
$data = json_decode($status, true); | |
if (is_array($data) && isset($data['user']['screen_name'])) { | |
$conn = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'username', 'password'); | |
mysql_select_db('smsshortcode'); | |
if (!$conn) { | |
throw new Exception('No database connection!'); | |
} | |
$result = mysql_query("INSERT INTO `data` (`user`, `tweet_text`) VALUES ('" . $data['user']['screen_name'] . "', '" . mysql_real_escape_string(urldecode($data['text'])) . "');"); | |
if (!$result) { | |
throw new Exception('Unable to query the database. ' . mysql_error()); | |
} | |
} | |
} | |
} | |
$shortcoder = new SmsShortCode('twitter-user', 'twitter-password', Phirehose::METHOD_FILTER); | |
$shortcoder->setTrack(array('#shortcode')); | |
$shortcoder->consume(); | |
?> | |
<?php | |
class SmsShortCodeProcess { | |
protected $checkInterval; | |
/** | |
* Construct the consumer and start processing | |
*/ | |
public function __construct($checkInterval = 10) | |
{ | |
$this->checkInterval = $checkInterval; | |
} | |
/** | |
* Method that actually starts the processing task (never returns). | |
*/ | |
public function process() { | |
// Init some things | |
$lastCheck = 0; | |
// Loop infinitely | |
while (TRUE) { | |
$lastCheck = time(); | |
// Iterate over each | |
$conn = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'username', 'password'); | |
mysql_select_db('smsshortcode'); | |
$result = mysql_query('SELECT * FROM `data`;'); | |
while ($row = mysql_fetch_assoc($result)) { | |
preg_match('/vote [0-9]+/', $row['tweet_text'], $matches); | |
$matches = explode(' ', $matches[0]); | |
$inserted = mysql_query('INSERT INTO `processed` (`user`, `tweet_text`, `vote_id`) VALUES (\'' . $row['user'] . '\', \'' . mysql_real_escape_string($row['tweet_text']) . '\', ' . $matches[count($matches) - 1] . ')'); | |
if (!$inserted) { | |
throw new Exception('Could not process tweet. ' . mysql_error()); | |
} | |
$deleted = mysql_query('DELETE FROM `data` WHERE `id` = ' . $row['id']); | |
if (!$deleted) { | |
throw new Exception('Could not delete the tweet. ' . mysql_error()); | |
} | |
} | |
// Wait until ready for next check | |
while (time() - $lastCheck < $this->checkInterval) { | |
sleep(1); | |
} | |
} // Infinite loop | |
} // End process() | |
} | |
$shortcoder = new SmsShortCodeProcess(); | |
$shortcoder->process(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment