changeset 19:6b3f38af3fa4

use child_process for backend
author Mathias Gebbe <mgebbe@intevation.de>
date Fri, 13 Jun 2014 18:13:36 +0200
parents c552faae9c1c
children 9436298e6d78
files src/app.coffee src/google.coffee src/sync.coffee src/syncALONE.coffee
diffstat 4 files changed, 161 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/app.coffee	Fri Jun 13 16:37:20 2014 +0200
+++ b/src/app.coffee	Fri Jun 13 18:13:36 2014 +0200
@@ -36,9 +36,18 @@
         on address %s port %d', config.address, config.port
 
         # Start Deamon!
-        Sync.sync()
+        # or comment to the next hashtag after ')' and user
+        # Sync.sync()
+        # or start coffee syncALONE.coffee in a different window
+        exec = require("child_process").exec
+        child = undefined
+        child = exec("coffee syncALONE.coffee", (error, stdout, stderr) ->
+          console.log "stdout: " + stdout
+          console.log "stderr: " + stderr
+          console.log "exec error: " + error  if error isnt null
+          return
+        )
+        #
 
-        #app.get '/hello', (req, res) ->
-        # res.send 'Hello, Zaiste!'
       return
 ], (err, result) ->
--- a/src/google.coffee	Fri Jun 13 16:37:20 2014 +0200
+++ b/src/google.coffee	Fri Jun 13 18:13:36 2014 +0200
@@ -49,7 +49,7 @@
         (callback) ->
           getStream(user, friend, callback)
         (stream, callback) ->
-          return if not stream?
+          return if not stream? or not stream.items? or stream.items.length is 0
           async.each stream.items, ((post, cb) ->
             async.waterfall [
               (callback) ->
--- a/src/sync.coffee	Fri Jun 13 16:37:20 2014 +0200
+++ b/src/sync.coffee	Fri Jun 13 18:13:36 2014 +0200
@@ -34,7 +34,7 @@
         Facebook.sync(user)
     ), (err) ->
   catch err
-    console.log 'Error!'
+    console.log 'Error!' + err
 
   # Twitter
   try
@@ -44,7 +44,7 @@
         Twitter.sync(user)
     ), (err) ->
   catch err
-    console.log 'Error!'
+    console.log 'Error!' + err
 
 
   # Google
@@ -55,7 +55,7 @@
         Google.sync(user)
     ), (err) ->
   catch err
-    console.log 'Error!'
+    console.log 'Error!' + err
   ####
   return
 
@@ -132,6 +132,5 @@
 
   return
 
-
 exports.postParser = postParser
 exports.sync = sync
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/syncALONE.coffee	Fri Jun 13 18:13:36 2014 +0200
@@ -0,0 +1,145 @@
+# Copyright (C) 2014 by Intevation GmbH
+# Author: Mathias Gebbe <mgebbe@intevation.de>
+#
+# This file is Free Software under the Apache License, Version 2.0;
+# and comes with NO WARRANTY!
+# See the documentation coming with pumpbridge for details.
+
+_ = require("underscore")
+async = require("async")
+Facebook = require("./facebook")
+Usermap = require("./usermap")
+Google = require("./google")
+Config = require ("./config")
+config = Config.config
+Twitter = require("./twitter")(config)
+
+databank = require("databank")
+Databank = databank.Databank
+DatabankObject = databank.DatabankObject
+
+db = Databank.get(config.driver, config.params)
+DatabankObject.bank = db
+
+syncFromESN = () ->
+  console.log 'syncFromESN'
+
+  async.waterfall [
+    (callback) ->
+      db.connect(config.params, callback)
+    (callback) ->
+      ####
+      # Facebook
+    
+      try
+        Usermap.scan ((user) ->
+          if user.id.indexOf('@facebook') isnt -1
+            console.log "start sync for facebook user"
+            Facebook.sync(user)
+        ), (err) ->
+      catch err
+        console.log 'Error!' + err
+    
+      # Twitter
+      try
+        Usermap.scan ((user) ->
+          if user.id.indexOf('@twitter') isnt -1
+            console.log "start sync for twitter user"
+            Twitter.sync(user)
+        ), (err) ->
+      catch err
+        console.log 'Error!' + err
+    
+    
+      # Google
+      try
+        Usermap.scan ((user) ->
+          if user.id.indexOf('@google') isnt -1
+            console.log "start sync for google user"
+            Google.sync(user)
+        ), (err) ->
+      catch err
+        console.log 'Error!' + err
+      ####
+  ], (err, result) ->
+      callback null, 'done'
+
+  return
+
+
+postParser = (post, user, network, callback) ->
+  parsed = ""
+  #console.log "\n" + post.id + "\n" + user.id + user.displayName
+  #PROFILE_LINK PROFILE_NAME PROFILE_PIC_LINK_80x80=$3 POST_LINK POST_TIME CONTENT
+  #text='<img src='$PROFILE_PIC_LINK_80x80'></img> <a href='$PROFILE_LINK'>'$PROFILE_NAME'</a> <a href='$POST_LINK'>wrotes</a> at '$POST_TIME':<br><br>'$CONTENT''
+
+  if network is 'facebook'
+    if user? and user.profilePicLink?
+      profilePicLink = user.profilePicLink.replace("50x50", "80x80")
+    else
+      profilePicLink = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/80px-F_icon.svg.png'
+
+    if post.type is 'status'
+      parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='https://www.facebook.com/" + post.id + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br><br>" + post.message
+
+    if post.type is 'photo'
+      parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='"+ post.link + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br>"
+      parsed += "<br>" + post.message if post.message? and post.message isnt ""
+      parsed += "<br><img src='" + post.picture + "'></img>"
+
+    if post.type is 'link' or post.type is 'video'
+      parsed = "<img src='" + profilePicLink + "'></img> <a href='" + user.profileLink + "'>" + user.displayName + "</a> <a href='https://www.facebook.com/" + post.id + "'> wrotes</a> via " + network + " at " + post.updated_time + ":<br>"
+      parsed += "<br>" + post.message if post.message? and post.message isnt ""
+      parsed += "<br>" + post.description if post.description?
+      parsed +=  "<br><a href='" + post.link + "'>"+ post.link + "</a>" if post.link?
+      parsed += "<br><img src='" + post.picture + "'></img>" if post.picture?
+
+    callback null, parsed
+
+  if network is 'google'
+    parsed = "<img src='" + post.actor.image.url + "'></img> <a href='" + post.actor.url + "'>" + post.actor.displayName + "</a> <a href='"+ post.object.url + "'> wrotes</a> via " + network + " at " + post.updated + ":<br><br>" + post.object.content
+    if not post.object.attachments?
+      _.each post.object.attachments, (attachment) ->
+
+        if attachment.objectType == "photo"
+          parsed += "<br><img src='" + attachment.image.url + "'></img>"
+
+        if attachment.objectType == "article"
+          parsed +=  "<br><a href='" + attachment.url + "'>"+ attachment.displayName + "</a>"
+          if not attachment.image?
+            parsed += "<br><img src='" + attachment.image.url + "'></img>"
+
+        if attachment.objectType == "video"
+          parsed +=  "<br><a href='" + attachment.url + "'>"+ attachment.displayName + "</a>"
+          if not attachment.image?
+            parsed += "<br><img src='" + attachment.image.url + "'></img>"
+
+    callback null, parsed
+
+  if network is 'twitter'
+
+    parsed = "<img src='" + post.user.profile_image_url + "'></img> <a href='https://twitter.com/account/redirect_by_id/" + post.user.id + "'>" + post.user.name + "</a> <a href='https://twitter.com/" + post.user.screen_name + "/status/" + post.id_str + "'> wrotes </a> via " + network + " at " + post.created_at + ":<br><br>" + post.text
+    _.each post.entities.media, (attachment) ->
+      parsed += "<br><img src='" + attachment.media_url + "'></img>"
+    callback null, parsed
+
+  return
+
+
+sync = () ->
+
+  # Do this every xx minutes
+  console.log '\n\n\n' + "starting sync deamon"
+  interval = config.interval
+  if not (interval?)
+    interval =  15 * 60 * 1000 # 900 000 ms (15min)
+  setInterval syncFromESN, interval
+
+  syncFromESN()
+
+  return
+
+sync()
+
+exports.postParser = postParser
+exports.sync = sync
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)