Skip to content

Commit

Permalink
make a crontable tweet handler since the stream is giving me issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dubhunter committed May 5, 2013
1 parent 340e043 commit 890438d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions dbdApp/controllers/Sms.php
Expand Up @@ -7,6 +7,7 @@ protected function init() {

public function doDefault() {
dbdLog($this->getParams());
LastAction::logSms();
self::getNotifyrClient()->publish(self::NOTIFYR_CHANNEL, $this->getParam('Body'));
}
}
5 changes: 3 additions & 2 deletions dbdApp/controllers/TweetStream.php
Expand Up @@ -37,11 +37,12 @@ public function doDefault() {
// fclose($filePointer);

//Now, do something forever.
$method = 'https://stream.twitter.com/1/statuses/filter.json';
$method = 'https://stream.twitter.com/1.1/statuses/filter.json';

$params = array(
'track' => 'twilio,hyduino',
'follow' => '1780784149',
// 'follow' => '138862289',
'follow' => '1118284267',
);

self::getTwitterClient()->streaming_request('POST', $method, $params, function ($data, $length, $metrics) {
Expand Down
29 changes: 29 additions & 0 deletions dbdApp/controllers/Tweets.php
@@ -0,0 +1,29 @@
<?php
class Tweets extends TSController {

const SMS_TIMEOUT = 60;

protected function init() {
$this->noRender();
}

public function doDefault() {
if (time() - strtotime(LastAction::getLastSms()) > self::SMS_TIMEOUT) {
$response = Purl::get('http://search.twitter.com/search.json', array(
'params' => array(
'q' => 'twilio',
'rpp' => '1',
'lang' => 'en',
'since_id' => LastAction::getLastTweet(),
),
));

$data = json_decode($response->text, true);

if (count($data['results']) > 0) {
LastAction::logTweet($data['results'][0]['id']);
self::getNotifyrClient()->publish(self::NOTIFYR_CHANNEL, $data['results'][0]['from_user'] . ': ' .$data['results'][0]['text']);
}
}
}
}
47 changes: 47 additions & 0 deletions dbdApp/models/LastAction.php
@@ -0,0 +1,47 @@
<?php
class LastAction {
const TABLE_NAME = 'last_actions';
/**
* @var dbdDB
*/
protected static $db = null;
/**
* @var LastAction
*/
protected static $instance = null;
protected $last_sms = 0;
protected $last_tweet = 0;

protected function __construct() {
self::$db = dbdDB::getInstance();
$sql = "select * from `" . self::TABLE_NAME . "`";
list($this->last_sms, $this->last_tweet) = self::$db->prepExec($sql)->fetch();
}

protected static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}

public static function getLastSms() {
return self::getInstance()->last_sms;
}

public static function getLastTweet() {
return self::getInstance()->last_tweet;
}

public static function logSms() {
self::$db = dbdDB::getInstance();
$sql = "update `" . self::TABLE_NAME . "` set last_sms = ?";
self::$db->prepExec($sql, array(dbdDB::time()))->execute();
}

public static function logTweet($id) {
self::$db = dbdDB::getInstance();
$sql = "update `" . self::TABLE_NAME . "` set last_tweet = ?";
self::$db->prepExec($sql, array($id))->execute();
}
}

0 comments on commit 890438d

Please sign in to comment.