public function processQueue($queueUrls) { try { foreach ($queueUrls as $url) { $twitter = new SSTwitter(); $reply = $twitter->search($url); if ($reply->errors) { $this->errorQueue[] = $url; continue; } $metadata = $reply->statuses; $count = intval($metadata[0]->user->statuses_count); $id = $entry['ID']; $statistic = URLStatistics::get()->filter(array('URL' => $url, 'Service' => $this->service, 'Action' => $this->statistic))->first(); if (!$statistic || !$statistic->exists()) { $statistic = URLStatistics::create(); $statistic->URL = $url; $statistic->Service = $this->service; $statistic->Action = $this->statistic; } $statistic->Count = $count; $statistic->write(); } } catch (Exception $e) { return 0; } return $this->errorQueue; }
function processQueue($queueUrls) { try { foreach ($queueUrls as $url) { $fileData = file_get_contents($this->getLinkedInCall($url)); if ($fileData === FALSE) { $this->errorQueue[] = $url; continue; } $output = str_replace(array('IN.Tags.Share.handleCount(', ');'), '', trim($fileData)); if ($output !== FALSE) { $json = json_decode($output); unset($fileData); // free memory $count = intval($json->count); $statistic = URLStatistics::get()->filter(array('URL' => $url, 'Service' => $this->service, 'Action' => $this->statistic))->first(); if (!$statistic || !$statistic->exists()) { $statistic = URLStatistics::create(); $statistic->URL = $url; $statistic->Service = $this->service; $statistic->Action = $this->statistic; } $statistic->Count = $count; $statistic->write(); } } } catch (Exception $e) { return 0; } return $this->errorQueue; }
public function countsFor() { $urls = explode(',', $this->request->getVar('urls')); // queue all urls to be checked foreach ($urls as $url) { $result = SocialQueue::queueURL($url); } $urlObjs = URLStatistics::get()->filter(array('URL' => $urls)); if (!$urlObjs->count()) { return json_encode(array()); } $results = array(); // do we need to filter the results any further $service = $this->getRequest()->param('SERVICE'); $filter = null; if ($service && $service == 'service') { $filter = $this->getRequest()->param('FILTER'); } foreach ($urlObjs as $urlObj) { if ($filter) { if (strtoupper($urlObj->Service) == strtoupper($filter)) { $results['results'][$urlObj->URL][$urlObj->Service][] = array($urlObj->Action => $urlObj->Count); } } else { $results['results'][$urlObj->URL][$urlObj->Service][] = array($urlObj->Action => $urlObj->Count); } } return json_encode($results); }
public function testTwitterStat() { $stat = URLStatistics::get()->filter(array('Service' => 'Twitter'))->first(); $this->assertEquals($stat->Service, 'Twitter'); $this->assertEquals($stat->Action, 'statuses_count'); $this->assertEquals($stat->Count, 50); $this->assertEquals($stat->URL, $this->testURL); }
public function tearDown() { // remove db records otherwise we end up with multiple stats foreach (URLStatistics::get() as $stat) { $stat->delete(); } foreach (SocialQueue::get() as $queue) { $queue->delete(); } parent::tearDown(); }
function processQueue($queueUrls) { try { foreach ($queueUrls as $url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc"); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json')); $curl_results = curl_exec($curl); if (curl_errno($curl)) { $this->errorQueue[] = $url; continue; } curl_close($curl); $json = json_decode($curl_results, true); if (isset($json['error'])) { $this->errorQueue[] = $url; continue; } $count = intval($json[0]['result']['metadata']['globalCounts']['count']); $statistic = URLStatistics::get()->filter(array('URL' => $url, 'Service' => $this->service, 'Action' => $this->statistic))->first(); if (!$statistic || !$statistic->exists()) { $statistic = URLStatistics::create(); $statistic->URL = $url; $statistic->Service = $this->service; $statistic->Action = $this->statistic; } $statistic->Count = $count; $statistic->write(); } } catch (Exception $e) { return 0; } return $this->errorQueue; }
public function processQueue($queueUrls) { $urls = array(); $noEntries = count($queueUrls); $i = 0; $step = 0; try { foreach ($queueUrls as $url) { $i++; $step++; $urls[] = $url; if ($i == $this->requestCount || $step == $noEntries) { $fileData = file_get_contents($this->getFacebookCall($urls)); if ($fileData === false) { foreach ($urls as $errorUrl) { $this->errorQueue[] = $url; } } else { $xml = simplexml_load_string($fileData); if ($xml->error_code || !$xml->link_stat) { foreach ($urls as $errorUrl) { $this->errorQueue[] = $url; continue; } } $results = $xml->link_stat; $ids = array_flip($urls); foreach ($results as $result) { $resultUrl = (string) $result->url; $statistics = URLStatistics::get()->filter(array('URL' => $resultUrl, 'Service' => $this->service, 'Action' => $this->statistic)); if ($statistics && $statistics->exists()) { foreach ($statistics as $statistic) { $statistic->Count = (int) $result->{$statistic->Action}; $statistic->write(); } } else { foreach ($this->statistic as $countStat) { $statistic = URLStatistics::create(); $statistic->URL = $resultUrl; $statistic->Service = $this->service; $statistic->Action = $countStat; $statistic->Count = (int) $result->{$statistic->Action}; $statistic->write(); } } // sanity check what should be in the db and what is actually in it if (count($this->statistic) != $statistics->count()) { foreach ($this->statistic as $statistic) { if ($result->{$statistic}) { // do we have this in the db $stat = URLStatistics::get()->filter(array('URL' => $resultUrl, 'Service' => $this->service, 'Action' => $statistic))->first(); if (!$stat || !$stat->exists()) { $stat = URLStatistics::create(); $stat->URL = $resultUrl; $stat->Service = $this->service; $stat->Action = $statistic; $stat->Count = (int) $result->{$statistic}; $stat->write(); } } } } } } unset($fileData); // free memory $i = 0; $urls = array(); } } } catch (Exception $e) { return 0; } return $this->errorQueue; }