/** * Expand Bit.ly links and recheck click count on any links less than 2 days old. * * @param str bitly api key * @param str bitly login name */ public function acquireBitlyClickStats($api_key, $bit_login) { $this->logger->setUsername(null); $api_accessor = new BitlyAPIAccessor($api_key, $bit_login); $bitly_urls = array('http://bit.ly/', 'http://bitly.com/', 'http://j.mp/'); foreach ($bitly_urls as $bitly_url) { if ($this->link_limit != 0) { //all short links first seen in the last 48 hours $bitly_links_to_update = $this->short_link_dao->getLinksToUpdate($bitly_url); if (count($bitly_links_to_update) > 0) { $this->logger->logUserInfo(count($bitly_links_to_update) . " {$bitly_url}" . " links to acquire click stats for.", __METHOD__ . ',' . __LINE__); } else { $this->logger->logUserInfo("There are no " . $bitly_url . " links to fetch click stats for.", __METHOD__ . ',', __LINE__); } $total_links = 0; $total_errors = 0; $total_updated = 0; foreach ($bitly_links_to_update as $link) { $this->logger->logInfo("Getting bit.ly click stats for " . ($total_updated + 1) . " of " . count($bitly_links_to_update) . " " . $bitly_url . " links (" . $link->short_url . ")", __METHOD__ . ',' . __LINE__); $link_data = $api_accessor->getBitlyLinkData($link->short_url); if ($link_data["clicks"] != '') { //save click total here $this->short_link_dao->saveClickCount($link->short_url, $link_data["clicks"]); // Save title to links table if ($link_data["title"] != '') { $this->link_dao->updateTitle($link->link_id, $link_data["title"]); } $total_links = $total_links + 1; $total_updated = $total_updated + 1; } elseif ($link_data["error"] != '') { $this->link_dao->saveExpansionError($link->short_url, $link_data["error"]); $total_errors = $total_errors + 1; $total_updated = $total_updated + 1; } } $this->logger->logUserSuccess($total_links . " " . $bitly_url . " link click stats acquired (" . $total_errors . " errors)", __METHOD__ . ',' . __LINE__); } } }