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/Provider.php | 54 +++++++++++++++++++++++ providers/Twitter.php | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 providers/Provider.php create mode 100644 providers/Twitter.php (limited to 'providers') diff --git a/providers/Provider.php b/providers/Provider.php new file mode 100644 index 0000000..0c3f344 --- /dev/null +++ b/providers/Provider.php @@ -0,0 +1,54 @@ +_feed = $feed; + $this->_options = $options; + } + + abstract protected function _getCachePath(); + + protected function _getCache($path) { + return file_get_contents($path); + } + + abstract protected function _fetchItems(); + + abstract protected function _spamFilter($items); + + protected function _getItems() { + $cacheFile = sprintf($this->_getCachePath(), $this->_feed); + $this->_cacheTime = file_exists($cacheFile) ? filemtime($cacheFile) : 0; + if ($this->_cacheTime > strtotime('-' . $this->_cacheTimeout)) { + return json_decode($this->_getCache($cacheFile)); + } else { + $content = $this->_fetchItems(); + file_put_contents($cacheFile, json_encode($content)); + $this->_cacheTime = time(); + return $content; + } + } + + public function get() { + $items = $this->_getItems(); + if (isset($this->_options['spamfilter'])) { + $items = $this->_spamFilter($items); + } + return $items; + } + + public function cacheTime() { + return $this->_cacheTime; + } + +} + +?> 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