AtoraSuunva / booru

Searches boorus for images using some js magic
https://www.npmjs.com/package/booru
MIT License
75 stars 18 forks source link

Received HTTP 404 from booru? #74

Closed dimactavishy closed 2 years ago

dimactavishy commented 2 years ago

Here's what the log says:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

BooruError: Received HTTP 404 from booru: '{}'
    at Booru.doSearchRequest (C:\Users\dimas\Desktop\dimacbot\node_modules\booru\dist\boorus\Booru.js:1:1966)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Booru.search (C:\Users\dimas\Desktop\dimacbot\node_modules\booru\dist\boorus\Booru.js:1:841)

BTW, it's a Discord bot on a local machine. So i put this on the code instead:

const Booru = require('booru')

module.exports = {
    info: {
        name: "booru",
        description: "booru image scraper",
        cooldown: 30,
    },
    async execute(client, message, args, Discord) {
        const image_query = args.join(' ');
        if (!image_query) {
            message.channel.send("i'm very sorry, but i cannot find what you are looking for.")
        }
        Booru.search('yandere', image_query, { limit: 1, random: true })
        const booruEmbed = new Discord.MessageEmbed()
        .setColor('#990000')
        .setTitle('You want me to look for an image?')
        .setDescription("Here's what i have found according to what you have specified. I hope this result satisfies you.")
        .setThumbnail('https://media.discordapp.net/attachments/898563395807232061/899534056356724756/sketch-1634535999710.png?width=499&height=499')
        .setImage(Booru.search.url)
        .setFooter('Egg-Shaped Battle Maid', 'https://images-ext-2.discordapp.net/external/l7-PY5Kkvta4_p-sOE0ftwQCmJ9iAe72eMPSTczuWi0/%3Fsize%3D512/https/cdn.discordapp.com/avatars/897674562265817088/e36ef03370367a4b3cd51b864e9df392.png?width=499&height=499');

        message.channel.send(booruEmbed);
    }
} 

If it was impossible for Discord bots hosted locally to do this to begin with, please tell me.

AtoraSuunva commented 2 years ago

Booru works locally, what tags/args are you using when you get the 404 error?

Also Booru.search.url will always return undefined, you should check out the example You can also pass in an array for image_query and booru will handle formatting it for you

dimactavishy commented 2 years ago

Booru works locally, what tags/args are you using when you get the 404 error?

Also Booru.search.url will always return undefined, you should check out the example You can also pass in an array for image_query and booru will handle formatting it for you

To be honest, i am not experienced in npmjs in any way. About what tags am i using, i used image_query so anybody using the bot can specify their desired tags, if that's what you meant. As you have advised, i changed Booru.search.url to post.fileUrl and it says ReferenceError: posts is not defined. I might put the wrong thing in the .setImage() because i'm not really bright in this field. I would be glad if you would ELI5 for me.

Edit: it would seem that you have posted a Booru Discord bot on GitHub too. Is it possible for me to integrate its booru features to my bot?

AtoraSuunva commented 2 years ago

About what tags am i using, i used image_query so anybody using the bot can specify their desired tags, if that's what you meant.

Which tags are you searching for when the error happens? Does it happen for any tag(s) you use, or just some? You can also just pass in args to search Booru.search('yandere', args, { limit: 1, random: true }), booru accepts an array of tags

As you have advised, i changed Booru.search.url to post.fileUrl and it says ReferenceError: posts is not defined. I might put the wrong thing in the .setImage() because i'm not really bright in this field. I would be glad if you would ELI5 for me.

Booru.search returns a Promise, you need to either await it inside an async function, or attach a callback using .then(result => {})

Since you already have an async function (async execute()), you can just await it:

const posts = await Booru.search('yandere', args, { limit: 1, random: true })

Edit: it would seem that you have posted a Booru Discord bot on GitHub too. Is it possible for me to integrate its booru features to my bot?

You can, although that bot is written for a custom framework so you might have to adapt the code to work

dimactavishy commented 2 years ago

Which tags are you searching for when the error happens? Does it happen for any tag(s) you use, or just some?

i tried different tags and it still says BooruError: Received HTTP 404 from booru: '{}' in the console.

Booru.search returns a Promise, you need to either await it inside an async function, or attach a callback using .then(result => {}) Since you already have an async function (async execute()), you can just await it:

same thing, it still says the same HTTP 404 error. Even if i put post.fileUrl in the .setImage().

Edit: It seems that Booru-Discord is beyond my skills and i can't integrate it's features to my bot.

AtoraSuunva commented 2 years ago

i tried different tags and it still says BooruError: Received HTTP 404 from booru: '{}' in the console.

Any specific tags? Every tag I've tried on yandere works

same thing, it still says the same HTTP 404 error. Even if i put post.fileUrl in the .setImage().

HTTP 404 isn't related to the file url; somehow booru is generating an incorrect URL when trying to search that gives a 404 or somehow you can't access the booru, I'm not sure which

dimactavishy commented 2 years ago

Any specific tags? Every tag I've tried on yandere works

Unless i put the tags wrong, every tag i've tried doesn't work. I don't want to try out every single one available though, so i've come with the conclusion that it doesn't work.

HTTP 404 isn't related to the file url; somehow booru is generating an incorrect URL when trying to search that gives a 404 or somehow you can't access the booru, I'm not sure which

I can still open the booru page i desired in Edge and Chrome, and this HTTP 404 error appears on both danbooru and yande.re, so it's probably not because of the booru site.

AtoraSuunva commented 2 years ago

If you have some example code (including a booru + tag(s) that cause the 404) and the version of booru that you're using, I could take a look.

But otherwise, unless I can reproduce the bug I have no idea what's wrong

dimactavishy commented 2 years ago

Sorry for the late reply, i got schoolwork and can't work on the bot for the past few days.

If you have some example code

Here's the code:

const Booru = require('booru')

module.exports = {
    info: {
        name: "booru",
        description: "booru image scraper",
        cooldown: 30,
    },
    async execute(client, message, args, Discord) {
        const image_query = args.join(' ');
        if (!image_query) {
            message.channel.send("i'm very sorry, but i cannot find what you are looking for.")
        }
        const posts = await Booru.search('yandere', image_query, { limit: 1, random: true })
        const booruEmbed = new Discord.MessageEmbed()
        .setColor('#990000')
        .setTitle('You want me to look for an image?')
        .setDescription("Here's what i have found according to what you have specified. I hope this result satisfies you.")
        .setThumbnail('links are long so i removed it here')
        .setImage(post.fileUrl)
        .setFooter('links are long so i removed it here');

        message.channel.send(booruEmbed);

(including a booru + tag(s) that cause the 404) and the version of booru that you're using, I could take a look.

for the tags i used image_query so i can just go !booru in the channel. I use yande.re for now. for the tags which i am using, i use the tags that appears on the front page like hololive,genshin_impact, azur_lane, nijisanji, and others that appeared on the front page. All of which returned the same error;

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

BooruError: Received HTTP 404 from booru: '{}'
    at Booru.doSearchRequest (C:\Users\dimas\Desktop\dimacbot\node_modules\booru\dist\boorus\Booru.js:1:1966)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Booru.search (C:\Users\dimas\Desktop\dimacbot\node_modules\booru\dist\boorus\Booru.js:1:841)
    at async Object.execute (C:\Users\dimas\Desktop\dimacbot\commands\booru.js:14:23)

But, there is one exception which is vocaloid, which returns:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

FetchError: request to https://yande.re/post.json?tags=vocaloid+order:random&limit=1&page=0 failed, reason: connect ETIMEDOUT 36.86.63.185:443   
    at ClientRequest.<anonymous> (C:\Users\dimas\Desktop\dimacbot\node_modules\booru\node_modules\node-fetch\lib\index.js:1483:11)
    at ClientRequest.emit (node:events:390:28)
    at ClientRequest.emit (node:domain:475:12)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:390:28)
    at TLSSocket.emit (node:domain:475:12)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {

But it only returns this error the first time i used this tag, and when i tried it again it shows the same errors as the others.

Could it be that the reason for these errors are actually because of a flaw in the package instead of the code?

Edit: formatting

AtoraSuunva commented 2 years ago

Here's the code:

I meant a minimal example, like:

const booru = require('booru')
booru.search('yandere', ['hololive'], { limit: 1, random: true })
  .then(posts => console.log(posts[0].fileUrl))

I can't reproduce any 404s (using booru@2.4.0) with any of the tags you listed, can you access https://yande.re/post.json?tags=vocaloid+order:random&limit=1&page=0 in your web browser?

image

dimactavishy commented 2 years ago

I can't reproduce any 404s (using booru@2.4.0) with any of the tags you listed, can you access https://yande.re/post.json?tags=vocaloid+order:random&limit=1&page=0 in your web browser?

Finally, i could figure out what's wrong with this. yande.re and danbooru simply does not work for some reason and will always return a 404. Rule34 and gelbooru for some reason always returns "No images found." but safebooru works with no problem. I am not trying to make a NSFW command, so this result already satisfies me. Thanks for being patience and always helping me even though i'm a rookie with no experience lol.

AtoraSuunva commented 2 years ago

I'm still not sure why you're getting 404s I can't reproduce, but I'll mark this as closed I guess