Yii 1.1: tumblr-data-provider

Library that pulls posts from tumblr + data provider class that can be used with CListView or CGridView
4 followers

This is my very first contribution to the world of open source. I needed to build a blog for http://www.instinctiv.com/ (which is built on Yii) and as tumblr is one of the best blogging solutions on the web and its API allows to pull the content however you'd like without anyone knowing that its hosted on tumblr - it was an amazing shortcut to writing a blog software for the site.

Requirements

Yii 1.1 or above (actually developed and tested only on 1.1.6)

Usage

Unpack the archive with files in lets say protected/extensions/tumblr/ then in your controller just instantiate the tumblr library that pulls the posts with the preferred tumblr user ID and then instantiate the data provider.

class BlogController extends Controller
    {
        public function actionIndex()
        {
            Yii::import('application.extensions.tumblr.ETumblrDataProvider', true);
 
            $tumblrLib = new ETumblrJson('instinctiv');
            $dataProvider = new ETumblrDataProvider($tumblrLib);
 
            $this->render('index', array(
                'dataProvider' => $dataProvider
            ));
        }
    }

In your views/blog/index.php lets have this:

$this->widget('zii.widgets.CListView', array(
    'dataProvider' => $dataProvider,
    'itemView' => '_post',
));

Then in views/blog/_post.php you will have each blog post as PHP array under the "$data" var. As tumblr allows different types of posts (quote, text, video, audio, photo) and it produces different response for each and every of them, I would recommend having your _post.php view to have code that does the generic styling (html + css classes, ids), something like this:

<div class="post">
    <div class="date"><?php echo $data['date']; ?></div>
    <?php $this->renderPartial('_' . $data['type'] . 'Post', array('data' => $data)); ?>
</div>

This way you will have to create protected/views/blog/_audioPost.php, protected/views/blog/_videoPost.php, etc for every kind of post but you will have maximum control over the content.

Future enhencements: - Have in mind that to minimize requests to remote host (tumblr api servers) thus reducing lag of page loads, caching should be implemented. - Build another simple class to take care of remote requests, tumblr JSON API lib should not care how the posts are fetched.

As this is my first extension, feel free to report bugs, suggest improvements, etc, here or at:

dimitar@instinctiv.com

Regards, Dimitar Dinchev at http://instinctiv.com/

Total 6 comments

#8609 report it
Veseliq at 2012/06/14 05:34am
Go for it

After all, the code should be fairly readable. I haven't used in while as well. Try it, modify it as needed.

#8606 report it
Neil Kenealy at 2012/06/14 04:56am
Is anyone using this extension now?

I'm worried about using it because it hasn't been updated in about a year.

#3121 report it
Veseliq at 2011/03/17 07:45am
Re: An issue with pagination

Thank you Dmitri!

Yes, I have already solved that. I will push a new version soon, I'm releasing the new Instinctiv site in matter of few days, then I will release the new version with this problem solved, fetching post by id and even caching.

#3120 report it
daemons at 2011/03/17 06:55am
An issue with pagination

Dimitar,

Don't know, maybe you already fixed it, but I've encountered a little issue when I tried to use your data provider within a CListView with pagination.

The problem starts to emerge in the ETumblrDataProvider::fetchData() method. When we want the pagination to be working correctly, we first need to get the "totalItemCount" and then, based on the calculated offset and limit, get a range of posts. With Tumblr API this will take two requests but with different parameter sets (actually this only slows down everything, but you can't dump out all posts to the user).

For the sake of optimization in the ETumblrJson::setParams() method you first check if passed params differ and only then update them and clear "_response". However the first call to getTotalItemCount() already set "_params" to the instance that was created inside ETumblrDataProvider::getTumblrParams() and the difference check always returns false since both variables refer to the same instance of ETumblrParams.

I suggest cloning params, not simple assignment which is by reference for objects.

Anyways, for me this is a very useful extension. I already had a simple logic to retrieve posts from Tumblr (though it used XML) but it was inflexible, and when it came to pagination I decided to implement a data provider. I'm lucky I decided to check among Yii extensions and found yours! It saved me a great deal of time.

Thank you and good luck!

Regards, Dmitri Silaev

#3030 report it
Veseliq at 2011/03/09 04:25am
Further development

Thank you for your comment. Currently I'm actively developing the Instinctiv Blog so I don't have time to clean the code and push new version but when I do, it will have the ability to fetch post by ID (for post permalink page) and I'm working on implementing reblogging and liking features of tumblr. In the end it might become a complete solution.

#3027 report it
Dana at 2011/03/08 09:52pm
Can't wait to try it =)

I like the simplicity of use that you've described.

Leave a comment

Please to leave your comment.

Create extension