AtoraSuunva / booru

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

blacklist tags #76

Closed dimactavishy closed 2 years ago

dimactavishy commented 2 years ago

just a simple question is there a feature to blacklist tags on this package? if not, i already have my own way to block certain words on my discord bot code with just using if(message.content.includes(blacklist)). but this method couldn't blacklist more than one tags without making the code incredibly messy.

edit: even if i blacklisted a word using that code, it could still return image tagged with something that i want to blacklist if the tags are specific enough.

AtoraSuunva commented 2 years ago

You can use SearchResults#blacklist:

const posts = await booru.search('sb', ['cat'], { limit: 3 })
// Remove all posts from search results that have "dress" or "clouds" in their tags
const filtered = posts.blacklist(['dress', 'clouds'])
console.log(filtered[0])

Since this filters tags on your end (and not the API), you can end up with no results for 1 page and might need to search the next page:

const posts = await booru.search('sb', ['cat'], { limit: 3, page: 2 })

I'll leave it up to you to decide if you just want to search 1 page only, or if you want to paginate until you find a result for the user (but you'll need to make sure that you don't get ratelimited or fetch pages forever)


Most APIs will allow you to remove a tag from search results using -tag, but most APIs have a strict limit on max number of tags.

So blacklisting tags is done in the package, not the API, so you can blacklist more than 3 tags.


You can check out the implementation if you want to know how you could've implemented it in your own code (or if you need something more specific)

You need to compare the array of tags you want blacklisted with the array of tags of each post

If there's 1+ matches then you can filter out that post

dimactavishy commented 2 years ago

Thanks, the tag that i desire to blacklist wasn't all that common anyways so i don't think it would return a "no result" message.

I'm sorry if this is out of the topic, but sometimes the embed for my bot doesn't show the image result (fileURL). Fortunately, i put a postView so i can still see the result and i figure that it's probably too high-res for Discord to put in on an embed. Is there a way to resize the output image? Or do i have to use my own custom code for it?

Edit: does SearchResults#blacklist works on promise or does it only work on async/await? And if it does, where to put it?

AtoraSuunva commented 2 years ago

You might be able to use Post#sampleUrl or Post#previewUrl but some Boorus don't send a medium/small preview of the image.

I know some file types and I think images over 10MB don't embed on Discord? If you have some examples I could check them out.

If the booru doesn't provide sample/preview sizes, then you'd have to resize it yourself, booru doesn't provide any image-editing code


Edit: does SearchResults#blacklist works on promise or does it only work on async/await? And if it does, where to put it?

Booru.search returns a Promise that resolves with SearchResults. async/await is just another way to work with promises instead of .then()

Or in other words:

booru.search('sb', ['cat'])
  .then(posts => {
    // "posts" is an instance of "SearchResults"
    const filtered = posts.blacklist(['dog'])
  })

is functionally the same as

const posts = await booru.search('sb', ['cat'])
// "posts" is an instance of "SearchResults"
const filtered = posts.blacklist(['dog'])

(await can only be used inside an async function, you can check out the MDN documentation for more info)

dimactavishy commented 2 years ago

Thanks, i already figured out how to blacklist.

You might be able to use Post#sampleUrl or Post#previewUrl but some Boorus don't send a medium/small preview of the image. I know some file types and I think images over 10MB don't embed on Discord? If you have some examples I could check them out. If the booru doesn't provide sample/preview sizes, then you'd have to resize it yourself, booru doesn't provide any image-editing code

Then what about videos? I don't think it is possible to embed a video. Or is it possible to detect if the fileURL is a video so it would send a custom message instead?

edit: the bot still returns an image/gif with the blacklisted tag after sending a "post not found" message.

AtoraSuunva commented 2 years ago

Videos can't be embedded inside message embeds (but gifs can)

A video link (.mp4/.webm) can be embedded if you post the URL:

image

You'd have to write your own code (or use another library) to detect videos, on my own bot I have an array of embeddable types and just check that

edit: the bot still returns an image/gif with the blacklisted tag after sending a "post not found" message.

You might have to check your bot's logic to make sure you aren't continuing after you post the "post not found" message

dimactavishy commented 2 years ago

Videos can't be embedded inside message embeds (but gifs can)

You'd have to write your own code (or use another library) to detect videos, on my own bot I have an array of embeddable types and just check that

Thanks for giving me a reference, i've made it so my bot sends a separate non-embed message if the file is over 10MB/a video.

However, it turns out that the embed limit was not actually 10MB and some images over 10MB still got embedded. I think it's actually around 50 MB (maximum for nitro classic users) since my bot embeds a 36MB file at some point. Don't know if it will consistently do so though, since it only returned a GIF above 10MB a few times.

BTW, sampleUrl freezes GIFs and videos, but that's from the booru site and not this package.

I also removed the blacklist and just added a if (message.content.includes(['forbidden', 'words']) since it's not like my friends will figure out a way to go around this anyway.

Much thanks for the help.