benzi11
benzi11

Reputation: 3

How to set options for the bing image search api

I'm currently working on a school project that uses bing image search api. My goal is to get a profile picture for every starwars character (as accurately as possible). I dont understand why, but the api doesnt seem to find results with accents on charaters.

For instance, in the code below, when searchTerm = "Han Solo" everything works find but when searchTerm = "Dormé" the API doesn't return any picture.

The wierd thing is if I do the same search in bing directly, it finds many pictures, but I get none from the API.

//server.js
app.get('/getPortrait/*', (req, res) => {  
    const serviceKey = "MY_KEY";

    let searchTerm = req.url.split('/').pop(); 
    // when searchTerme= "han solo", it works fine
    // when searchTerme= "Dormé", it works but returns no image in the response

    let credentials = new CognitiveServicesCredentials(serviceKey);
    let imageSearchClient = new Search.ImageSearchClient(credentials);

    let resultURL;

    const sendQuery = async () => {
        return await imageSearchClient.imagesOperations.search(searchTerm);
    };
    sendQuery().then(imageResults =>{
        console.debug(imageResults)
        if (imageResults == null || imageResults.value.length == 0) {
            console.error("No image results were found.");
            res.send(defaultPic);
        }
        else {
            resultURL = imageResults.value[0].contentUrl;
            console.log(resultURL);
            res.send(resultURL);
       }
    }).catch(err => {
        console.error(err)
        res.send(defaultPic);
    });
});

Is there a way configurate the search to accept all types of characters ?

Here are the results I get for theses queries :

\\searchTerm = "Dormé"
Object {_type: "Images", value: Array(0)}
_type:"Images"
value:Array(0) []
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
\\searchTerm = "han solo"
Object {_type: "Images", readLink: "https://api.cognitive.microsoft.com/api/v7/images/…", webSearchUrl: "https://www.bing.com/images/search?q=han%20solo&FO…", totalEstimatedMatches: 868, nextOffset: 43, …}
nextOffset:43
readLink:"https://api.cognitive.microsoft.com/api/v7/images/search?q=han%20solo"
totalEstimatedMatches:868
value:Array(35) [Object, Object, Object, …]
webSearchUrl:"https://www.bing.com/images/search?q=han%20solo&FORM=OIIARP"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
https://www.starwarsnewsnet.com/wp-content/uploads/2017/01/Alden-Ehrenreich-as-Han-Solo-4.jpg

Thank you for your help :)

Upvotes: 0

Views: 479

Answers (1)

Pirate Briggs
Pirate Briggs

Reputation: 186

The problem isn't related to Bing Image search directly, unfortunately you've opened a lovely can of worms surrounding character encoding in node.js (and the web in general)

To demonstrate the problem, first write a simpler version of your method:

app.get('/getPortrait/*', (req, res) => {  
    const serviceKey = "MY_KEY";

    let searchTerm = req.url.split('/').pop(); 
    // when searchTerme= "han solo", it works fine
    // when searchTerme= "Dormé", it works but returns no image in the response

    // will return Dorm%C3%A9
    res.send(searchTerm);
});

Dorm%C3%A9 is the URL-encoded version of Dormé that your browser automatically generated when you typed it into the url bar. You can verify this using an online url-decode site such as: https://urldecode.org/?text=Dorm%25C3%25A9&mode=decode

So, you need to decode this value in your code using the decodeURI function.

const sendQuery = async () => {
    return await imageSearchClient.imagesOperations.search(decodeURI(searchTerm));
};

And this now requesting http://localhost:3000/getPortrait/Dorm%C3%A9 now returns the following:

http://vignette2.wikia.nocookie.net/starwars/images/1/18/Dormesenate.jpg/revision/latest?cb=20070506233854

Upvotes: 0

Related Questions