Skip to content

Commit f194766

Browse files
committed
facebook group
1 parent 01399f1 commit f194766

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

facebook.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*jslint indent: 2, node: true, vars: true, nomen: true */
2+
3+
'use strict';
4+
5+
var fbGroupSync = require('facebook-group-sync');
6+
var models = require('./models');
7+
var Topic = models.Topic;
8+
var User = models.User;
9+
10+
fbGroupSync.setConfig({
11+
clientId: '393020914114844',
12+
clientSecret: '1c7c96dbb4416f37d53a642b6de52892',
13+
groupId: 'node.js.tw',
14+
accessToken: '393020914114844|dIX28XdQz3rrd_GDvafcN3fhz_w', // 可以不給,程式會自己取得
15+
frequency: 1000 // 預設是 3600000 (一小時)
16+
});
17+
18+
function run(user) {
19+
fbGroupSync(function (posts) {
20+
posts.forEach(function (post) {
21+
var topic = new Topic();
22+
var title;
23+
var message;
24+
25+
if (post.message) {
26+
title = post.message.length > 13 ? post.message.substr(13) + '...' : post.message;
27+
message = post.message;
28+
}
29+
30+
if (post.story) {
31+
title = post.story.length > 13 ? post.story.substr(13) + '...' : post.story;
32+
message = post.story;
33+
}
34+
35+
if (post.type === 'link') {
36+
message = '<a href="' + post.link + '">' + post.description + '</a>' + message;
37+
}
38+
39+
topic.title = title;
40+
topic.author_id = user._id;
41+
topic.content = message;
42+
topic.created_at = post.created_time;
43+
topic.update_at = post.update_time;
44+
topic.facebook_id = post.id;
45+
46+
topic.save(function (err, topic) {
47+
if (err) {
48+
throw err;
49+
}
50+
console.log('Save ' + topic._id);
51+
});
52+
});
53+
});
54+
}
55+
56+
User.findOne({loginname: 'fbgroup'}, function (err, user) {
57+
if (err) {
58+
throw err;
59+
}
60+
if (!user) {
61+
user = new User();
62+
user.name = 'FacebookGroup';
63+
user.loginname = 'fbgroup';
64+
user.pass = '=====';
65+
user.profile_image_url = 'http://www.gravatar.com/avatar/884821923470e57e3bebb2959e2ac1c8?size=48';
66+
user.save(function (err, user) {
67+
if (err) {
68+
throw err;
69+
}
70+
run(user);
71+
});
72+
return;
73+
}
74+
run(user);
75+
});
76+
77+

models/topic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var TopicSchema = new Schema({
1919
update_at: { type: Date, default: Date.now },
2020
last_reply: { type: ObjectId },
2121
last_reply_at: { type: Date, default: Date.now },
22-
content_is_html: { type: Boolean }
22+
content_is_html: { type: Boolean },
23+
facebook_id: { type: String }
2324
});
2425

2526
mongoose.model('Topic', TopicSchema);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"nodemailer": "0.3.5",
1515
"data2xml": "0.4.0",
1616
"xss": ">=0.0.2",
17-
"bcrypt ": "*"
17+
"bcrypt ": "*",
18+
"facebook-group-sync": "*"
1819
},
1920
"devDependencies": {
2021
"should": "*",

0 commit comments

Comments
 (0)