Skip to content

Commit c39c080

Browse files
NathanNathan
authored andcommitted
initial commit
0 parents  commit c39c080

File tree

13 files changed

+381
-0
lines changed

13 files changed

+381
-0
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store
32+
.DS_Store?
33+
._*
34+
.Spotlight-V100
35+
.Trashes
36+
ehthumbs.db
37+
Thumbs.db

DependencyInjection/Configuration.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* This is the class that validates and merges configuration from your app/config files
10+
*
11+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfigTreeBuilder()
19+
{
20+
$treeBuilder = new TreeBuilder();
21+
$rootNode = $treeBuilder->root('scripterco_twitter_stream');
22+
23+
// Here you should define the parameters that are allowed to
24+
// configure your bundle. See the documentation linked above for
25+
// more information on that topic.
26+
27+
return $treeBuilder;
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader;
9+
10+
/**
11+
* This is the class that loads and manages your bundle configuration
12+
*
13+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14+
*/
15+
class TwitterStreamExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
22+
$configuration = new Configuration();
23+
$config = $this->processConfiguration($configuration, $configs);
24+
25+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26+
$loader->load('services.yml');
27+
}
28+
}

Event/RequestEvent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Event;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
7+
class RequestEvent extends Event
8+
{
9+
10+
private $_tweet;
11+
12+
public function setTweet($tweet)
13+
{
14+
$this->_tweet = $tweet;
15+
}
16+
17+
public function getTweet()
18+
{
19+
return $this->_tweet;
20+
}
21+
22+
}

Model/Model.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Model;
4+
5+
abstract class Model
6+
{
7+
8+
public function get($variable_name)
9+
{
10+
return isset($this->$variable_name) ? $this->$variable_name : false;
11+
}
12+
13+
}

Model/TweetModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Model;
4+
5+
class TweetModel extends Model
6+
{
7+
}

Model/UserModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Model;
4+
5+
class UserModel extends Model
6+
{
7+
}

Request/Request.php

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Request;
4+
5+
use ScripterCo\Bundle\TwitterStreamBundle\Model\TweetModel,
6+
ScripterCo\Bundle\TwitterStreamBundle\Model\UserModel,
7+
ScripterCo\Bundle\TwitterStreamBundle\RequestEvents,
8+
ScripterCo\Bundle\TwitterStreamBundle\Event\RequestEvent;
9+
10+
class Request implements RequestInterface
11+
{
12+
private $_oauth_consumer_key;
13+
private $_oauth_consumer_secret;
14+
private $_oauth_token;
15+
private $_oauth_token_secret;
16+
private $_keywords = array();
17+
18+
private $_oauth_nonce;
19+
private $_oauth_signature;
20+
private $_oauth_signature_method = 'HMAC-SHA1';
21+
private $_oauth_timestamp;
22+
private $_oauth_version = '1.0';
23+
24+
public function __construct($event_dispatcher)
25+
{
26+
set_time_limit(0);
27+
$this->_event_dispatcher = $event_dispatcher;
28+
$this->_oauth_nonce = md5(mt_rand());
29+
}
30+
31+
public function setConsumerKey($consumer_key)
32+
{
33+
$this->_oauth_consumer_key = $consumer_key;
34+
35+
return $this;
36+
}
37+
38+
public function setConsumerSecret($consumer_secret)
39+
{
40+
$this->_oauth_consumer_secret = $consumer_secret;
41+
42+
return $this;
43+
}
44+
45+
public function setToken($token)
46+
{
47+
$this->_oauth_token = $token;
48+
49+
return $this;
50+
}
51+
52+
public function setTokenSecret($token_secret)
53+
{
54+
$this->_oauth_token_secret = $token_secret;
55+
56+
return $this;
57+
}
58+
59+
60+
private function process(array $_data)
61+
{
62+
print_r($_data);
63+
64+
return true;
65+
}
66+
67+
public function setKeywords($keywords)
68+
{
69+
if(!is_array($keywords)){
70+
throw new \Exception('setKeywords expects parameter one to be an array.');
71+
}
72+
if(count($keywords) === 0){
73+
throw new \Exception('setKeywords expects keywords to contain at least one array element.');
74+
}
75+
$this->_keywords = $keywords;
76+
77+
return $this;
78+
}
79+
80+
private function _buildRequest()
81+
{
82+
$data = 'track=' . rawurlencode(implode($this->_keywords, ','));
83+
$this->_oauth_timestamp = time();
84+
$base_string = 'POST&' .
85+
rawurlencode('https://stream.twitter.com/1.1/statuses/filter.json') . '&' .
86+
rawurlencode('oauth_consumer_key=' . $this->_oauth_consumer_key . '&' .
87+
'oauth_nonce=' . $this->_oauth_nonce . '&' .
88+
'oauth_signature_method=' . $this->_oauth_signature_method . '&' .
89+
'oauth_timestamp=' . $this->_oauth_timestamp . '&' .
90+
'oauth_token=' . $this->_oauth_token . '&' .
91+
'oauth_version=' . $this->_oauth_version . '&' .
92+
$data);
93+
$secret = rawurlencode($this->_oauth_consumer_secret) . '&' .
94+
rawurlencode($this->_oauth_token_secret);
95+
$raw_hash = hash_hmac('sha1', $base_string, $secret, true);
96+
$this->_oauth_signature = rawurlencode(base64_encode($raw_hash));
97+
$oauth = 'OAuth oauth_consumer_key="' . $this->_oauth_consumer_key . '", ' .
98+
'oauth_nonce="' . $this->_oauth_nonce . '", ' .
99+
'oauth_signature="' . $this->_oauth_signature . '", ' .
100+
'oauth_signature_method="' . $this->_oauth_signature_method . '", ' .
101+
'oauth_timestamp="' . $this->_oauth_timestamp . '", ' .
102+
'oauth_token="' . $this->_oauth_token . '", ' .
103+
'oauth_version="' . $this->_oauth_version . '"';
104+
105+
$request = "POST /1.1/statuses/filter.json HTTP/1.1\r\n";
106+
$request .= "Host: stream.twitter.com\r\n";
107+
$request .= "Authorization: " . $oauth . "\r\n";
108+
$request .= "Content-Length: " . strlen($data) . "\r\n";
109+
$request .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
110+
$request .= $data;
111+
return $request;
112+
}
113+
114+
public function start()
115+
{
116+
while(1)
117+
{
118+
$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);
119+
if (!$fp){
120+
echo "ERROR: Twitter Stream Error: failed to open socket";
121+
}else{
122+
123+
fwrite($fp, $this->_buildRequest());
124+
125+
stream_set_blocking($fp, 0);
126+
127+
while(!feof($fp))
128+
{
129+
$read = array($fp);
130+
$write = null;
131+
$except = null;
132+
133+
$res = stream_select($read, $write, $except, 600, 0);
134+
if(($res == false) || ($res == 0)){
135+
break;
136+
}
137+
138+
$json = fgets($fp);
139+
140+
if (strncmp($json, 'HTTP/1.1', 8) == 0){
141+
$json = trim($json);
142+
if ($json != 'HTTP/1.1 200 OK'){
143+
echo 'ERROR: ' . $json . "\n";
144+
return false;
145+
}
146+
}
147+
148+
if(($json !== false) && (strlen($json) > 0)){
149+
$tweet = json_decode($json);
150+
if($tweet){
151+
152+
$tweet_model = new TweetModel();
153+
$user_model = new UserModel();
154+
foreach($tweet as $key => $value){
155+
if($key === 'user'){
156+
foreach($value as $key => $value){
157+
$user_model->$key = $value;
158+
}
159+
$tweet_model->user = $user_model;
160+
}else{
161+
$tweet_model->$key = $value;
162+
}
163+
}
164+
165+
$request_event = new RequestEvent();
166+
$request_event->setTweet($tweet_model);
167+
168+
$this->_event_dispatcher->dispatch(RequestEvents::TWEET_RECIEVED, $request_event);
169+
}
170+
}
171+
}
172+
}
173+
174+
fclose($fp);
175+
sleep(10);
176+
}
177+
178+
return;
179+
}
180+
};

Request/RequestInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle\Request;
4+
5+
interface RequestInterface
6+
{
7+
8+
public function setConsumerKey($consumer_key);
9+
10+
public function setConsumerSecret($consumer_secret);
11+
12+
public function setToken($token);
13+
14+
public function setTokenSecret($token_secret);
15+
16+
public function start();
17+
18+
}

RequestEvents.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ScripterCo\Bundle\TwitterStreamBundle;
4+
5+
final class RequestEvents
6+
{
7+
const TWEET_RECIEVED = 'scripter_twitter_stream.received';
8+
}

0 commit comments

Comments
 (0)