changeset 5:af9f5b94c019

receive tweets
author Mathias Gebbe <mgebbe@intevation.de>
date Fri, 06 Jun 2014 14:42:28 +0200
parents f352c74a6a5b
children a94ac7c3b182
files src/sync.coffee src/twitter.coffee
diffstat 2 files changed, 44 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/sync.coffee	Fri Jun 06 10:18:14 2014 +0200
+++ b/src/sync.coffee	Fri Jun 06 14:42:28 2014 +0200
@@ -118,6 +118,13 @@
 
     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 + "'> schreibt </a> via " + network + " am " + post.created_at + ":<br><br>" + post.text
+    _.each post.entities.media, (attachment) ->
+      parsed += "<br><img src='" + attachment.media_url + "'></img>"
+    callback null, parsed
+
   return
 
 
--- a/src/twitter.coffee	Fri Jun 06 10:18:14 2014 +0200
+++ b/src/twitter.coffee	Fri Jun 06 14:42:28 2014 +0200
@@ -25,10 +25,12 @@
 RequestToken = PumpIOClientApp.RequestToken
 Usermap = require("./usermap")
 Pump = require("./pumpio")
+FromESN = require("./fromESN")
 ToESN = require("./toESN")
-
+Sync = require("./sync")
 
 module.exports = (config) ->
+  bridgeid = config.bridgeid
   client_id = config.twclient_id
   client_secret = config.twclient_secret
   request_token_endpoint = "https://api.twitter.com/oauth/request_token"
@@ -86,6 +88,7 @@
 
     sync: (user) ->
       me = user.user_pumpio
+      id = user.user_ESN.substr(0,user.user_ESN.indexOf('@'))
       token = user.oauth_token
       secret = user.extra_token
       util = require("util")
@@ -139,8 +142,41 @@
             callback null, 'done'
       ],(err, result) ->
     
+      ######################################
+      # get tweets and post them to pump.io#
+      ######################################
+      twit.verifyCredentials((data) ->
+        #console.log util.inspect(data)
+        return
+      ).getHomeTimeline {include_rts: false}, (data) ->
+        #console.log util.inspect(data)
+        _.each data, (tweet) ->
+          async.waterfall [
+            (callback) ->
+              FromESN.search {uid: tweet.id + "@twitter_to_" + me, recipientUser: me}, callback
+            (result, callback) ->
+              return if result.length isnt 0 or tweet.user.id == id
+              Sync.postParser tweet, null, 'twitter', callback
+            (parsed, callback) ->
+              Pump.postUser bridgeid, me, parsed, callback
+            (pumppost, callback) ->
+              pumppost = JSON.parse(pumppost)
+              FromESN.create
+                postid: tweet.id + "@twitter"
+                sourceUser: tweet.user.id
+                sourcePost: 'https://twitter.com/'+tweet.user.name+"/status/" + tweet.id
+                pumpPost: pumppost.object.id
+                recipientUser: me
+                created: Date.now()
+              , callback
+          ], (err, result) ->
+              #console.log result
+        # end
+        return
       return
 
+      return
+  
     getOAuth: (site) ->
       new OAuth(request_token_endpoint, access_token_endpoint, client_id, client_secret, "1.0", site.url("/authorized-for-twitter"), "HMAC-SHA1", null, # nonce size; use default
         "User-Agent": site.userAgent()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)