summaryrefslogtreecommitdiff
path: root/providers/Twitter.php
blob: 9a98364135d576d5755e3635cc97c30d3e69bdbc (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

namespace Providers;

require_once('Provider.php');
require_once('Item.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';
    }

    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;
              }
          }
      }
      $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;
    }

    protected function _mapItems($content) {
        $items = [];
        foreach ($content as $i) {
            $item = new Item();
            $item->ID = $i->id_str;
            $item->Title = $i->text;
            $item->Link = sprintf('https://twitter.com/%s/status/%s', $this->_feed, $i->id_str);
            $item->Time = $i->created_at;
            $item->Text = $i->text;
            if (isset($i->user)) {
                $item->Author = $i->user->screen_name;
            }
            $items[] = $item;
        }
        return $items;
    }

    protected function _sortContent($content) {
      usort($content, function($c1, $c2) { return strcmp($c2->ID, $c1->ID); });
      return $content;
    }

    public function title() {
        return sprintf("%s's timeline", $this->_feed);
    }

}

?>