summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Plugin/Twitter/twitter.class.php
blob: 31173a6d0815901da10fc408a2d42856c6aad0b4 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
 * Sean's Simple Twitter Library
 *
 * Probably a little more or a little less than you need.
 *
 * Copyright 2008, Sean Coates
 * Usage of the works is permitted provided that this instrument is retained
 * with the works, so that any entity that uses the works is notified of this
 * instrument.
 * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
 * ( Fair License - http://www.opensource.org/licenses/fair.php )
 * Short license: do whatever you like with this.
 *
 * komode: le=unix language=php codepage=utf8 tab=4 notabs indent=4
 */
class Twitter {

    /**
     * Base URL for Twitter API
     *
     * Do not specify user/password in URL
     */
    protected $baseUrl = 'http://twitter.com/';
    
    /**
     * Full base URL (includes user/pass)
     *
     * (created in Init)
     */
    protected $baseUrlFull = null;
    
    /**
     * Twitter API user
     */
    protected $user;
    
    /**
     * Twitter API password
     */
    protected $pass;
    
    /**
     * Constructor; sets up configuration.
     * 
     * @param string $user Twitter user name; null for limited read-only access
     * @param string $pass Twitter password; null for limited read-only access
     */
    public function __construct($user=null, $pass=null) {
        $this->baseUrlFull = $this->baseUrl;
        if (null !== $user) {
            // user is defined, so use it in the URL
            $this->user = $user;
            $this->pass = $pass;
            $parsed = parse_url($this->baseUrl);
            $this->baseUrlFull = $parsed['scheme'] . '://' . $this->user . ':' .
                $this->pass . '@' . $parsed['host'];
            // port (optional)
            if (isset($parsed['port']) && is_numeric($parsed['port'])) {
                $this->baseUrlFull .= ':' . $parsed['port'];
            }
            // append path (default: /)
            if (isset($parsed['path'])) {
                $this->baseUrlFull .= $parsed['path'];
            } else {
                $this->baseUrlFull .= '/';
            }
        }
    }

    /**
     * Fetches a tweet by its number/id
     *
     * @param int $num the tweet id/number
     * @return string (null on failure)
     */
    public function getTweetByNum($num) {
        if (!is_numeric($num)) {
            return;
        }
        $tweet = json_decode(file_get_contents($this->getUrlStatus($num)));
        return $tweet;
    }

    /**
     * Reads [last] tweet from user
     *
     * @param string $tweeter the tweeter username
     * @param int $num this many tweets ago (1 = current tweet)
     * @return string (false on failure)
     */
    public function getLastTweet($tweeter, $num = 1)
    {
        $source = json_decode(file_get_contents($this->getUrlUserTimeline($tweeter)));
        if ($num > count($source)) {
            return false;
        }
        $tweet = $source[$num - 1];
        if (!isset($tweet->user->screen_name) || !$tweet->user->screen_name) {
            return false;
        }
        return $tweet;
    }
    
    /**
     * fetches mentions for a user
     */
    public function getMentions($sinceId=null, $count=20) {
        return json_decode(file_get_contents($this->getUrlMentions($sinceId, $count)));
    }
    
    /**
     * Fetches followers for a user
     */
    public function getFollowers($cursor=-1) {
        return json_decode(file_get_contents($this->getUrlFollowers($cursor)));
    }
    
    /**
     * Follow a userid
     */
    public function follow($userId) {
        $params = array(
            'http' => array(
                'method' => 'POST',
                'content' => array(),
                'header' => 'Content-type: application/x-www-form-urlencoded',
            )
        );
        $ctx = stream_context_create($params);
        $fp = fopen($this->getUrlFollow($userId), 'rb', false, $ctx);
        if (!$fp) {
            return false;
        }
        $response = stream_get_contents($fp);
        if ($response === false) {
            return false;
        }
        $response = json_decode($response);
        return $response;
    }
    
    /**
     * fetches DMs for a user
     */
    public function getDMs($sinceId=null, $count=20, $page=1) {
        return json_decode(file_get_contents($this->getUrlDMs($sinceId, $count, $page)));
    }
    
    /**
     * Send DM
     */
    public function sendDM($screenName, $text) {
        $data = http_build_query(array('screen_name'=>$screenName, 'text'=>$text));
        $params = array(
            'http' => array(
                'method' => 'POST',
                'content' => $data,
                'header' => 'Content-type: application/x-www-form-urlencoded',
            )
        );
        $ctx = stream_context_create($params);
        $fp = fopen($this->getUrlSendDM(), 'rb', false, $ctx);
        if (!$fp) {
            return false;
        }
        $response = stream_get_contents($fp);
        if ($response === false) {
            return false;
        }
        $response = json_decode($response);
        return $response;
    }

    /**
     * Sends a tweet
     *
     * @param string $txt the tweet text to send
     * @return string URL of tweet (or false on failure)
     */
    public function sendTweet($txt, $limit=true) {
        if ($limit) {
            $txt = substr($txt, 0, 140); // twitter message size limit
        }
        $data = 'status=' . urlencode($txt);
        $params = array(
            'http' => array(
                'method' => 'POST',
                'content' => $data,
                'header' => 'Content-type: application/x-www-form-urlencoded',
            )
        );
        $ctx = stream_context_create($params);
        $fp = fopen($this->getUrlTweetPost(), 'rb', false, $ctx);
        if (!$fp) {
            return false;
        }
        $response = stream_get_contents($fp);
        if ($response === false) {
            return false;
        }
        $response = json_decode($response);
        return $response;
    }
    
    /**
     * Returns the base API URL
     */
    protected function getUrlApi() {
        return $this->baseUrlFull;
    }
    
    /**
     * Returns the status URL
     *
     * @param int $num the tweet number
     */
    protected function getUrlStatus($num) {
        return $this->getUrlApi() . 'statuses/show/'. urlencode($num) .'.json';
    }
    
    /**
     * Returns the user timeline URL
     */
    protected function getUrlUserTimeline($user) {
        return $this->getUrlApi() . 'statuses/user_timeline/'. urlencode($user) .'.json';
    }
    
    /**
     * Returns the tweet posting URL
     */
    protected function getUrlTweetPost() {
        return $this->getUrlApi() . 'statuses/update.json';
    }
    
    /**
     * Output URL: status
     */
    public function getUrlOutputStatus(StdClass $tweet) {
        return $this->baseUrl . urlencode($tweet->user->screen_name) . '/statuses/' . urlencode($tweet->id);
    }
    
    /**
     * Return mentions URL
     */
    public function getUrlMentions($sinceId=null, $count=20) {
        $url = $this->baseUrlFull . 'statuses/mentions.json?count=' . urlencode($count);
        if ($sinceId !== null) {
            $url .= '&since_id=' . urlencode($sinceId);
        }
        return $url;
    }
    
    /**
     * Returns the followers URL
     */
    public function getUrlFollowers($cursor=-1) {
        return $this->baseUrlFull . 'statuses/followers.json?cursor=' . ((int)$cursor);
    }
    
    /**
     * Returns the follow-user URL
     */
    public function getUrlFollow($userid) {
        return $this->baseUrlFull . 'friendships/create/' . ((int) $userid) . '.json';
    }
    
    /**
     * Returns the get DMs URL
     */
    public function getUrlDMs($sinceId=null, $count=20, $page=1) {
        $url = $this->baseUrlFull . 'direct_messages.json?';
        if ($sinceId !== null) {
            $url .= 'since_id=' . urlencode($sinceId);
        }
        $url .= "&page={$page}";
        $url .= "&count={$count}";
        return $url;
    }

    /**
     * Returns the send DM URL
     */
    public function getURLSendDM() {
        return $this->baseUrlFull . 'direct_messages/new.json';
    }
}