comparison src/twitter.coffee @ 28:faeb3b96bdeb

use html-to-text
author Mathias Gebbe <mgebbe@intevation.de>
date Tue, 29 Jul 2014 12:14:02 +0200
parents f27ddfa42e94
children 3e3fa35e3ce2
comparison
equal deleted inserted replaced
27:f27ddfa42e94 28:faeb3b96bdeb
27 Pump = require("./pumpio") 27 Pump = require("./pumpio")
28 FromESN = require("./fromESN") 28 FromESN = require("./fromESN")
29 ToESN = require("./toESN") 29 ToESN = require("./toESN")
30 Sync = require("./sync") 30 Sync = require("./sync")
31 util = require("util") 31 util = require("util")
32 htmlToText = require('html-to-text')
32 twitter = require("twitter") 33 twitter = require("twitter")
33 34
34 module.exports = (config) -> 35 module.exports = (config) ->
35 bridgeid = config.bridgeid 36 bridgeid = config.bridgeid
36 client_id = config.twclient_id 37 client_id = config.twclient_id
114 else 115 else
115 return 116 return
116 (feed, callback) -> 117 (feed, callback) ->
117 return if not feed? 118 return if not feed?
118 ti = new Date().getTime() - interval 119 ti = new Date().getTime() - interval
119 async.eachLimit feed.items, 10, ((post, callback) -> 120 async.eachLimit feed.items, 5, ((post, callback) ->
120 # do for each post 121 # do for each post
121 ts = Date.parse(post.updated) 122 ts = Date.parse(post.updated)
122 if (ts >= ti and post.verb is "post" or post.verb is "share") and (post.object.objectType is "note" or post.object.objectType is "image") and (Pump.isPublicActivity(post)) and (typeof post.object.deleted is "undefined") 123 if (ts >= ti and post.verb is "post" or post.verb is "share") and (post.object.objectType is "note" or post.object.objectType is "image") and (Pump.isPublicActivity(post)) and (typeof post.object.deleted is "undefined")
123 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) -> 124 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) ->
124 if result.length is 0 125 if result.length is 0
125 # post to twitter 126 # post to twitter
126 status = "" 127 status = ""
127 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '') 128 text = post.object.content
128 re = new RegExp(String.fromCharCode(160), "g") 129 text = htmlToText.fromString(text,wordwrap: 140)
129 text = text.replace(re, " ") 130 #text = post.object.content.replace(/<(?:.|\n)*?>/gm, '')
131 #re = new RegExp(String.fromCharCode(160), "g")
132 #text = text.replace(re, " ")
130 if post.verb is "share" 133 if post.verb is "share"
131 status = post.object.author.url + " writes: " 134 status = post.object.author.url + " writes: "
132 if text.length <= 140 135 if text.length <= 140
133 status += text 136 status += text
134 else 137 else
153 return 156 return
154 callback null, 'done' 157 callback null, 'done'
155 ), (err) -> 158 ), (err) ->
156 callback null, 'done' 159 callback null, 'done'
157 ],(err, result) -> 160 ],(err, result) ->
158 console.log "done ToESN twitter user" + me 161 console.log "done ToESN twitter user " + me
159 return 162 return
160 return 163 return
161 164
162 syncFromESN: (user) -> 165 syncFromESN: (user) ->
163 me = user.user_pumpio 166 me = user.user_pumpio
175 ###################################### 178 ######################################
176 twit.verifyCredentials((data) -> 179 twit.verifyCredentials((data) ->
177 #console.log util.inspect(data) 180 #console.log util.inspect(data)
178 console.log "get twitter HomeTimeline " + me 181 console.log "get twitter HomeTimeline " + me
179 return 182 return
180 ).getHomeTimeline {include_rts: true, count: 10 }, (data) -> 183 ).getHomeTimeline {include_rts: true, count: 15 }, (data) ->
181 console.log "got twitter HomeTimeline " + me 184 console.log "got twitter HomeTimeline " + me
182 return if not data? or data.length is 0 185 return if not data? or data.length is 0
183 console.log "data isnt empty for" + me 186 console.log "data isnt empty for " + me
184 ti = (new Date().getTime() - interval) 187 ti = (new Date().getTime() - interval)
185 async.eachLimit data, 5, ((tweet, cb) -> 188 async.eachLimit data, 1, ((tweet, cb) ->
186 async.waterfall [ 189 async.waterfall [
187 (callback) -> 190 (callback) ->
188 FromESN.search {uid: tweet.id + "@twitter_to_" + me}, callback 191 FromESN.search {uid: tweet.id + "@twitter_to_" + me}, callback
189 (result, callback) -> 192 (result, callback) ->
190 return if result.length isnt 0 or tweet.user.id_str is id 193 return if result.length isnt 0 or tweet.user.id_str is id
211 pumpPost: pumppost.object.id 214 pumpPost: pumppost.object.id
212 recipientUser: me 215 recipientUser: me
213 created: Date.now() 216 created: Date.now()
214 , callback 217 , callback
215 ], (err, result) -> 218 ], (err, result) ->
216 console.log "post a tweet to user" + me 219 console.log "post a tweet to user " + me
217 cb() 220 cb()
218 ), (err) -> 221 ), (err) ->
219 if (err) 222 if (err)
220 console.log "Error: " + err 223 console.log "Error: " + err
221 else 224 else
222 console.log "done FromESN twitter user" + me 225 console.log "done FromESN twitter user " + me
223 return 226 return
224 227
225 getOAuth: (site) -> 228 getOAuth: (site) ->
226 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 229 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
227 "User-Agent": site.userAgent() 230 "User-Agent": site.userAgent()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)