-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHundredFortySecondsRace.php
More file actions
282 lines (224 loc) · 8.13 KB
/
HundredFortySecondsRace.php
File metadata and controls
282 lines (224 loc) · 8.13 KB
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
<?php
$game = new HundredFortySecondsRace(array('#CuandoHaceFrioYo', '#OkuluSevmemeNedenim')); //hashtags
$game->login('MsrAEqq42Bom767QusO4318PL', 'C3odEyJEEF4UrLFnoj44wtR8f0kWOjpyUS9Ytmp4wxF0RLs3Qw', '124289477-qNZe9IBb23YNOoL7r5bERIBkM0nV4d4jSr84dDzI', '7JnyD1Y1skRmdFJuH24LzywM2GJhjwsxsjp5TtZK1Hwd1');
$game->start();
class Player {
private $hash;
private $points;
public function __construct($hash) {
$this->hash = $hash;
}
public function setPoints($points) {
$this->points = $points;
}
public function getPoints() {
return $this->points;
}
public function getHash() {
return $this->hash;
}
};
class HundredFortySecondsRace {
private $scoring = Array(
"characters" => 1,
"retweet" => 10,
"favorite" => 20,
"hashtag" => 4,
"symbols" => 2,
"urls" => 3,
"user_mentions" => 5,
"media" => 8
);
private $dieAfter = 50;
private $player1;
private $player2;
private $_hashes;
private $m_oauth_consumer_key;
private $m_oauth_consumer_secret;
private $m_oauth_token;
private $m_oauth_token_secret;
private $m_oauth_nonce;
private $m_oauth_signature;
private $m_oauth_signature_method = 'HMAC-SHA1';
private $m_oauth_timestamp;
private $m_oauth_version = '1.0';
public function __construct($hashes) {
//
// set a time limit to unlimited
//
set_time_limit(0);
if(count($hashes) != 2)
die("We need 2 hashes!");
$this->_hashes = $hashes;
$this->player1 = new Player($hashes[0]);
$this->player2 = new Player($hashes[1]);
}
//
// set the login details
//
public function login($_consumer_key, $_consumer_secret, $_token, $_token_secret) {
$this->m_oauth_consumer_key = $_consumer_key;
$this->m_oauth_consumer_secret = $_consumer_secret;
$this->m_oauth_token = $_token;
$this->m_oauth_token_secret = $_token_secret;
//
// generate a nonce; we're just using a random md5() hash here.
//
$this->m_oauth_nonce = md5(mt_rand());
return true;
}
//
// process a tweet object from the stream
//
private function process_tweet(array $_data) {
$player = null;
foreach($_data["entities"]["hashtags"] as $hash){
if($hash["text"] == str_replace("#", "", $this->player1->getHash())){
$player = $this->player1;
break;
}
if($hash["text"] == str_replace("#", "", $this->player2->getHash())){
$player = $this->player2;
break;
}
}
$player->setPoints($player->getPoints() + (strlen($_data["text"]) * $this->scoring["characters"]));
if(!is_null($_data["coordinates"]))
$player->setPoints($player->getPoints() + $this->scoring["coordinates"]);
//retweets
$player->setPoints($player->getPoints() + ($_data["retweet_count"] * $this->scoring["retweet"]));
//favorites
$player->setPoints($player->getPoints() + ($_data["favorite_count"] * $this->scoring["favorite"]));
//hashtags
$player->setPoints($player->getPoints() + (count($_data["entities"]["hashtags"]) * $this->scoring["hashtag"]));
//symbols
$player->setPoints($player->getPoints() + (count($_data["entities"]["symbols"]) * $this->scoring["symbols"]));
//urls
$player->setPoints($player->getPoints() + (count($_data["entities"]["urls"]) * $this->scoring["urls"]));
//user_mentions
$player->setPoints($player->getPoints() + (count($_data["entities"]["user_mentions"]) * $this->scoring["user_mentions"]));
//user_mentions
if(isset($_data["entities"]["media"]))
$player->setPoints($player->getPoints() + (count($_data["entities"]["media"]) * $this->scoring["media"]));
echo "\nhash: ".$player->getHash()." (".$player->getPoints().")";
//if($_data["coordinates"])
//$player->setPoints( $player->getPoints() + strlen($_data["text"]));
/*
$_data["text"]
$_data["coordinates"]
$_data["retweet_count"]
$_data["favorite_count"]
$_data["entities"]
*/
$this->dieAfter--;
if($this->dieAfter <= 0)
die("\nGAME OVER!\n\n".
$this->player1->getHash().": ".$this->player1->getPoints()."\n".
$this->player2->getHash().": ".$this->player2->getPoints())."\n\n";
//return true;
}
//
// the main stream manager
//
public function start() {
while (1) {
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
if (!$fp) {
echo "ERROR: Twitter Stream Error: failed to open socket";
} else {
//
// build the data and store it so we can get a length
//
$data = 'track=' . rawurlencode(implode($this->_hashes, ','));
//
// store the current timestamp
//
$this->m_oauth_timestamp = time();
//
// generate the base string based on all the data
//
$base_string = 'POST&' . rawurlencode('https://stream.twitter.com/1.1/statuses/filter.json') . '&' . rawurlencode('oauth_consumer_key=' . $this->m_oauth_consumer_key . '&' . 'oauth_nonce=' . $this->m_oauth_nonce . '&' . 'oauth_signature_method=' . $this->m_oauth_signature_method . '&' . 'oauth_timestamp=' . $this->m_oauth_timestamp . '&' . 'oauth_token=' . $this->m_oauth_token . '&' . 'oauth_version=' . $this->m_oauth_version . '&' . $data);
//
// generate the secret key to use to hash
//
$secret = rawurlencode($this->m_oauth_consumer_secret) . '&' . rawurlencode($this->m_oauth_token_secret);
//
// generate the signature using HMAC-SHA1
//
// hash_hmac() requires PHP >= 5.1.2 or PECL hash >= 1.1
//
$raw_hash = hash_hmac('sha1', $base_string, $secret, true);
//
// base64 then urlencode the raw hash
//
$this->m_oauth_signature = rawurlencode(base64_encode($raw_hash));
//
// build the OAuth Authorization header
//
$oauth = 'OAuth oauth_consumer_key="' . $this->m_oauth_consumer_key . '", ' . 'oauth_nonce="' . $this->m_oauth_nonce . '", ' . 'oauth_signature="' . $this->m_oauth_signature . '", ' . 'oauth_signature_method="' . $this->m_oauth_signature_method . '", ' . 'oauth_timestamp="' . $this->m_oauth_timestamp . '", ' . 'oauth_token="' . $this->m_oauth_token . '", ' . 'oauth_version="' . $this->m_oauth_version . '"';
//
// build the request
//
$request = "POST /1.1/statuses/filter.json HTTP/1.1\r\n";
$request .= "Host: stream.twitter.com\r\n";
$request .= "Authorization: " . $oauth . "\r\n";
$request .= "Content-Length: " . strlen($data) . "\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
$request .= $data;
//
// write the request
//
fwrite($fp, $request);
//
// set it to non-blocking
//
stream_set_blocking($fp, 0);
while (!feof($fp)) {
$read = array($fp);
$write = null;
$except = null;
//
// select, waiting up to 10 minutes for a tweet; if we don't get one, then
// then reconnect, because it's possible something went wrong.
//
$res = stream_select($read, $write, $except, 600, 0);
if (($res == false) || ($res == 0)) {
break;
}
//
// read the JSON object from the socket
//
$json = fgets($fp);
//
// look for a HTTP response code
//
if (strncmp($json, 'HTTP/1.1', 8) == 0) {
$json = trim($json);
if ($json != 'HTTP/1.1 200 OK') {
echo 'ERROR: ' . $json . "\n";
return false;
}
}
//
// if there is some data, then process it
//
if (($json !== false) && (strlen($json) > 0)) {
//
// decode the socket to a PHP array
//
$data = json_decode($json, true);
if ($data) {
//
// process it
//
$this->process_tweet($data);
}
}
}
}
fclose($fp);
sleep(10);
}
return;
}
};