forked from AtoraSuunva/booru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
74 lines (63 loc) · 2.22 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
69
70
71
72
73
74
const Booru = require('./dist')
const { BooruError, sites } = require('./dist')
// for ES6:
// import Booru, { search, BooruError, sites } from 'booru'
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}`)
function formatPost(post) {
return {
id: post.id,
postView: post.postView,
fileUrl: post.fileUrl,
score: post.score,
rating: post.rating,
source: post.source,
createdAt: post.createdAt,
tags:
post.tags.length > 5
? [...post.tags.slice(0, 5), `...${post.tags.length - 5} more`]
: post.tags,
}
}
// 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(formatPost(post))
}
})
.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)
}
// 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))