Welcome Guest, Not a member yet? Register   Sign In
Small library for fetching and formatting Twitter feeds (With cache!)
#1

[eluser]Starovich[/eluser]
I needed to see a Twitter feed on a homepage I was developing and looked into what I could find that was compatible with CodeIgniter. Unfortunately, many libraries were either outdated or overly complex. Some of them didn't utilise oAuth (An authentication system Twitter will require after June 2010) and others required Curl, which my hosting provider does not have.

Then I figured that since I would only need to get feeds, not update twitter status from inside my application, I could use a RSS library to fetch the regular RSS that Twitter provides for every user. SimplePie worked fine, and here we are.

So if you are interested in -only- fetching feeds and displaying them on a page, this is for you.

Prequisites:
- Simplepie for CodeIgniter, get it here: http://www.haughin.com/code/simplepie/
(and put it in your /libraries/ folder)
- A database configured in CI, must support TRUNCATE command. (MySQL works fine for this, other DB's should too.)

Optional:
- Cron, or another way of scheduling the cache update.

Download:
Zip file is attached to post!

Installation:
You will need to upload the two files, Twitter_rss.php and twitter_rss_queries.php to the appropriate folders. The directory structure is intact so you should be able to drop the folders directly into your config. Also don't forget to fetch Simplepie.php, look at Prequisites above.

Then create the table we will use to store the tweets, 'media_tweets' for which the structure you will find in create.sql inside the downloaded zip archive.

Code:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Table structure for table `media_tweets`
--

CREATE TABLE IF NOT EXISTS `media_tweets` (
  `tweet_id` int(11) NOT NULL auto_increment,
  `tweet_date` text NOT NULL,
  `tweet_content` text NOT NULL,
  PRIMARY KEY  (`tweet_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

You will need to use some kind of tool (like phpMyAdmin) or use a console for this.

Now we're all set, using the library from any controller works like this:
Code:
$this->load->library('twitter_rss', array('number_of_items' => 3, 'feed_url' => 'http://twitter.com/statuses/user_timeline/12889082.rss'));
        
//fetches the rss, formats it and puts it in your database. Probably best to set this in a CRON job every X minutes.
$this->twitter_rss->refresh_twitter_rss();
        
//Shows the tweets from DB with simple html formatting using divs
$data['my_formatted_rss'] = $this->twitter_rss->get_formatted_feed();

The content of $data['my_formatted_rss'] will be:
Code:
<div id="content-rss">
    <div id="content-rss-heading">
        30/03/2010
    </div>
    
    <div id="content-rss-text">
         Tweet content here
    </div>
</div>

<div id="content-rss">
    <div id="content-rss-heading">
        29/03/2010
    </div>
    
    <div id="content-rss-text">
         Second tweet content here
    </div>
</div>

<div id="content-rss">
    <div id="content-rss-heading">
        23/03/2010
    </div>
    
    <div id="content-rss-text">
         Third tweet content here
    </div>
</div>

There are no additional options for the library than in the example. If there will be popular demand I could add styling options etc, but the library code clocks in at only 150 rows so it shouldn't be too hard to add if needed.

This is my very first library, so if you spot something that isn't correct, let me know!
#2

[eluser]nolastan[/eluser]
Thanks, I found this very helpful.

One note: in Twitter_rss.php:
Code:
/* parses the DB output of the tweets */
    function parse_rss_feed($raw_indata)
    {
        $string_builder = '';
        foreach ($raw_indata as $row)
        {
            $string_builder .= '<li id="content-rss">
            
                                    <div id="content-rss-heading">
                                        ' . $row['tweet_date'] .
                                    '</div>
                                    
                                    <div id="content-rss-text">
                                        '. $row['tweet_content'] .
                                    '</div>
                                    
                                </div>';
        }
        
        return $string_builder;
    }
You are defining div ID's in a loop, resulting in multiple div's with the same ID, which isn't proper. So you should change those to classes. Minor error in an otherwise very helpful library!

Thanks again,
Stan
#3

[eluser]nolastan[/eluser]
One more issue...
Code:
/* kindly stolen from: http://codeigniter.com/wiki/Twitter-Tweet_Parser/ */
    function parse_tweet($tweet)
    {
        $search = array('|(http://[^ ]+)|', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i');
        $replace = array('<a href="$1" target="_blank">$1</a>', '$1<a href="http://twitter.com/$2" target="_blank">@$2</a>', '$1<a href="http://search.twitter.com/search?q=#$2" target="_blank">#$2</a>');
        $tweet = preg_replace($search, $replace, $tweet);
        
        return $tweet;
    }
Having the hashtag in the URL messes up the link, and brings you to an empty Twitter search page. One fix would be to remove the hashtag from the link; another would be to use the HTML entity instead.




Theme © iAndrew 2016 - Forum software by © MyBB