Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make a crontable tweet handler since the stream is giving me issues
- Loading branch information
dubhunter
committed
May 5, 2013
1 parent
340e043
commit 890438d
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |