Source of file SyncTwitter.php
Size: 7,026 Bytes - Last Modified: 2022-01-13T10:01:15+00:00
/var/www/docs.ssmods.com/process/src/code/BuildTasks/SyncTwitter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 | <?php /** * @todo need reconcile removals in both directions */ class SyncTwitter extends BuildTask implements CronTask{ protected static $conf_instance; protected static $tmh_oauth_instance; protected $conf; protected $tmhOAuth; protected $errors = array(); protected $messages = array(); public function __construct() { $this->conf = $this->getConf(); $this->tmhOAuth = $this->getTmhOauth(); parent::__construct(); } public function getSchedule() { return "*/5 * * * *"; } public function getConf() { if (!static::$conf_instance) static::$conf_instance = SiteConfig::current_site_config(); return static::$conf_instance; } public function getTmhOauth() { if (!$this->conf) $this->conf = $this->getConf(); if (!static::$tmh_oauth_instance) { static::$tmh_oauth_instance = new tmhOAuth(array( 'consumer_key' => $this->conf->TwitterConsumerKey, 'consumer_secret' => $this->conf->TwitterConsumerSecret, )); static::$tmh_oauth_instance->config['user_token'] = $this->conf->TwitterOAuthToken; static::$tmh_oauth_instance->config['user_secret'] = $this->conf->TwitterOAuthSecret; } return static::$tmh_oauth_instance; } public function init() { if (method_exists(parent,'init')) parent::init(); if (!Director::is_cli() && !Permission::check("ADMIN") && $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']) { return Security::permissionFailure(); } if (!$this->conf || !$this->tmhOAuth) $this->__construct(); } public function process() { $this->init(); $this->run(); } public function run($request = null) { // output echo "<br />\n<br />\nSyncing...<br />\n<br />\n"; flush(); @ob_flush(); if (!$this->conf->TwitterPullUpdates) { echo "Sync disabled <br />\n<br />\n"; return; } // grab the most recent tweet $params = array(); if ($lastTweet = DataObject::get_one('Tweet','','','TweetID DESC')) { if ($lastTweet->TweetID) $params['since_id'] = $lastTweet->TweetID; } // set the number of hits $params['count'] = 200; // if there was no last tweet we need to go into initial population $initPop = $lastTweet ? false : true ; // get tweets $code = $this->tmhOAuth->request( 'GET', $this->tmhOAuth->url('1.1/statuses/user_timeline'), $params ); // only proceed if the request was valid if ($code == 200) { // decode the response $resp = json_decode($this->tmhOAuth->response['response']); // only proceed if we have results to work with if (count($resp)) { // process the response $this->processResponse($resp); // check if we need to do an initial population if ($initPop) { //output echo "<br />\n<br />\nDoing initial Population<br />\n<br />\n"; flush(); @ob_flush(); // keep going until we hit a problem while ($code == 200 && count($resp)) { // find the earliest tweet we have in the db $firstTweet = DataObject::get_one('Tweet','','','TweetID ASC'); // reconfigure the params unset($params['since_id']); $params['max_id'] = $firstTweet->TweetID; // get tweets $code = $this->tmhOAuth->request( 'GET', $this->tmhOAuth->url('1.1/statuses/user_timeline'), $params ); // only proceed if the request was valid if ($code == 200) { // decode the response $resp = json_decode($this->tmhOAuth->response['response']); // only proceed if we have results to work with if (count($resp)) { // process the response $noNew = $this->processResponse($resp); // break if we haven't added anything if ($noNew) break; } } } // output echo "Finished"; flush(); @ob_flush(); } } else { // output echo "No hits <br />\n<br />\n"; flush(); @ob_flush(); } } else { die($code." : ".$this->tmhOAuth->response['response']); } } public function processResponse(array $resp) { $noNew = true; foreach ($resp as $tweetData) { if (!$savedTweet = DataObject::get_one('Tweet',"TweetID='".$tweetData->id_str."'")) { if (!$pubTweet = DataObject::get_one('PublicationTweet',"TweetID='".$tweetData->id_str."'")) { // push output echo "Adding Tweet ".$tweetData->id_str."<br />\n"; flush(); @ob_flush(); // create the Tweet Page $tweet = new Tweet; $tweet->updateFromTweet($tweetData); if ($tweet->write() && $tweet->doRestoreToStage() && $tweet->doPublish()) { echo 'Successfully created' . $tweet->Title ."<br />\n"; } else { die('Failed to Publish ' . $tweet->Title); } // set no new flag $noNew = false; } else { // push output echo "Tweet ".$tweetData->id_str." came from the website<br />\n"; flush(); @ob_flush(); } } else { // this should only happen during initial population because we should have only got in tweets that are newer than x // push output echo "Already added Tweet ".$tweetData->id_str."<br />\n"; flush(); @ob_flush(); } } return $noNew; } } |