comparison src/twitter.coffee @ 7:2123f355ab68

go back interval or 15 min, delete credentials, layout
author Mathias Gebbe <mgebbe@intevation.de>
date Wed, 11 Jun 2014 02:32:53 +0200
parents af9f5b94c019
children 6fd0a307850f
comparison
equal deleted inserted replaced
6:a94ac7c3b182 7:2123f355ab68
31 31
32 module.exports = (config) -> 32 module.exports = (config) ->
33 bridgeid = config.bridgeid 33 bridgeid = config.bridgeid
34 client_id = config.twclient_id 34 client_id = config.twclient_id
35 client_secret = config.twclient_secret 35 client_secret = config.twclient_secret
36 interval = config.interval
37 if not (interval?)
38 interval = 15 * 60 * 1000
36 request_token_endpoint = "https://api.twitter.com/oauth/request_token" 39 request_token_endpoint = "https://api.twitter.com/oauth/request_token"
37 access_token_endpoint = "https://api.twitter.com/oauth/access_token" 40 access_token_endpoint = "https://api.twitter.com/oauth/access_token"
38 authorization_endpoint = "https://api.twitter.com/oauth/authorize" 41 authorization_endpoint = "https://api.twitter.com/oauth/authorize"
39 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json" 42 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json"
40 hostname = "twitter.com" 43 hostname = "twitter.com"
105 (callback) -> 108 (callback) ->
106 Usermap.search {id: me + '_to_' + me}, callback 109 Usermap.search {id: me + '_to_' + me}, callback
107 (user, callback) -> 110 (user, callback) ->
108 Pump.getUserFeed(user[0],callback) 111 Pump.getUserFeed(user[0],callback)
109 (feed, callback) -> 112 (feed, callback) ->
113 ti = new Date().getTime() - interval
110 async.eachSeries feed.items, ((post, callback) -> 114 async.eachSeries feed.items, ((post, callback) ->
111 # do for each post 115 # do for each post
112 if (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") 116 ts = Date.parse(post.updated)
117 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")
113 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) -> 118 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) ->
114 if result.length is 0 119 if result.length is 0
115 # post to twitter 120 # post to twitter
121 status = ""
116 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '') 122 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '')
117 status = "" 123 if post.verb is "share"
124 status = post.object.author.url + " wrotes: "
118 if text.length <= 140 125 if text.length <= 140
119 status = text 126 status += text
120 else 127 else
121 status = text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url 128 status += text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url
122 twit.verifyCredentials((data) -> 129 twit.verifyCredentials((data) ->
123 #console.log util.inspect(data) 130 #console.log util.inspect(data)
124 return 131 return
125 ).updateStatus status, (data) -> 132 ).updateStatus status, (data) ->
126 async.waterfall [ 133 async.waterfall [
146 # get tweets and post them to pump.io# 153 # get tweets and post them to pump.io#
147 ###################################### 154 ######################################
148 twit.verifyCredentials((data) -> 155 twit.verifyCredentials((data) ->
149 #console.log util.inspect(data) 156 #console.log util.inspect(data)
150 return 157 return
151 ).getHomeTimeline {include_rts: false}, (data) -> 158 ).getHomeTimeline {include_rts: true, count: 20 }, (data) ->
152 #console.log util.inspect(data) 159 #console.log util.inspect(data)
160 ti = (new Date().getTime() - interval)
153 _.each data, (tweet) -> 161 _.each data, (tweet) ->
154 async.waterfall [ 162 async.waterfall [
155 (callback) -> 163 (callback) ->
156 FromESN.search {uid: tweet.id + "@twitter_to_" + me, recipientUser: me}, callback 164 FromESN.search {uid: tweet.id + "@twitter_to_" + me, recipientUser: me}, callback
157 (result, callback) -> 165 (result, callback) ->
158 return if result.length isnt 0 or tweet.user.id == id 166 return if result.length isnt 0 or tweet.user.id_str is id
159 Sync.postParser tweet, null, 'twitter', callback 167 twitterdate = new Date(Date.parse(tweet.created_at.replace(/( +)/, " UTC$1")))
168 ts = Date.parse(twitterdate)
169 if (ts >= ti)
170 Sync.postParser tweet, null, 'twitter', callback
171 return
160 (parsed, callback) -> 172 (parsed, callback) ->
161 Pump.postUser bridgeid, me, parsed, callback 173 Pump.postUser bridgeid, me, parsed, callback
162 (pumppost, callback) -> 174 (pumppost, callback) ->
163 pumppost = JSON.parse(pumppost) 175 pumppost = JSON.parse(pumppost)
164 FromESN.create 176 FromESN.create
174 # end 186 # end
175 return 187 return
176 return 188 return
177 189
178 return 190 return
179 191
192 parseTwitterDate : (text) ->
193
180 getOAuth: (site) -> 194 getOAuth: (site) ->
181 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 195 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
182 "User-Agent": site.userAgent() 196 "User-Agent": site.userAgent()
183 ) 197 )
184 198
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)