forked from bcomnes/twitter-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
109 lines (102 loc) · 3.03 KB
/
cli.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
99
100
101
102
103
104
105
106
107
108
109
var Twit = require('twit')
var twitterVideo = require('./index')
var appname = 'twitter-video'
var appCfg = require('rc')(appname, {})
var inquirer = require('inquirer')
var allowedOptions = [
{
name: 'config',
boolean: true,
help: 'configure your API keys'
},
{
name: 'video',
abbr: 'v',
help: "path to mp4 file e.g. '~/myVideo.mp4' (requires -s)"
},
{
name: 'status',
abbr: 's',
help: "twitter status text e.g. 'this is a tweet yolo' (requires -v)"
}
]
var cliOpts = require('cliclopts')(allowedOptions)
var argv = require('minimist')(process.argv.slice(2), cliOpts.options())
var fs = require('fs')
var osenv = require('osenv')
var path = require('path')
var chalk = require('chalk')
function main () {
if (argv.config) {
var configPath = path.join(osenv.home(), '.config', appname)
console.log('Generate your oAuth keys at https://apps.twitter.com')
console.log('')
var cfgQ = [
{type: 'input',
name: 'consumer_key',
message: 'Consumer Key'
},
{
type: 'password',
name: 'consumer_secret',
message: 'Consumer Secret'
},
{
type: 'input',
name: 'access_token',
message: 'Access Token'
},
{
type: 'password',
name: 'access_token_secret',
message: 'Access Token Secret'
}
]
inquirer.prompt(cfgQ, function (answers) {
fs.writeFile(configPath, JSON.stringify(answers), function (err) {
if (err) return console.error(err)
console.log(chalk.bgRed.bold('Attention:'), 'Secret API keys saved to', configPath)
console.log(chalk.bgGreen.bold('Ready to Send!'))
})
})
} else if (argv.video && argv.status && configIsGood(appCfg)) {
var oauth = {
consumer_key: appCfg.consumer_key,
consumer_secret: appCfg.consumer_secret,
access_token: appCfg.access_token,
access_token_secret: appCfg.access_token_secret
}
var T = new Twit(oauth)
var PATH = argv.video
var status = argv.status
twitterVideo.fromFile(PATH, oauth, function (err, media_id) {
if (err) throw err
var params = {status: status, media_ids: [ media_id ]}
T.post('statuses/update', params, function post (err, data, res) {
if (err) return console.error(err)
console.log('Post created at: https://twitter.com/' + data.user.screen_name + '/status/' + data.id_str)
process.exit()
})
})
} else if (argv.status && argv.status && !configIsGood(appCfg)) {
console.log('You need to configure your API keys first')
printHelp()
} else {
printHelp()
}
}
function configIsGood (appCfg) {
return (appCfg &&
appCfg.hasOwnProperty('consumer_key') &&
appCfg.hasOwnProperty('consumer_secret') &&
appCfg.hasOwnProperty('access_token') &&
appCfg.hasOwnProperty('access_token_secret')
)
}
function printHelp () {
console.log('Uploads .mp4 video to twitter with a status update')
console.log('Usage: %s [options]', appname)
cliOpts.print()
process.exit()
}
module.exports = main