3969e32b8307bc67387f5049d5c390201d7dd4b1
[lhc/web/wiklou.git] / js2 / mwEmbed / libAddMedia / searchLibs / flickrSearch.js
1 /*
2 * basic flickr search uses flickr jsonp api
3 * http://www.flickr.com/services/api/
4 *
5 * uses the "example api_key" 519b66e3fd8d8080e27a64fe51101e2c
6 * should update with a different "public" key sometime soon
7 * http://www.flickr.com/services/rest/?method=flickr.test.echo&format=json&api_key=519b66e3fd8d8080e27a64fe51101e2c
8 *
9 * we look for licenses from method=flickr.photos.licenses.getInfo
10 * per http://commons.wikimedia.org/wiki/Special:Upload?uselang=fromflickr
11 * we are interested in:
12 * (4) Attribution License
13 * (5) Attribution-ShareAlike License,
14 * (7) No known copyright restrictions,
15 * (8) United States Government Work
16 */
17
18 var flickrSearch = function ( iObj ) {
19 return this.init( iObj );
20 }
21 flickrSearch.prototype = {
22 dtUrl : 'http://www.flickr.com/photos/',
23 // @@todo probably would be good to read the api-key from configuration
24 apikey : '2867787a545cc66c0bce6f2e57aca1d1',
25 // what licence we are interested in
26 _licence_keys: '4,5,7,8',
27 _srctypes: ['t', 'sq', 's', 'm', 'o'],
28 licenceMap: {
29 '4' : 'http://creativecommons.org/licenses/by/3.0/',
30 '5' : 'http://creativecommons.org/licenses/by-sa/3.0/',
31 '7' : 'http://www.flickr.com/commons/usage/',
32 '8' : 'http://www.usa.gov/copyright.shtml'
33 },
34 init:function( iObj ) {
35 // init base class and inherit:
36 var baseSearch = new baseRemoteSearch( iObj );
37 for ( var i in baseSearch ) {
38 if ( typeof this[i] == 'undefined' ) {
39 this[i] = baseSearch[i];
40 } else {
41 this['parent_' + i] = baseSearch[i];
42 }
43 }
44 // inherit the cp settings for
45 },
46 getSearchResults:function() {
47 var _this = this;
48 js_log( "flickr::getSearchResults" );
49 // call parent (sets loading sate and other setup stuff)
50 this.parent_getSearchResults();
51 // setup the flickr request:
52 var reqObj = {
53 'method':'flickr.photos.search',
54 'format':'json',
55 'license':this._licence_keys,
56 'api_key':this.apikey,
57 'per_page': this.cp.limit,
58 'page' : this.cp.offset,
59 'text': $j( '#rsd_q' ).val(),
60 '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'
61 }
62 do_api_req( {
63 'data': reqObj,
64 'url':this.cp.api_url,
65 'jsonCB':'jsoncallback',
66 }, function( data ) {
67 _this.addResults( data );
68 _this.loading = false;
69 } );
70 },
71 addResults:function( data ) {
72 var _this = this;
73 if ( data.photos && data.photos.photo ) {
74 // set result info:
75 this.num_results = data.photos.total;
76 if ( this.num_results > this.cp.offset + this.cp.limit ) {
77 this.more_results = true;
78 }
79 for ( var resource_id in data.photos.photo ) {
80 var resource = data.photos.photo[resource_id];
81
82 var rObj = {
83 'titleKey' : resource.title + '.jpg',
84 'resourceKey': resource.id,
85 'link' : _this.dtUrl + resource.pathalias + '/' + resource.id,
86 'title' : resource.title,
87 'thumbwidth' : resource.width_t,
88 'thumbheight': resource.height_t,
89 'desc' : resource.title,
90 // set the license: (rsd is a pointer to the parent remoteSearchDriver )
91 'license' : this.rsd.getLicenceFromUrl( _this.licenceMap[ resource.license ] ),
92 'pSobj' : _this,
93 // assume image/jpeg for "photos"
94 'mime' : 'image/jpeg'
95 };
96 // set all the provided srcs:
97 rObj['srcSet'] = { };
98 for ( var i in _this._srctypes ) {
99 var st = _this._srctypes[i];
100 // if resource has a url add it to the srcSet:
101 if ( resource['url_' + st] ) {
102 rObj['srcSet'][st] = {
103 'h': resource['height_' + st],
104 'w': resource['width_' + st],
105 'src': resource['url_' + st]
106 }
107 // set src to the largest
108 rObj['src'] = resource['url_' + st];
109 }
110 }
111
112 _this.resultsObj[ resource_id ] = rObj;
113 }
114 }
115 },
116 // return image transform via callback
117 getImageObj:function( rObj, size, callback ) {
118 if ( size.width ) {
119 var skey = this.getSrcTypeKey( rObj, size.width )
120 callback ( {
121 'url' : rObj.srcSet[ skey ].src,
122 'width' : rObj.srcSet[ skey ].w,
123 'height' : rObj.srcSet[ skey ].h
124 } );
125 }
126 },
127 getImageTransform:function( rObj, opt ) {
128 if ( opt.width ) {
129 return rObj.srcSet[ this.getSrcTypeKey( rObj, opt.width ) ].src;
130 }
131 },
132 getSrcTypeKey:function( rObj, width ) {
133 if ( width <= 75 ) {
134 if ( rObj.srcSet['sq'] )
135 return 'sq';
136 } else if ( width <= 100 ) {
137 if ( rObj.srcSet['t'] )
138 return 't';
139 } else if ( width <= 240 ) {
140 if ( rObj.srcSet['s'] )
141 return 's';
142 } else if ( width <= 500 ) {
143 if ( rObj.srcSet['m'] )
144 return 'm';
145 } else {
146 if ( rObj.srcSet['o'] )
147 return 'o';
148 }
149 // original was missing return medium or small
150 if ( rObj.srcSet['m'] )
151 return 'm';
152 if ( rObj.srcSet['s'] )
153 return 's';
154
155 }
156 }