-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-fb-members.js
executable file
·151 lines (137 loc) · 4.66 KB
/
import-fb-members.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
#!/usr/bin/env node
var Parse = require('parse/node').Parse;
var Rx = require('rx');
var program = require('commander');
var Parses = require('./parses');
program
.version('1.0.0')
.option('-p, --production', 'production')
.option('-a, --appId <appId>', 'appId')
.option('-j, --jsKey <jsKey>', 'jsKey')
.option('-m, --masterKey <masterKey>', 'masterKey')
.option('-t, --token <token>', 'facebook access token')
//.option('-d, --debug', 'Show debug messages')
//.option('-i, --info', 'Show info only')
//.option('-vv, --verbose', 'Show more messages')
//.option('--years_ago <YEARS_AGO>', 'sync <YEARS_AGO> today 00:00:00 in user\'s timezone')
//.option('--days <DAYS>', 'sync <DAYS> from <YEARS_AGO>')
//.option('--since <SINCE>', 'A Unix timestamp or strtotime data value that points to the start of the range of time-based data')
//.option('--until <UNTIL>', 'A Unix timestamp or strtotime data value that points to the end of the range of time-based data')
//.option('--location', 'sync only location posts')
.option('-g, --groups <groups>', 'Specifiy group ids')
.option('--url <url>', 'Specifiy parse api url')
.option('-f, --config <config>', 'Specifiy config path')
.option('--dryrun', 'Dryrun')
.parse(process.argv);
var appId = program.appId ? program.appId : process.env.APP_ID;
var jsKey = program.jsKey ? program.jsKey : process.env.JS_KEY;
var masterKey = program.masterKey ? program.masterKey : process.env.MASTER_KEY;
var user = program.user ? program.user : process.env.PARSE_USER;
var url = program.url;
var groups = (program.groups) ? program.groups.split(",") : null;
var token = program.token;
var configPath = program.config;
var config = hasFile(configPath) ? require(configPath) : null;
var configJson;
if (config) {
if (program.production) {
appId = appId ? appId : config.production.appId;
jsKey = jsKey ? jsKey : config.production.jsKey;
masterKey = masterKey ? masterKey : config.production.masterKey;
url = url ? url : config.production.url;
token = token ? token : config.dev.token;
groups = groups ? groups : config.dev.groups;
} else {
appId = appId ? appId : config.dev.appId;
jsKey = jsKey ? jsKey : config.dev.jsKey;
masterKey = masterKey ? masterKey : config.dev.masterKey;
url = url ? url : config.dev.url;
token = token ? token : config.dev.token;
groups = groups ? groups : config.dev.groups;
}
}
if (program.dryrun) {
console.log(configJson);
process.exit();
}
// TODO fatal error and exit
if (!appId) {
console.error('missing appId');
process.exit();
}
if (!jsKey) {
console.error('missing jsKey');
process.exit();
}
if (!masterKey) {
console.error('missing masterKey');
}
if (!token) {
console.error('missing token');
process.exit();
}
/*
if (!groups) {
console.error('missing groups');
process.exit();
}
*/
//if (!url) console.error('missing server url');
console.log(appId);
console.log(jsKey);
console.log(masterKey);
console.log(groups);
console.log(token);
console.log(url);
if (url) Parse.serverURL = url;
Parse.initialize(appId, jsKey, masterKey);
Parse.Cloud.useMasterKey();
//require('es6-promise').polyfill();
//var Fetch = require('isomorphic-fetch');
var RxFacebook = require('rx-facebook');
Rx.Observable.from(groups)
.concatMap(function (group) {
return RxFacebook.Members(group, token)
.concatMap(function (member) {
return getFbUserByFbId(member.id).concatMap(function (fbUser) {
console.log(member.id + ":" + group + ":\"" + member.name + "\"");
fbUser.addUnique("groups", group);
fbUser.set("fbid", member.id);
fbUser.set("name", member.name);
return Parses.save(fbUser); // target, new or update
}).concatMap(function (fbUser) {
fbUser.remove("groups", null);
fbUser.remove("groups", "null");
return Parses.save(fbUser);
}).doOnNext(function (it) {
console.log(it);
});
});
})
.subscribe(function (it) {
//console.log(it);
}, function (e) {
console.log(e);
});
function getFbUserByFbId(fbid) {
var FbUser = Parse.Object.extend("FbUser");
var query = new Parse.Query(FbUser);
query.equalTo("fbid", fbid);
return Rx.Observable.fromPromise(query.first()).catch(function (e) {
return Rx.Observable.just(new FbUser());
}).map(function (user) {
return (user != null) ? user : new FbUser();
//}).concatMap(function (user) { return fetch(user); }).defaultIfEmpty(new FbUser());
}).defaultIfEmpty(new FbUser());
}
function hasFile(f) {
if (!f) return false;
var fs = require('fs');
var b = false;
try {
b = fs.statSync(f).isFile();
} catch (e) {
}
return b;
}
/* vim: set sw=2: */