forked from dphiffer/flickr-cc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flickr-cc.js
66 lines (52 loc) · 2.18 KB
/
flickr-cc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function($) {
// Create a new Flickr API wrapper object
var flickr = new Flickr('cb2172edd1a978b6c2835d2f8c7c9939');
// If the page was loaded with a 'q' param...
if (location.search.substr(0, 3) == '?q=') {
// Add some user feedback
$('#photos').html('Hang on a sec...');
// We start with location.search, something like ?q=wilson%27s+warbler
var query = location.search.substr(3); // drop the first three chars: wilson%27s+warbler
query = decodeURIComponent(query); // decode the URI encoding: wilson's+warbler
query = query.replace(/\+/g, ' '); // replace all '+' with ' ': wilson's warbler
// Insert query into text input
$('input[name="q"]').val(query);
// Set up a callback function to handle the photo results
var callback = function(rsp) {
// Ok, all ready for photos now
$('#photos').html('');
// Iterate over the results, one photo at a time
$.each(rsp.photos.photo, function(i, photo) {
var src = photo.src('b').replace('http:', '');
var img = '<img class="lazy" data-original="' + src + '" alt="">';
var link = '<a href="' + photo.href() + '">';
var close = '</a>';
$('#photos').append(link + img + close);
});
// Set up the lazyload plugin
$("img.lazy").lazyload({
threshold: 200,
effect: "fadeIn"
});
// Add a link back to the official Flickr search results page
var flickrSearch = 'https://www.flickr.com/search/?tags=' + encodeURIComponent(query) +
'&license=' + encodeURIComponent('7,8,9,10') +
'&sort=relevance';
$('#see-also').html('Check out this search on the <a href="' + flickrSearch + '">Flickr advanced search page</a>');
};
// Search for query we derived from the URL
// See also: https://www.flickr.com/services/api/flickr.photos.search.html
flickr.photos.search({
tags: query,
sort: 'relevance',
license: "7,8,9,10" // All the permissively licensed photos
}, callback);
}
// You can uncomment this and check the JS console to see more details about licenses...
/*flickr.photos.licenses.getInfo({
text: query,
license: "7,8,9,10" // All the permissively licensed photos
}, function(rsp) {
console.log(rsp);
});*/
})(jQuery);