2 * Basic flickr search uses flickr jsonp api
3 * http://www.flickr.com/services/api/
6 * we look for licenses from method=flickr.photos.licenses.getInfo
7 * per http://commons.wikimedia.org/wiki/Special:Upload?uselang=fromflickr
8 * we are interested in:
9 * (4) Attribution License
10 * (5) Attribution-ShareAlike License,
11 * (7) No known copyright restrictions,
12 * (8) United States Government Work
15 var flickrSearch = function ( iObj
) {
16 return this.init( iObj
);
18 flickrSearch
.prototype = {
19 dtUrl
: 'http://www.flickr.com/photos/',
20 // @@todo probably would be good to read the api-key from configuration
21 apikey
: '2867787a545cc66c0bce6f2e57aca1d1',
22 // What license we are interested in
23 _license_keys
: '4,5,7,8',
24 _srctypes
: ['t', 'sq', 's', 'm', 'o'],
26 '4' : 'http://creativecommons.org/licenses/by/3.0/',
27 '5' : 'http://creativecommons.org/licenses/by-sa/3.0/',
28 '7' : 'http://www.flickr.com/commons/usage/',
29 '8' : 'http://www.usa.gov/copyright.shtml'
32 * Initialize the flickr Search with provided options
34 init:function( options
) {
35 var baseSearch
= new baseRemoteSearch( options
);
36 for ( var i
in baseSearch
) {
37 if ( typeof this[i
] == 'undefined' ) {
38 this[i
] = baseSearch
[i
];
40 this['parent_' + i
] = baseSearch
[i
];
45 * Gets the Search results setting _loading flag to false once results have been added
47 getSearchResults:function() {
49 js_log( "flickr::getSearchResults" );
50 // call parent (sets loading sate and other setup stuff)
51 this.parent_getSearchResults();
52 // setup the flickr request:
54 'method':'flickr.photos.search',
56 'license':this._license_keys
,
57 'api_key':this.apikey
,
58 'per_page': this.cp
.limit
,
59 'page' : this.cp
.offset
,
60 'text': $j( '#rsd_q' ).val(),
61 'extras' : 'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_m, url_o'
65 'url':this.cp
.api_url
,
66 'jsonCB':'jsoncallback',
68 _this
.addResults( data
);
69 _this
.loading
= false;
73 * Adds Results for a given data response from api query
75 addResults:function( data
) {
77 if ( data
.photos
&& data
.photos
.photo
) {
79 this.num_results
= data
.photos
.total
;
80 if ( this.num_results
> this.cp
.offset
+ this.cp
.limit
) {
81 this.more_results
= true;
83 for ( var resource_id
in data
.photos
.photo
) {
84 var sourceResource
= data
.photos
.photo
[ resource_id
];
85 var rObj
= _this
.getResourceObject( sourceResource
);
86 _this
.resultsObj
[ resource_id
] = rObj
;
91 * Gets an individual resource object from a given source Resource
93 getResourceObject: function( resource
){
96 'titleKey' : resource
.title
+ '.jpg',
97 'resourceKey': resource
.id
,
98 'link' : _this
.dtUrl
+ resource
.pathalias
+ '/' + resource
.id
,
99 'title' : resource
.title
,
100 'thumbwidth' : resource
.width_t
,
101 'thumbheight': resource
.height_t
,
102 'desc' : resource
.title
,
104 'license' : this.rsd
.getLicenceFromUrl( _this
.licenseMap
[ resource
.license
] ),
106 // Assume image/jpeg for flickr response
107 'mime' : 'image/jpeg'
109 // Add all the provided src types that are avaliable
110 rObj
['srcSet'] = { };
111 for ( var i
in _this
._srctypes
) {
112 var st
= _this
._srctypes
[i
];
113 // if resource has a url add it to the srcSet:
114 if ( resource
['url_' + st
] ) {
115 rObj
['srcSet'][st
] = {
116 'h': resource
['height_' + st
],
117 'w': resource
['width_' + st
],
118 'src': resource
['url_' + st
]
120 // Set src to the largest
121 rObj
['src'] = resource
['url_' + st
];
127 * return image transform via callback
129 getImageObj:function( rObj
, size
, callback
) {
131 var skey
= this.getSrcTypeKey( rObj
, size
.width
)
133 'url' : rObj
.srcSet
[ skey
].src
,
134 'width' : rObj
.srcSet
[ skey
].w
,
135 'height' : rObj
.srcSet
[ skey
].h
140 * Gets an image transformation based a SrcTypeKey gennerated by the requested options
142 getImageTransform:function( rObj
, options
) {
143 if ( options
.width
) {
144 return rObj
.srcSet
[ this.getSrcTypeKey( rObj
, options
.width
) ].src
;
146 return rObj
.srcSet
[ _srctypes
[_srctypes
.length
-1] ];
148 getSrcTypeKey:function( rObj
, width
) {
150 if ( rObj
.srcSet
['sq'] )
152 } else if ( width
<= 100 ) {
153 if ( rObj
.srcSet
['t'] )
155 } else if ( width
<= 240 ) {
156 if ( rObj
.srcSet
['s'] )
158 } else if ( width
<= 500 ) {
159 if ( rObj
.srcSet
['m'] )
162 if ( rObj
.srcSet
['o'] )
165 // original was missing return medium or small
166 if ( rObj
.srcSet
['m'] )
168 if ( rObj
.srcSet
['s'] )