Welcome Guest, Not a member yet? Register   Sign In
Phirehose or a similar library in CI
#1

[eluser]s0l1dsnak3123[/eluser]
Hi there, I am looking for a CI-friendly library to use with twitter's Streaming API. I found one such library on twitter's recommended list (called phirehose), but it doesn't look like it would be usable in an MVC framework. Am I wrong? Is there a more CI-friendly library I can use with the streaming API?
#2

[eluser]umefarooq[/eluser]
hi this is what you are looking for have look on this library

http://www.haughin.com/code/twitter/
#3

[eluser]s0l1dsnak3123[/eluser]
Hi there, no that is not what I am looking for, haughin's library makes use of the regular API. I want to use the Streaming API, which is a different kettle of fish all together.
#4

[eluser]danoph[/eluser]
Hi s0l1dsnak3123,

Libraries like these can be used easily with MVC frameworks like CodeIgniter. Just place the PHP file or lib folder in your application/libraries folder and put the require('script here'); line in your controller's function.
#5

[eluser]s0l1dsnak3123[/eluser]
Thanks danoph, I figured it out myself:

Code:
<?php
require_once('static/libs/Phirehose.php');

class Twitter extends Controller {
    function Twitter() {
        parent::Controller();    
        $this->load->model('twitter_model');
    }
    function auto_reply(){
        $this->load->model('database');
        // Start streaming/collecting data and inserting it into the database to be analysed.
        $sc = new GhettoQueueCollector( $this->twitter_model->get_option('twitter_username'),
        $this->twitter_model->get_option('twitter_password'));
        
        //Get the terms with which to search for important posts
        $search_terms[] = $this->twitter_model->get_option('twitter_stream_search_1');
        //$search_terms[] = $this->twitter_model->get_option('twitter_stream_search_2');
        
        $sc->setTrack( $search_terms );
        $sc->consume();
    }
}

class GhettoQueueCollector extends Phirehose {
    public function __construct($username, $password, $rotateInterval = 10) {
        
        $this->CI =& get_instance();
        $this->CI->load->model('twitter_model');
        $this->CI->load->database();
        $this->rotateInterval = $rotateInterval;
        
        // Call parent constructor
        return parent::__construct($username, $password, Phirehose::METHOD_FILTER);
    }
    
    /**
    * Enqueue each status
    *
    * @param string $status
    */
    public function enqueueStatus($status) {
        // Write the status to the stream (must be via getStream())
        
        $data = array('timestamp' => time(), 'data' => $status );
        
        $this->CI->db->insert('twitter_stream',$data);
        //keep database connection open.
        $this->CI->db->reconnect();
        
    }
    
    public function checkFilterPredicates() {
        
        $search_terms[] = $this->CI->twitter_model->get_option('twitter_stream_search_1');
        //$search_terms[] = $this->CI->twitter_model->get_option('twitter_stream_search_2');
        
        $this->setTrack( $search_terms );
    }
}
#6

[eluser]danoph[/eluser]
Oh ok. One tip since you already posted your controller code...I would recommend keeping the controller code and extended library class separate so you stick with the MVC principle and stay organized. Things get ugly if your controller code gets a lot bigger. Hard to keep track of things then!
#7

[eluser]s0l1dsnak3123[/eluser]
Agreed! Where should I store my custom functions for the library? I need the codeigniter framework in order to use it correctly.
#8

[eluser]danoph[/eluser]
I understand that you need the library. When I add third party libraries to my applications, I try to stick with the MVC principle as much as I can, or you don't really benefit as much by using the framework. You should make your own library that extends the phirehose library. So they should be in the same folder in application/libraries. Then, you can load your custom library in the controller by using normal $this->load->library() code, and put the require statement in the library file.

A few more tips...if you create any library and need to access variables from CI, such as another library, session data, the database, etc, you should call other libraries like this in your library's functions:

Code:
$CI =& get_instance();

$CI->db->from('users');
$query = $CI->db->get();

In the codeigniter user guide and some other CI tutorials, they declare and use the $CI var in the class outside of your functions, like this...DO NOT do the following!

Code:
class MyLibrary extends Library {

var $CI;

function __construct() {
parent::__construct();

$this->CI =& get_instance();
}
}

Even though you are using & to copy by reference, it creates a duplicate copy of CI in memory. Having 2 duplicate CI instances in memory makes performance terrible!




Theme © iAndrew 2016 - Forum software by © MyBB