mgebbe@0: # Copyright (C) 2014 by Intevation GmbH mgebbe@0: # Author: Mathias Gebbe mgebbe@0: # mgebbe@3: # This file is Free Software under the Apache License, Version 2.0; mgebbe@3: # and comes with NO WARRANTY! mgebbe@3: # See the documentation coming with pumpbridge for details. mgebbe@0: mgebbe@0: _ = require("underscore") mgebbe@0: async = require("async") mgebbe@0: Facebook = require("./facebook") mgebbe@0: Usermap = require("./usermap") mgebbe@0: Google = require("./google") mgebbe@3: Config = require ("./config") mgebbe@3: config = Config.config mgebbe@3: Twitter = require("./twitter")(config) mgebbe@0: mgebbe@0: syncFromESN = () -> mgebbe@0: console.log 'syncFromESN' mgebbe@0: mgebbe@0: # STEP1: get all from usermap *network (3) "usermap:mgebbe@io.intevation.de_to_100002056487693@facebook" mgebbe@0: # STEP2: for each user in network do sync mgebbe@0: # STEP 2.1 (do the network sync) mgebbe@0: # get all new POSTS from ESN mgebbe@0: # post all new POSTS to ESN mgebbe@0: # check all FROMESN and update them mgebbe@0: # check all toESN and update them mgebbe@0: mgebbe@0: #### mgebbe@0: # Facebook mgebbe@0: try mgebbe@0: Usermap.scan ((user) -> mgebbe@7: if user.id.indexOf('@facebook') isnt -1 mgebbe@0: console.log "start sync for facebook user" mgebbe@0: Facebook.sync(user) mgebbe@0: ), (err) -> mgebbe@0: catch err mgebbe@19: console.log 'Error!' + err mgebbe@0: mgebbe@3: # Twitter mgebbe@3: try mgebbe@3: Usermap.scan ((user) -> mgebbe@7: if user.id.indexOf('@twitter') isnt -1 mgebbe@3: console.log "start sync for twitter user" mgebbe@3: Twitter.sync(user) mgebbe@3: ), (err) -> mgebbe@3: catch err mgebbe@19: console.log 'Error!' + err mgebbe@3: mgebbe@3: mgebbe@0: # Google mgebbe@0: try mgebbe@0: Usermap.scan ((user) -> mgebbe@7: if user.id.indexOf('@google') isnt -1 mgebbe@0: console.log "start sync for google user" mgebbe@0: Google.sync(user) mgebbe@0: ), (err) -> mgebbe@0: catch err mgebbe@19: console.log 'Error!' + err mgebbe@0: #### mgebbe@0: return mgebbe@0: mgebbe@0: mgebbe@0: postParser = (post, user, network, callback) -> mgebbe@0: parsed = "" mgebbe@0: #console.log "\n" + post.id + "\n" + user.id + user.displayName mgebbe@0: #PROFILE_LINK PROFILE_NAME PROFILE_PIC_LINK_80x80=$3 POST_LINK POST_TIME CONTENT mgebbe@7: #text=' '$PROFILE_NAME' wrotes at '$POST_TIME':

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

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

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

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