|
| 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 | + |
0 commit comments