_api = \Codebird\Codebird::getInstance(); } protected function _getCachePath() { return '../cache/twitter.%s'; } protected function _fetchItems() { $content = $this->_api->statuses_userTimeline([ 'screen_name' => $this->_feed, 'count' => 200, 'exclude_replies' => TRUE ], TRUE); if (isset($content->rate)) { unset($content->rate); } if ($content->httpstatus !== 200) { $errorString = ''; if (isset($content->error)) { $errorString = $content->error; } if (isset($content->errors)) { $errorString = implode('\n', array_map( function($error) { return $error->message . ' (' . $error->code . ')'; }, $content->errors )); } throw new Exception($errorString); } unset($content->httpstatus); return $content; } protected function _spamFilter($items) { $db = new \PDO('sqlite:../spamlinks.db'); $spamQuery = $db->prepare('SELECT id FROM twitter WHERE username = :name'); $spamQuery->bindParam(':name', $user); $spamQuery->execute(); $spamContent = array_map( function($row) { return $row[0]; }, $spamQuery->fetchAll() ); $spamHashes = []; $filteredContent = []; foreach ($items as $c) { if (!in_array($c->id_str, $spamContent)) { $twitterURLs = FALSE; $urls = array_filter( array_map( function($url) { return $url->expanded_url; }, $c->entities->urls ), function($url) use(&$twitterURLs) { $urlParts = parse_url($url); if ($urlParts['host'] == 'twitter.com') { $twitterURLs = TRUE; return FALSE; } return TRUE; } ); if (!$urls) { if (!$twitterURLs) { $filteredContent[] = $c; } } else { sort($urls); $urlHash = md5(implode('|', $urls)); if (isset($filteredContent[$urlHash])) { $spamHashes[] = $c->id_str; } $filteredContent[$urlHash] = $c; } } } $content = $filteredContent; if ($spamHashes) { foreach ($spamHashes as $hash) { $insertQuery = $db->prepare( 'INSERT INTO twitter(id, username) VALUES (?, ?)' ); $insertQuery->bindParam(1, $hash); $insertQuery->bindParam(2, $user); $insertQuery->execute(); } } return $content; } protected function _mapItems($content) { $items = []; foreach ($content as $i) { $item = new Item(); $item->ID = $i->id_str; $item->Title = $i->text; $item->Link = sprintf('https://twitter.com/%s/status/%s', $this->_feed, $i->id_str); $item->Time = $i->created_at; $item->Text = $i->text; if (isset($i->user)) { $item->Author = $i->user->screen_name; } $items[] = $item; } return $items; } protected function _sortContent($content) { usort($content, function($c1, $c2) { return strcmp($c2->ID, $c1->ID); }); return $content; } public function title() { return sprintf("%s's timeline", $this->_feed); } } ?>