diff options
Diffstat (limited to 'providers')
-rw-r--r-- | providers/Provider.php | 54 | ||||
-rw-r--r-- | providers/Twitter.php | 117 |
2 files changed, 171 insertions, 0 deletions
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 @@ +<?php + +namespace Providers; + +abstract class Provider { + + protected $_options = []; + protected $_feed = NULL; + protected $_cacheTimeout = '15 minutes'; + protected $_cacheTime; + + public function __construct($feed, $options=[]) { + $this->_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 @@ +<?php + +namespace Providers; + +require_once('Provider.php'); +require_once('../lib/codebird-php/src/codebird.php'); + + +class Twitter extends \Providers\Provider { + + private $_api; + + public function __construct($feed, $options=[]) { + parent::__construct($feed, $options); + $config = json_decode(file_get_contents('../config/twitter.json'), TRUE); + \Codebird\Codebird::setConsumerKey($config['key'], $config['secret']); + \Codebird\Codebird::setBearerToken($config['token']); + $this->_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; + } + +} + +?> |