-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·95 lines (78 loc) · 1.82 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
function giph (search, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}
if (!options) { options = {} }
if (!options.rating) { options.rating = '' }
const giphy = require('giphy-api')(options.key)
return giphy.random({
tag: search,
rating: options.rating
})
.then(res => {
if (!res || !res.data || !res.data.id) {
throw new Error(`No gifs were found using the query '${search}'.`)
}
return res.data
})
.then(img => {
if (!options.buffer && !options.file) {
return img
}
const request = require('request-promise-native')
return request.get({ url: img.images.original.url, encoding: 'binary' })
.then(buffer => {
img.buffer = Buffer.from(buffer, 'binary')
return img
})
})
.then(img => {
if (options.file) {
const fs = require('fs')
fs.writeFileSync(options.file, img.buffer)
}
return img
})
.then(img => {
if (cb) {
cb(null, img)
}
return img
})
.catch(err => {
if (cb) {
cb(err)
} else {
throw err
}
})
}
if (require.main === module) {
const argv = require('minimist')(process.argv.slice(2))
const copyPaste = require('copy-paste')
const termImg = require('term-img')
var buffer = !argv.nodisplay
giph(argv._.join(' '), {
buffer: buffer,
file: argv.file,
key: argv.key,
rating: argv.rating
})
.then(img => {
if (argv.details) {
console.log(img)
}
if (buffer) {
termImg(img.buffer, () => {})
}
if (!argv.nocopy) {
copyPaste.copy(img.images.original.url)
}
})
.catch(err => {
console.error(err.message)
})
}
module.exports = giph