(AS3) Reading Tumblr feeds

| No Comments | No TrackBacks
Tumblr is a smart service where a user can collect contents like photos, videos, texts and so on by uploading or clipping them from a website and make them into a fancy blog-like pages.

http://www.tumblr.com/

I really like this service and have been using for about a year. But as the contents accumulates, it's getting harder to look for the old posts because they are separated into a number of pages. So I came up with an idea of a new visual presentation for the service using the API and this is the first step.

What I did here was just to read the XML feed and display it in a DataGrid, a Flash's pre-set component. It's not good-looking or useful so far. I'll work on this topic again later.

The original Tumblrlog to read the feed from: http://kynddev.tumblr.com/

Tumblr API documentation


 package {
    import flash.display.*;
    import flash.net.*;
    import flash.events.*;
    import flash.text.*;
    import fl.controls.DataGrid;
    import fl.data.DataProvider;

    public class TumblrTest extends Sprite {

        var accountName:String = "kynddev";
        var loadingText:TextField;
       
        public function TumblrTest() {
            feedRequest();
        }

        private function feedRequest():void {
            var loader:URLLoader = new URLLoader  ;
            var req = new URLRequest("http://" + accountName + ".tumblr.com/api/read/?num=50");
            req.method = URLRequestMethod.GET;
            loader.addEventListener(Event.COMPLETE,h_completeLoading);
            loader.load(req);
           
            loadingText = new TextField ();
            loadingText.text = "Loading...";
            addChild(loadingText);
        }

        private function h_completeLoading(evt:Event):void {
            removeChild(loadingText);
           
            evt.target.removeEventListener(Event.COMPLETE,h_completeLoading);
            var feed:XML = new XML((evt.target as URLLoader).data);
            var posts:XMLList = feed.posts.post;

            var dg:DataGrid = new DataGrid  ;
            dg.width = 500;
            dg.height = 500;

            dg.columns = ["title","type","desc"];
            dg.columns[0].width = 200;
            dg.columns[1].width = 60;
           
            var dp:DataProvider = new DataProvider  ;
            var item:Object;
            for (var i:uint = 0; i < posts.length(); i++) {
                item = new Object();
               
                item.type = posts[i].@type;
                if (item.type == "photo") {
                    item.title = posts[i].child("photo-caption")[0];
                } else if  (item.type == "video") {
                    item.title = posts[i].child("video-caption")[0];
                } else if (item.type == "quote") {
                    item.title = posts[i].child("quote-text")[0];
                } else {
                    item.title = posts[i].child("link-text")[0];
                }
                item.desc = (posts[i].child("link-description").length() > 0) ? posts[i].child("link-description")[0] : "no description";
                dp.addItem(item);
            }
           
            dg.dataProvider = new DataProvider(dp);
            addChild(dg);
        }
    }
}




Install flash player 10 or later if you don't see the sample movie.

No TrackBacks

TrackBack URL: http://www.kynd.info/cp-bin/mt/mt-tb.cgi/16

Leave a comment