libflickr.lzx

<library>

    <!--
        GLOBALS.
        When using this library, set api to your own flickr api key, from
        http://www.flickr.com/services/api/misc.api_keys.html                
    -->
    <node id="gGlobals">
        <attribute name="SHAREDSECRET" type="string" value="b199acb45fd055e7"/>
        <attribute name="RESTSRC" type="string" value="http://api.flickr.com/services/rest/"/>
    </node>
    
    <class name="photo" width="75" height="75"> 

        <handler name="ondata" args="d">
        
            if ( !d ) return;
            this.showSize('s');
        </handler>
        
        <method name="getImageURL" args="suffix">
        
            var id=datapath.p.getAttr("id");
            
            // build URL to image
            var server=datapath.p.getAttr("server");
            var secret=datapath.p.getAttr("secret");
            var s="http://static.flickr.com/"+server+"/"+id+"_"+secret + suffix + ".jpg";
            return s;
        
        </method>
        
        <!-- 
            s    small square 75x75
            t    thumbnail, 100 on longest side
            m    small, 240 on longest side
            -    medium, 500 on longest side
            b    large, 1024 on longest side (only exists for very large original images)
            o    original image, either a jpg, gif or png, depending on source format
        -->            
        <method name="showSize" args="sz">
            var suffix = "_s"; 
            switch (sz) {
                case 's':
                case 't':
                case 'b': 
                case 'o':                 
                    suffix = "_" + sz;
                    break;
                case 'm':
                    suffix = "";
            }
            this.setSource(this.getImageURL(suffix));
        </method>
        
    </class>

    <!--
        DATA MANAGER
    -->
    <node name="gFlickr">
        <handler name="oninit">
            this.errdel = new lz.Delegate( this, "dsError" );
            this['errdel'].register( photods , "onerror" );
            this['errdel'].register( photods , "ontimeout" );
        </handler>

        <method name="dsError" args="ignore=null">
            this['gError'].show();
        </method>
        
        <!-- 
            builds, signs and sends FlickrRequest 
            results go in provided dataset
            
            REQUIRED ARG - ds - dataset to make call with
            REQUIRED ARG - argobjarr - array of objects {argname:[name], argvalue:[value]}
            REQUIRED ARG - apikey - flickr api key
            
            Get your own flickr api key from http://www.flickr.com/services/api/misc.api_keys.html        

            
            Supports flickr api methods which do not require signing or user authentication.             
            http://www.flickr.com/services/api/
        -->
        <method name="doFlickrRequest" args="ds, argobjarr, apikey">
        
            // clear query params
            ds.setQueryParams(null);

            // string to start building sig
            var api_sig_string=gGlobals.SHAREDSECRET;

            // set source
            ds.setAttribute('src', gGlobals.RESTSRC);

            // add api_key
            argobjarr.push({argname:"api_key", argvalue:apikey});
            
            // alphabetize argobjs comparator
            var argcomparator = function (a, b) {
                if (a.argname < b.argname) return -1;
                if (a.argname > b.argname) return 1;
                return 0
            }
            argobjarr.sort(argcomparator)

            // build argobject
            for (var i=0;i<argobjarr.length;i++) 
            {
                var argobj=argobjarr[i];
                
                ds.setQueryParam(argobj.argname, argobj.argvalue);
            }

            // make actual request
            ds.doRequest();

            Debug.write('Loading URL', ds); 
        </method>

        <method name="getRecent" args="apikey">
            var argobj = [
                    {argname:"method", 
                        argvalue:"flickr.photos.getRecent"}, 
                    {argname:"extras",
                        argvalue:"owner_name"}
                ]                        
            this.doFlickrRequest(photods, argobj, apikey);                    
        
        </method>

        <method name="getInteresting" args="apikey">
            var argobj = [
                    {argname:"method", 
                        argvalue:"flickr.interestingness.getList"}, 
                    {argname:"extras",
                        argvalue:"owner_name"}
                ]                    
            this.doFlickrRequest(photods, argobj, apikey);                    
        
        </method>
            
        <method name="searchWithTags" args="tags, apikey">
            var argobj = [
                    {argname:"method", 
                        argvalue:"flickr.photos.search"},
                    {argname:"tags",
                        argvalue:tags}, 
                    {argname:"extras",
                        argvalue:"owner_name"}
                ]
            this.doFlickrRequest(photods, argobj, apikey);                    
        </method>

        <!-- Load details for the photo  --> 
        <method name="loadDetails" args="photo_id, apikey">
            var argobj = [
                {argname:"method", 
                    argvalue:"flickr.photos.getInfo"},
                {argname:"photo_id",
                    argvalue: photo_id}
                ];
            this.doFlickrRequest(detailsds, argobj, apikey);             
        </method>

    </node>

    <!-- ds to query for photos -->
    <dataset name="photods" ondata="if (global['debugdata']) Debug.write('got data', this)" type="http"/>
    <dataset name="detailsds" ondata="if (global['debugdata']) Debug.write('got data', this)" type="http"/>

    
</library>
<!--
* X_LZ_COPYRIGHT_BEGIN ****************************************************
* Copyright 2006, 2009 Laszlo Systems, Inc.  All Rights Reserved.          *
* Use is subject to license terms.                                        *
* X_LZ_COPYRIGHT_END ******************************************************-->

Cross References

Classes

Named Instances