mgebbe@19: # Copyright (C) 2014 by Intevation GmbH mgebbe@19: # Author: Mathias Gebbe mgebbe@19: # mgebbe@19: # This file is Free Software under the Apache License, Version 2.0; mgebbe@19: # and comes with NO WARRANTY! mgebbe@19: # See the documentation coming with pumpbridge for details. mgebbe@19: mgebbe@19: _ = require("underscore") mgebbe@19: async = require("async") mgebbe@19: Facebook = require("./facebook") mgebbe@19: Usermap = require("./usermap") mgebbe@19: Google = require("./google") mgebbe@19: Config = require ("./config") mgebbe@19: config = Config.config mgebbe@19: Twitter = require("./twitter")(config) mgebbe@19: mgebbe@19: databank = require("databank") mgebbe@19: Databank = databank.Databank mgebbe@19: DatabankObject = databank.DatabankObject mgebbe@19: mgebbe@19: db = Databank.get(config.driver, config.params) mgebbe@19: DatabankObject.bank = db mgebbe@19: mgebbe@19: syncFromESN = () -> mgebbe@19: console.log 'syncFromESN' mgebbe@19: mgebbe@19: async.waterfall [ mgebbe@19: (callback) -> mgebbe@19: db.connect(config.params, callback) mgebbe@19: (callback) -> mgebbe@19: #### mgebbe@19: # Facebook mgebbe@19: mgebbe@19: try mgebbe@19: Usermap.scan ((user) -> mgebbe@19: if user.id.indexOf('@facebook') isnt -1 mgebbe@19: console.log "start sync for facebook user" mgebbe@19: Facebook.sync(user) mgebbe@19: ), (err) -> mgebbe@19: catch err mgebbe@19: console.log 'Error!' + err mgebbe@19: mgebbe@19: # Twitter mgebbe@19: try mgebbe@19: Usermap.scan ((user) -> mgebbe@19: if user.id.indexOf('@twitter') isnt -1 mgebbe@19: console.log "start sync for twitter user" mgebbe@19: Twitter.sync(user) mgebbe@19: ), (err) -> mgebbe@19: catch err mgebbe@19: console.log 'Error!' + err mgebbe@19: mgebbe@19: mgebbe@19: # Google mgebbe@19: try mgebbe@19: Usermap.scan ((user) -> mgebbe@19: if user.id.indexOf('@google') isnt -1 mgebbe@19: console.log "start sync for google user" mgebbe@19: Google.sync(user) mgebbe@19: ), (err) -> mgebbe@19: catch err mgebbe@19: console.log 'Error!' + err mgebbe@19: #### mgebbe@19: ], (err, result) -> mgebbe@19: callback null, 'done' mgebbe@19: mgebbe@19: return mgebbe@19: mgebbe@19: mgebbe@19: postParser = (post, user, network, callback) -> mgebbe@19: parsed = "" mgebbe@19: #console.log "\n" + post.id + "\n" + user.id + user.displayName mgebbe@19: #PROFILE_LINK PROFILE_NAME PROFILE_PIC_LINK_80x80=$3 POST_LINK POST_TIME CONTENT mgebbe@19: #text=' '$PROFILE_NAME' wrotes at '$POST_TIME':

'$CONTENT'' mgebbe@19: mgebbe@19: if network is 'facebook' mgebbe@19: if user? and user.profilePicLink? mgebbe@19: profilePicLink = user.profilePicLink.replace("50x50", "80x80") mgebbe@19: else mgebbe@19: profilePicLink = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/80px-F_icon.svg.png' mgebbe@19: mgebbe@19: if post.type is 'status' mgebbe@19: parsed = " " + user.displayName + " wrotes via " + network + " at " + post.updated_time + ":

" + post.message mgebbe@19: mgebbe@19: if post.type is 'photo' mgebbe@19: parsed = " " + user.displayName + " wrotes via " + network + " at " + post.updated_time + ":
" mgebbe@19: parsed += "
" + post.message if post.message? and post.message isnt "" mgebbe@19: parsed += "
" mgebbe@19: mgebbe@19: if post.type is 'link' or post.type is 'video' mgebbe@19: parsed = " " + user.displayName + " wrotes via " + network + " at " + post.updated_time + ":
" mgebbe@19: parsed += "
" + post.message if post.message? and post.message isnt "" mgebbe@19: parsed += "
" + post.description if post.description? mgebbe@19: parsed += "
"+ post.link + "" if post.link? mgebbe@19: parsed += "
" if post.picture? mgebbe@19: mgebbe@19: callback null, parsed mgebbe@19: mgebbe@19: if network is 'google' mgebbe@19: parsed = " " + post.actor.displayName + " wrotes via " + network + " at " + post.updated + ":

" + post.object.content mgebbe@19: if not post.object.attachments? mgebbe@19: _.each post.object.attachments, (attachment) -> mgebbe@19: mgebbe@19: if attachment.objectType == "photo" mgebbe@19: parsed += "
" mgebbe@19: mgebbe@19: if attachment.objectType == "article" mgebbe@19: parsed += "
"+ attachment.displayName + "" mgebbe@19: if not attachment.image? mgebbe@19: parsed += "
" mgebbe@19: mgebbe@19: if attachment.objectType == "video" mgebbe@19: parsed += "
"+ attachment.displayName + "" mgebbe@19: if not attachment.image? mgebbe@19: parsed += "
" mgebbe@19: mgebbe@19: callback null, parsed mgebbe@19: mgebbe@19: if network is 'twitter' mgebbe@19: mgebbe@19: parsed = " " + post.user.name + " wrotes via " + network + " at " + post.created_at + ":

" + post.text mgebbe@19: _.each post.entities.media, (attachment) -> mgebbe@19: parsed += "
" mgebbe@19: callback null, parsed mgebbe@19: mgebbe@19: return mgebbe@19: mgebbe@19: mgebbe@19: sync = () -> mgebbe@19: mgebbe@19: # Do this every xx minutes mgebbe@19: console.log '\n\n\n' + "starting sync deamon" mgebbe@19: interval = config.interval mgebbe@19: if not (interval?) mgebbe@19: interval = 15 * 60 * 1000 # 900 000 ms (15min) mgebbe@19: setInterval syncFromESN, interval mgebbe@19: mgebbe@19: syncFromESN() mgebbe@19: mgebbe@19: return mgebbe@19: mgebbe@19: sync() mgebbe@19: mgebbe@19: exports.postParser = postParser mgebbe@19: exports.sync = sync