diff options
author | emkael <emkael@tlen.pl> | 2017-01-16 17:32:19 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2017-01-16 17:32:47 +0100 |
commit | 68f5ac77ee0ca7788c7c48681ad2067de2af427b (patch) | |
tree | a9d99d54578c117787e76da2ab00b9b99bfc44ee /providers/Twitter.php | |
parent | 4ea787e34ddbfa62781fa6ea4b6fd4af39ef9ff0 (diff) |
* new structure, for multiple providers
Diffstat (limited to 'providers/Twitter.php')
-rw-r--r-- | providers/Twitter.php | 117 |
1 files changed, 117 insertions, 0 deletions
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; + } + +} + +?> |