-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
68 lines (55 loc) · 2.2 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Booru = require('./dist')
const { BooruError, sites } = require('./dist')
// for ES6:
// import Booru, { search, BooruError, sites } from 'booru'
// Use you own cors proxy in browser case
// globalThis.BOORU_FETCH_PROXY = u => `https://cors.example.com/${u}`
const argTags = process.argv.slice(3)
const site = process.argv[2] || 'sb'
const tags = process.argv[2] ? argTags : ['cat']
const searchUrl = Booru.forSite(site).getSearchUrl({ tags, limit: 1 })
console.log(`Searching with url: ${searchUrl}`)
// Search with promises
Booru.search(site, tags, { limit: 1, random: false })
.then((posts) => {
if (posts.length === 0) {
console.log('No images found.')
}
console.log(`Found ${posts.length} image${posts.length === 1 ? '' : 's'}.`)
for (let post of posts) {
console.log(post.fileUrl)
}
})
.catch((err) => {
if (err instanceof BooruError) {
// It's a custom error thrown by the package
// Typically results from errors the boorus returns, eg. "too many tags"
console.error(err)
} else {
// This means something pretty bad happened
console.error(err)
}
})
// Search with async/await
async function booruSearch(site, tags, limit = 1, random = true) {
const posts = await Booru.search(site, tags, { limit, random })
return console.log(posts[0].fileUrl)
}
// Search with credentials
async function booruSearch(site, tags, limit = 1) {
const posts = await Booru.search(site, tags, { limit, credentials: { query: '&api_key=xxx&user_id=1' } })
return console.log(posts[0].fileUrl)
}
// 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.forSite('safebooru')`
async function booruClassSearch(site, tags, limit = 1, random = true) {
const myBooru = Booru.forSite(site)
const posts = await myBooru.search(tags, { limit, random })
return console.log(posts[0].fileUrl)
}
// You can also check the sites and the options for each
// console.log(Booru.sites)
// Or just the site URLs
// console.log(Object.keys(sites))