I have a CakePHP function here that gets info for a twitter screen_name from the twitter api and writes it to a mysql db. I'm just looking for feedback about other ways to accomplish this that might be more efficient with fewer loops maybe. The challenge is, there are many screen_names that need to be processed for a $reportid before the "report" is considered done. Any feedback is very much appreciated. thanks.
function admin_wtfTwitter($reportid, $jobid) {
$this->autoRender = false;
//use if the api lags
//set_time_limit(18000);
//notify the log
$this->admin_saveLog($reportid, $jobid, 'parsecrawlresults', 'Twitter wce started.');
//get the twitter channels from reportdetails
$channels = $this->Reportdetail->getChannel($reportid, 4); //channel 4 = twitter
foreach ($channels as $c) {
//open reportdetail channel for writting
$this->Reportdetail->create();
$this->Reportdetail->set(array('id' => $c['Reportdetail']['id']));
/*
* small_tag is null --> continue to next reportdetail record
* small_tag is bad
* small_tag is good but no data returned
*/
if ($c['Reportdetail']['small_tag'] == null) { $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag null')); $this->Reportdetail->save(); continue; }
$uri = 'http://api.twitter.com/1/users/show.json?screen_name=' . $c['Reportdetail']['small_tag'];
$data = @file_get_contents($uri);
if($data === false) { unset($data); $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag bad')); $this->Reportdetail->save(); continue; }
$content = json_decode($data, true);
//set fans, title username
$this->Reportdetail->set(array(
'fans' => $content['followers_count'],
'title' => $content['description'],
'username' => strtolower($content['screen_name'])
));
//proceed with second api call****************************
unset($uri);
unset($data);
unset($content);
$i = 0;
$e = 0;
$postmonths = array();
//LIMIT SET TO 1000 - CAN BE 3200 - https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
$uri = 'https://api.twitter.com/1/statuses/user_timeline.json?trim_user=true&exclude_replied=true&include_entities=true&include_rts=true&contributor_details=false&count=1000&screen_name=' . $c['Reportdetail']['small_tag'];
$data = @file_get_contents($uri);
if($data === false) { unset($data); $this->Reportdetail->set(array('done' => -1, 'note' => 'small_tag bad')); $this->Reportdetail->save(); continue; }
$content = json_decode($data, true);
//set engagement, posts
foreach ($content as $t) {
if (!empty($t['id_str'])) {
$i++;
$e = $e + $t['retweet_count'];
array_push($postmonths, substr($t['created_at'], 4, 3)); //API resutls are: "created_at":"Fri Nov 30 00:38:34 +0000 2012
}
}
$this->Reportdetail->set(array(
'posts' => $i,
'engagements' => $e
));
//get and set frequency
$freq = $this->admin_calcFrequency($i, count(array_unique($postmonths)));
$this->Reportdetail->set(array('frequency' => $freq));
//********************************************************
//we're done with this one
$this->Reportdetail->set(array('done' => 1));
//commit the content to reportdetail record
$this->Reportdetail->save();
}
//notify the log
$this->admin_saveLog($reportid, $jobid, 'parsecrawlresults', 'Twitter wce completed.');
}