Created
October 23, 2017 18:48
-
-
Save ratacibernetica/df045450c7620d7f280378d7b9f9209e to your computer and use it in GitHub Desktop.
Image searcher using Bing image Search API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="prd-gallery"> | |
<div class="title"></div> | |
<div class="contents"></div> | |
</div> | |
<script> | |
//PoC only, remove before final version | |
const loadImagesFromTheWeb = (q) => { | |
if (q) { | |
$.ajax({ | |
'url': 'https://api.cognitive.microsoft.com/bing/v5.0/images/search', | |
'data': { | |
q, | |
mkt: 'en' | |
}, | |
'beforeSend': xhr => { | |
xhr.setRequestHeader('Ocp-Apim-Subscription-Key', 'your-hash-id'); | |
} | |
}).done(response => { | |
const images = response.value.reduce((carry, item) => { | |
carry.push(item.thumbnailUrl); | |
return carry; | |
}, []); | |
$('.title', '.prd-gallery').html(`Web results for "${q}"`); | |
showImages(images); | |
}); | |
} | |
} | |
const showImages = images => { | |
// language=JQuery-CSS | |
$('.contents', '.prd-gallery').html(''); | |
images.map(url => { | |
$('.contents', '.prd-gallery').append(`<img src='${url}'>`); | |
}); | |
} | |
<script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment