-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·98 lines (87 loc) · 2.61 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
96
97
98
#!/usr/bin/env node
/* eslint camelcase:off */
const Twitter = require('twitter')
const chalk = require('chalk')
const yargs = require('yargs')
const banner = require('./banner')
const { getTweets, pickRandom, printTweets } = require('./lib')
// print the lovely tool banner
console.log(chalk.magenta(banner.ascii))
console.log(chalk.grey(banner.subtitle))
const args = yargs
.usage('$0 [options]')
.option('consumer-key', {
alias: 'ck',
describe:
'Your twitter API consumer key (can be specified also throung environment variable TWITTER_CONSUMER_KEY)',
default: process.env.TWITTER_CONSUMER_KEY
})
.option('consumer-secret', {
alias: 'cs',
describe:
'Your twitter API consumer secret (can be specified also throung environment variable TWITTER_CONSUMER_SECRET)',
default: process.env.TWITTER_CONSUMER_SECRET
})
.option('access-token-key', {
alias: 'atk',
describe:
'Your twitter API access token key (can be specified also throung environment variable TWITTER_ACCESS_TOKEN_KEY)',
default: process.env.TWITTER_ACCESS_TOKEN_KEY
})
.option('access-token-secret', {
alias: 'ats',
describe:
'Your twitter API access token secret (can be specified also throung environment variable TWITTER_ACCESS_TOKEN_SECRET)',
default: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
.option('keywords', {
alias: 'k',
describe: 'The keywords to use to search for tweets',
default: '#twaffle'
})
.option('winners', {
alias: 'w',
describe: 'The number of winners',
default: 1
})
.demandOption(
[
'consumer-key',
'consumer-secret',
'access-token-key',
'access-token-secret'
],
'Please provide all mandatory arguments to work with this tool'
).argv
const main = async () => {
try {
const keywords = args.keywords
const numTweets = args.winners
const {
consumerKey,
consumerSecret,
accessTokenKey,
accessTokenSecret
} = args
const client = new Twitter({
consumer_key: consumerKey,
consumer_secret: consumerSecret,
access_token_key: accessTokenKey,
access_token_secret: accessTokenSecret
})
console.log(
`\n🙋♀️ Searching for tweets with "${chalk.green(
keywords
)}". Please hold...`
)
const tweets = await getTweets(client, keywords)
console.log(`👌 Found ${tweets.length} tweets. Sweet!`)
console.log(`🙀 Extracting ${chalk.green(numTweets)} random tweets...\n`)
printTweets(pickRandom(tweets, numTweets))
process.exit(0)
} catch (e) {
console.error(e)
process.exit(1)
}
}
main()