From 68f5ac77ee0ca7788c7c48681ad2067de2af427b Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 16 Jan 2017 17:32:19 +0100 Subject: * new structure, for multiple providers --- providers/Twitter.php | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 providers/Twitter.php (limited to 'providers/Twitter.php') diff --git a/providers/Twitter.php b/providers/Twitter.php new file mode 100644 index 0000000..868bb28 --- /dev/null +++ b/providers/Twitter.php @@ -0,0 +1,117 @@ +_api = \Codebird\Codebird::getInstance(); + } + + protected function _getCachePath() { + return '../cache/twitter.%s.json'; + } + + 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; + } + } + } + usort($filteredContent, function($c1, $c2) { return strcmp($c1->id_str, $c2->id_str); }); + $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; + } + +} + +?> -- cgit v1.2.3