Ever wanted to select a random image from a MySpace user's photo collection? It's pretty easy to do.
I wrote this code block the other day while doing some MySpace functionality testing, and it occured to me that there may be someone out there who would like to use it. It grabs a random OWNER image URI using a single method call.
Hopefully this will save somebody some time.
To use this object, simply copy it to your code and call iw_RandPhoto.get( Fn ) where Fn is a the callback function that will recieve the image URI as a string.
If you find any problems with this code, please Drop a Line.
Example:
iw_RandPhoto.get( displayImage );
function displayImage( photo_url ){
document.body.innerHTML += "<img src='" + photo_url + "' alt='" + photo_url + "' />";
}
The Code:
/*
Copyright 2008 -- www.iWiddit.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* Retrieves a random OWNER photo using the MyOpenSpace javascript API
*/
iw_RandPhoto = {
/*
* Assembles and sends the request
* @param :: fn :: the callback function to which the random photo url will be passed
* @param-optional :: max_ :: the maximum number of photos to choose from (def is 20)
* @param-optional :: first_ :: the position to start selecting photos from (def is 0)
* @public
*/
get: function( fn, max_, first_ ){
// make sure fn is given
if( typeof( fn ) != 'function' ) return this.err( "No Callback Function Given." );
// defaults
max_ = max_ || 20;
first_ = first_ || 0;
// store this
var _self = this;
// create callback
var cB = function( response ){
_self.handle( response, fn );
}
// Get extended OS namespace
var os = opensocial.Container.get();
// instantiate a request Object
var req = os.newDataRequest();
// get 'OWNER' identifier
var os_owner = opensocial.DataRequest.PersonId.OWNER;
// create parameters
var photo_params = {}
photo_params[ opensocial.DataRequest.PeopleRequestFields.FIRST ] = first_;
photo_params[ opensocial.DataRequest.PeopleRequestFields.MAX ] = max_;
// queue the request
req.add( os.newFetchPhotosRequest( os_owner, photo_params ), 'o_photos' );
// send the request
req.send( cB );
}
/*
* Handles the returned data from the photos request
* @param :: response :: the dataResponse object
* @param :: fn :: the original callback function
* @private
*/
,handle: function( response, fn ){
var data = this.extract( response, 'o_photos' );
if( !data ) this.err( "A response error occured." );
var o_photos = data.asArray();
// find a random index
var elem_num = Math.round( Math.random() * o_photos.length );
// get the url from the photo corresponding to the random index
var photo_url = this.getPhotoUrl( o_photos[ elem_num ] );
// callback with the random photo url as a string and the photo object itself as a second parameter
fn( photo_url, o_photos[ elem_num ] );
}
/*
* Handles error situation and hopefully makes this object easier to use
* @param :: str :: the error string
* @private
*/
,err: function( str ){
str = "iw_RandPhoto Error :: " + str;
if( typeof( console.log ) == 'function' ) console.log( str );
else alert( str );
return;
}
/*
* This is a movable method that checks a response object for errors and handles unsupported issues.
* @param :: response :: the response object
* @param :: expected_item_key :: the key of the data that is being requested
* @returns :: the data object, or null on failure
* @private
*/
,extract: function( response, expected_item_key ){
// get outer response object
var c_resp = response.get( expected_item_key );
// check for overall response error
if( typeof( c_resp.hadError ) == 'function' && c_resp.hadError() ) return;
// get responseItem object from the returned data
var rData = c_resp.getData();
// check for item specific error
if( typeof( rData.hadError ) == 'function' && rData.hadError() ) return;
// finally, return the data object
return rData;
}
/*
* gets a url out of a myspace photo object
* @param :: photo :: the photo object
* @private
*/
,getPhotoUrl: function( photo ){
return photo.getField( MyOpenSpace.Photo.Field.IMAGE_URI );
}
}