|
| 1 | +Twitter Stream Bundle |
| 2 | +============= |
| 3 | + |
| 4 | +A Symfony bundle that connects you to Twitters stream API, currently only supporting **statuses/filter** (see https://dev.twitter.com/docs/api/1.1/post/statuses/filter) |
| 5 | + |
| 6 | +Twitter Stream Bundle simply: |
| 7 | + |
| 8 | + - Connects to Twitters streaming API |
| 9 | + - Recieves any data which matches your `track` keywords. |
| 10 | + - Triggers an event when a new Tweet is received |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +Version |
| 15 | +---- |
| 16 | + |
| 17 | +0.1 - Initial Release |
| 18 | + |
| 19 | +Installation |
| 20 | +-------------- |
| 21 | + |
| 22 | +Add TwitterStreamBundle to your composer.json: |
| 23 | + |
| 24 | +``` |
| 25 | +{ |
| 26 | + "require": { |
| 27 | + "scripter-co/twitter-stream-bundle": "dev-master" |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Get composer to fetch the package for you: |
| 33 | + |
| 34 | +``` |
| 35 | +php composer.phar update scripter-co/twitter-stream-bundle |
| 36 | +``` |
| 37 | + |
| 38 | +Usage |
| 39 | +----- |
| 40 | + |
| 41 | +Craete a `command` under your bundle (e.g. `CoreBundle\Command\TwitterStreamCommand.php`) and place the : |
| 42 | + |
| 43 | +``` |
| 44 | +$twitter_request_service = $this->container->get('scripterco_twitter_stream.request'); |
| 45 | +$twitter_request_service->setConsumerKey('KEY') |
| 46 | + ->setConsumerSecret('SECRET') |
| 47 | + ->setToken('TOKEN') |
| 48 | + ->setTokenSecret('SECRET') |
| 49 | + ->setKeywords(array( |
| 50 | + 'my_keyword' |
| 51 | + )) |
| 52 | + ->start(); |
| 53 | +``` |
| 54 | + |
| 55 | +When a tweet is found with your `my_keyword` keyword, it will trigger an event (`scripterco_twitter_stream.received`), so we need to create an event listener and add it to our services file: |
| 56 | + |
| 57 | +`CoreBundle\EventHandler\RequestEventHandler`: |
| 58 | +``` |
| 59 | +<?php |
| 60 | +
|
| 61 | +namespace Acme\CoreBundle\EventHandler; |
| 62 | +
|
| 63 | +class RequestEventHandler |
| 64 | +{ |
| 65 | + |
| 66 | + public $_container; |
| 67 | + |
| 68 | + public function __construct($container) |
| 69 | + { |
| 70 | + $this->_container = $container; |
| 71 | + } |
| 72 | +
|
| 73 | + public function processTweet($request_event) |
| 74 | + { |
| 75 | + $doctrine = $this->_container->get('doctrine'); |
| 76 | + |
| 77 | + $tweet_model = $request_event->getTweet(); |
| 78 | + |
| 79 | + // tweet id |
| 80 | + echo $tweet_model->get('id'); |
| 81 | + |
| 82 | + // user screen name |
| 83 | + echo $tweet_model->get('user')->get('screen_name') |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +**Note:** If you want to see all the available parameters, see this [here](https://gist.github.com/scripter-co/6905227). |
| 89 | + |
| 90 | +Now, we just need to add the event listener to the services file, I'm (using yaml): |
| 91 | + |
| 92 | +``` |
| 93 | +acme_core.twitter.event: |
| 94 | + class: Acme\CoreBundle\EventHandler\RequestEventHandler |
| 95 | + arguments: [@service_container] |
| 96 | + tags: |
| 97 | + - { name: kernel.event_listener, event: scripterco_twitter_stream.received, method: processTweet } |
| 98 | +``` |
| 99 | + |
| 100 | +That's it, you can easily add in saving of the tweets to a database as the `container` is available within your event handler. |
| 101 | + |
| 102 | + |
| 103 | +MIT License |
| 104 | +---- |
| 105 | + |
| 106 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 107 | + of this software and associated documentation files (the "Software"), to deal |
| 108 | + in the Software without restriction, including without limitation the rights |
| 109 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 110 | + copies of the Software, and to permit persons to whom the Software is furnished |
| 111 | + to do so, subject to the following conditions: |
| 112 | + |
| 113 | + The above copyright notice and this permission notice shall be included in all |
| 114 | + copies or substantial portions of the Software. |
| 115 | + |
| 116 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 117 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 118 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 119 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 120 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 121 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 122 | + THE SOFTWARE. |
| 123 | + |
0 commit comments