Skip to content

Commit

Permalink
Add Booru.forSite and change from new Booru (see #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlasTheBot committed Feb 5, 2020
1 parent 7e15e00 commit 53b5335
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ async function booruSearch(site, tags, limit = 1, random = true) {
// Create an instance of a booru to use yourself
// This allows you to create a booru with certain credentials/settings and reuse it
// Internally, `Booru.search` just creates boorus and caches them
// Ex: `Booru('safebooru')`
// Ex: `Booru.forSite('safebooru')`
async function booruClassSearch(site, tags, limit = 1, random = true) {
const myBooru = Booru(site)
const myBooru = Booru.forSite(site)
const posts = await myBooru.search(tags, {limit, random})

return console.log(posts[0].fileUrl)
Expand Down
18 changes: 13 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

## Features

- Able to search 17 different boorus (check [sites.json](./src/sites.json))
- Also alias support so you can be lazy (`sb` for `safebooru.org`)
- Promises because they're magical
- Search 17 different boorus (check [sites.json](./src/sites.json))
- Normalizes all received data into `Post` objects that are consistent no matter which booru you use
- Access to the raw data received from the booru as well (transformed from XML to JSON, if applicable)
- Alias support for boorus (`sb` for `safebooru.org`)
- Promises
- Types (using Typescript)
- Choose the amount of images to get
- Random support for all sites, using `order:random` on sites that support it and using a bit of magic on sites that don't
- Random support for all sites, using `order:random` on sites that support it and using custom code on those that don't
- Coming soon(-ish): Support for more than just searching

---
Expand All @@ -30,11 +33,16 @@ yarn add booru
```js
const Booru = require('booru')

Booru.search('safebooru', ['glaceon'], {limit: 3, random: true})
Booru.search('safebooru', ['glaceon'], { limit: 3, random: true })
.then(posts => {
for (let post of posts)
console.log(post.fileUrl, post.postView)
})

// or (using alias support and creating boorus)
const sb = Booru.forSite('sb')

sb.search(['cat', 'dog'], { limit: 2 })
```

See [example.js](./example.js) for more examples
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function booruFrom(booruSite: Site, credentials?: any): Booru {
* @param {*} credentials The credentials to use on this booru
* @return {Booru} A booru to use
*/
export default function(site: string, credentials: any = null): Booru {
function booruForSite(site: string, credentials: any = null): Booru {
const rSite = resolveSite(site)

if (!rSite) throw new BooruError('Site not supported')
Expand All @@ -49,6 +49,9 @@ export default function(site: string, credentials: any = null): Booru {
return booruFrom(booruSite, credentials)
}

export { booruForSite as forSite }
export default booruForSite

/**
* Searches a site for images with tags and returns the results
* @param {String} site The site to search
Expand Down

0 comments on commit 53b5335

Please sign in to comment.