ClarityCafe / Sagiri

Simple, lightweight, and easy to use SauceNAO API wrapper for Node.JS.
MIT License
97 stars 13 forks source link

SauceNAO returning error when searching with image URL #124

Closed digiAlchem closed 4 years ago

digiAlchem commented 5 years ago

Please describe the problem you are having in as much detail as possible:

SyntaxError:` Unexpected token < in JSON at position 3str"                       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                        <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <title>SauceNAO Error</title>
                        <link rel="stylesheet" href="css/saucenao-new.css" media="screen" type="text/css" />
                        </head><body><br />
                        You need an Image! <a href="/index.php">...GO BACK TO START...</a> <br /><br /><br /><br />If an image was provided, it may not be loading properly, or may not be in an acceptable format... The image must be: gif, jpg, png, bmp, svg, or webp... <br /> Try another, or convert your image first.</body></html>"
    at JSON.parse (<anonymous>)
    at ClientRequest.h.request (/home/pi/bots/node/discord/node_modules/bent/src/nodejs.js:105:24)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)

Getting the above error when trying to do a search with an image URL; happens with both await and .then() syntax (I use the latter). Running console.log(sagiriClient) confirms that it's a loaded function.

Enabling DEBUG=sagiri returns the following output before the error shows:

sagiri Requesting possible sources for https://danbooru.donmai.us/data/sample/__link_the_legend_of_zelda_and_1_more_drawn_by_pretty_purin720__sample-64ed3fd651fe4e1ca141383a4b7b0b4c.jpg +16s
sagiri Requesting 5 results from SauceNAO +7ms
sagiri Adding given file as a URL +20ms
sagiri Sending request to SauceNAO +4ms

Include a reproducible code sample here, if possible:


const sagiri = module.parent.require('sagiri');
const { saucenaoToken } = module.parent.require('./botConfig.json');
const sagiriClient = sagiri(saucenaoToken);

...

sagiriClient(imageUrl)
.then((sauceList) => {
    console.log(sauceList);
});

Running console.log(imageUrl) confirms that the input and output URLs match.

Further details:

Ovyerus commented 5 years ago

Able to repro, looks like it might be because we need to manually set the form-data headers, but bent doesn't let us in the request function. I'll look into PR-ing this functionality to bent.

digiAlchem commented 4 years ago

@Ovyerus Looks like the form-data module has a built-in function to get the required headers for sending multipart/form-data requests.

By moving the const declaration to below the form builder, you can pull those headers out and add them to the bent constructor.

Ovyerus commented 4 years ago

Am still going to PR this functionality to bent as I need it elsewhere, but I think it's also probably better to make the request function as soon as possible so I'll put it back in the initial constructor.