Lasso Soft Inc. > Home

[shp_FlickrAPI]

Linkshp_FlickrAPI
AuthorPieter Claerhout
CategoryCustom Tag
Version8.5.x
LicensePublic Domain
Posted12 Dec 2007
Updated12 Dec 2007
More by this author...

Description

This custom type allows you to interact with the Flickr API. It provides a simple way of executing FlickrAPI requests and getting the information. More information on the different methods available in the api can be found here. You will also need a Flickr API key which you can request here.

Sample Usage

// Create a new FlickrAPI object. 
var('flickrApi') = shp_FlickrAPI(-ApiKey='xxxxxxxxx');

// Get information about a person (using the flickr.people.getInfo call)
var:'personInfo' = $flickrApi->PeopleGetInfo(-UserId = 'xxxxx@Nxx');

// Get a list of photos
var:'photos' = $flickrApi->Request(
    -Method = 'flickr.photosets.getPhotos',
    -Params = (Map: 'photoset_id'='xxxxxxxx')
);

// Loop over the photos
Iterate($photos->find('photoset')->find('photo'), (var: 'photo'));
    
    // Get the photo url (for use in an img tag)
    var:'photoUrl' = $flickrApi->PhotoUrl(-Photo=$photo, -Size=$size);

    // Get the page url for a photo
    var:'pageUrl' = $flickrApi->PhotoPage(-Photo=$photo, -UserId='xxxxxx');

/Iterate;

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

[
    
    // Flickr API Connector for Lasso
    // (c) 2007 Pieter Claerhout, SHpartners
    // www.shpartners.com

    // Define the FlickrAPI type
    Define_Type( 
        'shp_FlickrApi', 
        -prototype, 
        -description='Implements the Flickr API .' 
    );

        // The local variables to use
        local: 'ApiKey' = string;     

        // onCreate
        Define_Tag: 'oncreate', -req='ApiKey'; 

            // Remember the API key
            self->ApiKey = #ApiKey; 

        /Define_Tag;

        // Define the flickrRequest tag
        Define_Tag: 'Request', -Required='Method', -Required='Params', -EncodeNone;
            
            // The rest url for Flickr
            local:'flickrRestUrl' = 'http://api.flickr.com/services/rest/';
            
            // Construct the url parameters
            local:'urlParams' = (Array: 'method'=#Method, 'api_key'=self->ApiKey, 'nojsoncallback'=1, 'format'='json');
            Iterate(#Params->Keys, (local: 'key'));
                #urlParams->Insert(#key=#Params->find(#key));
            /Iterate;
    
            // Perform the Flickr Request
            local:'Result' = (Include_URL: #flickrRestUrl, -GETParams=#urlParams);
    
            // Decode the result
            local:'DecodedResult' = self->DecodeJSON(#Result);
    
            // Check the status of the request
            If: #DecodedResult->find('stat') == 'fail';
                Fail: #DecodedResult->find('code'), #DecodedResult->find('message');
            /If;
    
            // Return the decoded result        
            Return: #DecodedResult;
    
        /Define_Tag;

        // Define the flickrPhotoUrl tag
        Define_Tag: 'PhotoUrl', -Required='Photo', -Required='Size', -EncodeNone;
        
            // The list of known sizes
            local:'KnownSizes' = (Map: 'square'='_s', 'thumb'='_t', 'small'='_m', 'medium'='', 'large'='_b');
        
            // Get the size
            If: #KnownSizes->Find(#Size) == null;
                local:'photo_size' = '';
            Else;
                local:'photo_size' = #KnownSizes->Find(#Size);
            /If;
            
            // Construct the url
            local:'Url' = "http://farm" + #Photo->find('farm') + ".static.flickr.com/" + #Photo->find('server') + "/" + #Photo->find('id') + "_" + #Photo->find('secret') + #photo_size + ".jpg";
            
            // Return the url
            Return: #Url;
        
        /Define_Tag;
        
        // Get the page url for a photo
        Define_Tag: 'PhotoPage', -Required='Photo', -Required='UserId', -EncodeNone;
            
            // Return the url
            Return: 'http://www.flickr.com/photos/' + #UserId + '/' + #Photo->find('id');
        
        /Define_Tag;

        // Get information about a person
        Define_Tag: 'PeopleGetInfo', -Required='UserId', -EncodeNone;
        
            // Perform the Flickr request
            local:'Result' = self->Request(
                -Method = 'flickr.people.getInfo',
                -Params = (Map: 'user_id' = #UserId)
            );
            
            // Return the result
            Return: #Result->find('person');
        
        /Define_Tag;

        // Custom JSON decoder (because of a bug in the default library that doesn't always interpret numbers correctly)
        Define_Tag: 'DecodeJSON', -Required='value';
    
            (#value == '') ? Return: Null;
            
            Define_Tag: 'consume_string', -Required='ibytes';
                Local: 'obytes' = bytes;
                local: 'temp' = 0;
                While: ((#temp := #ibytes->(export8bits: #temp)) != 34);
                    #obytes->(import8bits: #temp);
                    (#temp == 92) ? #obytes->(import8bits: #ibytes->export8bits); // Escape \
                /While;
                Local: 'output' = ((String: #obytes)->(Replace: '\\"', '\"') & (Replace: '\\r', '\r') & (Replace: '\\n', '\n') & (Replace: '\\t', '\t') & (Replace: '\\f', '\f') & (Replace: '\\b', '\b') &);
                If: #output->(BeginsWith: '') && #output->(EndsWith: '');
                    Local: 'temp' = #output - '' - '';
                    Local: 'output' = null;
                    Protect;
                        #output->(Deserialize: #temp);
                    /Protect;
                /If;			
                Return: @#output;
            /Define_Tag;
            Define_Tag: 'consume_token', -Required='ibytes', -required='temp';
                Local: 'obytes' = bytes->(import8bits: #temp) &;
                local: 'delimit' = (array: 9, 10, 13, 32, 44, 58, 93, 125); // \t\r\n ,:]}
                While: (#delimit !>> (#temp := #ibytes->export8bits));
                    #obytes->(import8bits: #temp);
                /While;
                Local: 'output' = (String: #obytes);
                If: (#output == 'true') || (#output == 'false');
                    Return: (Boolean: #output);
                Else: (#output == 'null');
                    Return: Null;
                Else: (String_IsNumeric: #output);
                    Return: (#output >> '.') ? (Decimal: #output) | (Integer: #output);
                /If;
                Return: @#output;
            /Define_Tag;
            Define_Tag: 'consume_array', -Required='ibytes';
                Local: 'output' = array;
                local: 'delimit' = (array:  9, 10, 13, 32, 44); // \t\r\n ,
                local: 'temp' = 0;
                While: ((#temp := #ibytes->export8bits) != 93); // ]
                    If: (#delimit >> #temp);
                    Else: (#temp == 34); // "
                        #output->(insert: (consume_string: @#ibytes));
                    Else: (#temp == 91); // [
                        #output->(insert: (consume_array: @#ibytes));
                    Else: (#temp == 123); // {
                        #output->(insert: (consume_object: @#ibytes));
                    Else;
                        #output->(insert: (consume_token: @#ibytes, @#temp));
                        (#temp == 93) ? Loop_Abort;
                    /If;
                /While;
                Return: @#output;
            /Define_Tag;
            Define_Tag: 'consume_object', -Required='ibytes';
                Local: 'output' = map;
                local: 'delimit' = (array:  9, 10, 13, 32, 44); // \t\r\n ,
                local: 'temp' = 0;
                local: 'key' = null;
                local: 'val' = null;
                While: ((#temp := #ibytes->export8bits) != 125); // }
                    If: (#delimit >> #temp);
                        // Discard whitespace 
                    Else: (#key !== null) && (#temp == 34); // "
                        #output->(insert: #key = (consume_string: @#ibytes));
                        #key = null;
                    Else: (#key !== null) && (#temp == 91); // [
                        #output->(insert: #key = (consume_array: @#ibytes));
                        #key = null;
                    Else: (#key !== null) && (#temp == 123); // {
                        #output->(insert: #key = (consume_object: @#ibytes));
                        #key = null;
                    Else: (#key !== null);
                        #output->(insert: #key = (consume_token: @#ibytes, @#temp));
                        (#temp == 125) ? Loop_abort;
                        #key = null;
                    Else;
                        #key = (consume_string: @#ibytes);
                         while(#delimit >> (#temp := #ibytes->export8bits));
                        /while;
                          #temp != 58 ? Loop_Abort;
                    /If;
                /While;
                
                If: (#output >> '__jsonclass__') && (#output->(Find: '__jsonclass__')->(isa: 'array')) && (#output->(Find: '__jsonclass__')->size >= 2) && (#output->(Find: '__jsonclass__')->First == 'deserialize');
                    Return: #output->(find: '__jsonclass__')->Second->First;
                Else: (#output >> 'native') && (#output >> 'comment') && (#output->(find: 'comment') == 'http://www.lassosoft.com/json');
                    Return: #output->(find: 'native');
                /If;
                Return: @#output;
            /Define_Tag;
            
            Local: 'ibytes' = (bytes: #value);
            Local: 'start' = 1;
               #ibytes->removeLeading(BOM_UTF8);
            Local: 'temp' = #ibytes->export8bits;
            If: (#temp == 91); // [
                Local: 'output' = (consume_array: @#ibytes);
                Return: @#output;
            Else: (#temp == 123); // {
                Local: 'output' = (consume_object: @#ibytes);
                Return: @#output;
            /If;
            
        /Define_Tag;
        
    /Define_Type;

]

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft