summaryrefslogtreecommitdiff
path: root/providers/Twitter.php
blob: 868bb284e9b96f1ecf4c6960374ee1f8ea2ebb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
    }

}

?>