-
Notifications
You must be signed in to change notification settings - Fork 4
/
facebook-cli.js
196 lines (167 loc) · 4.8 KB
/
facebook-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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander'),
prompt = require('prompt'),
fs = require('node-fs'),
fb = require('./facebook'),
nconf = require('nconf'),
httpget = require('http-get'),
async = require('async'),
progress = require('progress'),
auth = require('./auth');
// Config file
var configFile = __dirname + '/config.json';
/**
* Configuration file settings
*/
nconf.file({ file: configFile });
// TODO Get long live access_token
var init = function(callback,permissions){
var appId = nconf.get('appId');
var secret = nconf.get('secret');
if (appId && secret) {
auth({
appId: appId,
secret: secret,
fb: fb.init(appId,secret),
permissions: permissions
},function(){
callback();
});
} else {
console.log("\nPlease run 'fb config' at first. \n");
process.exit(1);
}
};
/**
* Prompt settings
*/
prompt.message = "";
prompt.delimiter = "";
/**
* Program
*/
// TODO version number should come from package.json
program
.version('0.0.1');
// Config
program
.command('config')
.description('Configure facebook app details')
.action(function(){
var schema = {
properties: {
appId: {
description: "Facebook app ID",
message: 'appId should be numbers.',
required: true
},
secret: {
description: "Facebook app secret",
required: true
}
}
};
// TODO let them know if config object already exists
console.log("\nPlease enter your facebook app details \n".grey);
prompt.start();
prompt.get(schema,function (err,result) {
// TODO validate via remote call
nconf.set('appId',result.appId);
nconf.set('secret',result.secret);
// Save the configuration object
nconf.save();
// Init facebook
init();
});
});
// $ fb me
program
.command('me')
.description('Get info about current user')
.action(function(){
var action = function(){
fb.me(function(data){
console.log();
console.log('You are '.grey + data.name.bold.cyan + '. Your ID is '.grey + data.id.bold.cyan);
console.log(('Link to your profile: ' + ('https://facebook.com/' + data.username).underline).grey);
console.log();
});
};
init(action);
});
// $ fb post "{message}"
program
.command('post <msg>')
.description('Post status update on your wall')
.action(function(msg){
var action = function(){
// TODO msg without " quotes. Also check all other calls.
fb.post(msg,function(data){
console.log();
console.log('Status update has been posted.'.green);
console.log(('Here is the link: ' + ('https://facebook.com/' + data.id).underline).grey);
console.log();
});
};
init(action);
});
// $ fb download {user}
program
.command('download:albums <user> [path]')
.description('Download user photo albums')
.action(function(user, path){
// If path has not been specified, current folder will be used.
if (!path) path = '.';
// Check if folder exists. If not, creates it.
// if (!fs.existsSync(path)) {
// fs.mkdirSync(path,0777,true);
// }
var permissions = ['user_photos','friends_photos'];
var action = function(){
fb.getAlbumsWithPhotos(user,function(albums){
async.eachSeries(albums,function(album, albumCallback){
// Progress bar properties
var bar = new progress('[:bar :percent] Downloaded :current/:total',{
total: album.photos.length,
complete: '=',
incomplete: ' ',
width: 50,
clear: true
});
// Create album folder
fs.mkdirSync(path + '/' + album.name);
// Download images
async.eachLimit(album.photos, 15, function(photo,photoCallback){
// Download and save photo
// TODO real extension
httpget.get(photo.source, path + '/' + album.name + '/' + photo.id + ".jpg", function(err) {
// TODO better output
if (err)
console.log(err);
// TODO sometimes hangs here
bar.tick();
photoCallback();
});
}, function(){
console.log(album.name.cyan + ' downloaded successfully.');
albumCallback();
})
}, function(){
if (path === '.') path = 'current directory.';
console.log();
console.log('Well Done!');
console.log('All photos have been saved to ' + path);
console.log();
})
});
};
init(action,permissions);
});
program
.parse(process.argv);
// TODO show help if wrong command name given
// Empty call
if (!program.args.length) program.help();